Spring Cloud Netflix

Spring Cloud Netflix

概述

Spring Cloud Netflix是Spring Cloud的子项目,集成了Netflix开源的微服务组件,包括Eureka、Hystrix、Zuul、Ribbon等。虽然Netflix已经将大部分组件置于维护模式,但这些组件在微服务架构中仍然发挥着重要作用。

核心组件

Eureka - 服务注册与发现

Eureka Server

java
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Eureka Client

java
@SpringBootApplication
@EnableEurekaClient
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}
@SpringBootApplication
@EnableEurekaClient
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}

Ribbon - 客户端负载均衡

java
@Configuration
public class RibbonConfig {
    
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@Service
public class UserService {
    
    @Autowired
    private RestTemplate restTemplate;
    
    public User getUser(Long id) {
        return restTemplate.getForObject("http://user-service/users/" + id, User.class);
    }
}
@Configuration
public class RibbonConfig {
    
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@Service
public class UserService {
    
    @Autowired
    private RestTemplate restTemplate;
    
    public User getUser(Long id) {
        return restTemplate.getForObject("http://user-service/users/" + id, User.class);
    }
}

Hystrix - 断路器

java
@Service
public class UserService {
    
    @HystrixCommand(fallbackMethod = "getUserFallback")
    public User getUser(Long id) {
        // 调用远程服务
        return restTemplate.getForObject("http://user-service/users/" + id, User.class);
    }
    
    public User getUserFallback(Long id) {
        return new User(id, "Unknown", "[email protected]");
    }
}
@Service
public class UserService {
    
    @HystrixCommand(fallbackMethod = "getUserFallback")
    public User getUser(Long id) {
        // 调用远程服务
        return restTemplate.getForObject("http://user-service/users/" + id, User.class);
    }
    
    public User getUserFallback(Long id) {
        return new User(id, "Unknown", "[email protected]");
    }
}

Zuul - API网关

java
@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulGatewayApplication.class, args);
    }
}
@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulGatewayApplication.class, args);
    }
}
yaml
zuul:
  routes:
    user-service:
      path: /api/users/**
      service-id: user-service
    order-service:
      path: /api/orders/**
      service-id: order-service
zuul:
  routes:
    user-service:
      path: /api/users/**
      service-id: user-service
    order-service:
      path: /api/orders/**
      service-id: order-service

组件状态

组件状态替代方案
Eureka维护模式Consul, Nacos
Ribbon维护模式Spring Cloud LoadBalancer
Hystrix维护模式Resilience4j, Sentinel
Zuul维护模式Spring Cloud Gateway
Feign活跃开发OpenFeign

迁移建议

从Hystrix到Resilience4j

java
// Hystrix
@HystrixCommand(fallbackMethod = "fallback")
public String getData() {
    return "data";
}

// Resilience4j
@CircuitBreaker(name = "service", fallbackMethod = "fallback")
public String getData() {
    return "data";
}
// Hystrix
@HystrixCommand(fallbackMethod = "fallback")
public String getData() {
    return "data";
}

// Resilience4j
@CircuitBreaker(name = "service", fallbackMethod = "fallback")
public String getData() {
    return "data";
}

从Zuul到Spring Cloud Gateway

yaml
# Zuul配置
zuul:
  routes:
    user-service:
      path: /api/users/**
      service-id: user-service

# Gateway配置
spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/api/users/**
# Zuul配置
zuul:
  routes:
    user-service:
      path: /api/users/**
      service-id: user-service

# Gateway配置
spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/api/users/**

总结

虽然Netflix组件进入维护模式,但它们为微服务架构奠定了重要基础。在新项目中建议使用更现代的替代方案,如Spring Cloud Gateway、Resilience4j等。