【Spring Cloud Hystrix】快速入门(一)

前面文章基于https://www.jianshu.com/writer#/notebooks/24441977/notes/35969879文章的内容,搭建了Spring Cloud Ribbon + Eureka的Spring Boot的工程,介绍了Ribbon客户端负载的相关内容。这篇文章在此基础上,来聊聊Spring Cloud Hystrix。另外,本文基于Spring Boot的Finchley.SR2版本所构建。

快速入门

Hystrix服务端

  1. application.yml配置
    构建两个Spring Boot项目,分别为hystrix-server:9091、hystrix-server:9092,并配置分布式注册中心Eureka
server:
  port: 9091
spring:
  application:
    name: hystrix-server

eureka:
  client:
     serviceUrl:
        defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  instance:
    hostname: ${spring.application.name}
    instance-id: ${spring.application.name}:${server.port}
    appname: hystrix-server

Hystrix-server启动类,端口为9091、9092的Hystrix-server启动类一样,并提供了Restful风格的hello接口,这里就只列出Hystrix-server:9091的启动类。

@SpringBootApplication
@EnableEurekaClient
@RestController
public class HystrixServer1Application {
    public static void main(String[] args) {
        SpringApplication.run(HystrixServer1Application.class, args);
    }
    @GetMapping("/hello")
    public String hello(){
        return "hello,this is :"+this.getClass().getName();
    }
}

Hystrix客户端:Hystrix-client
pom配置


        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.SR2
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix-dashboard
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

application.yml配置

server:
  port: 9094
spring:
  application:
    name: hystrix-client
  cloud:
    loadbalancer:
      retry:
        enabled: true #开启重试机制
hystrix-server:
  ribbon:
    ConnectTimeout: 250 #请求连接超时
    ReadTimeout: 1000  #请求处理的超时时间
    OkToRetryOnAllOperations: true #对所有操作请求都进行重试
    MaxAutoRetriesNextServer: 2 #切换实例的重试次数
    maxAutoRetries: 2 #对当前实例的重试次数

eureka:
  client:
     serviceUrl:
        defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  instance:
    hostname: ${spring.application.name}
    instance-id: ${spring.application.name}:${server.port}
    appname: hystrix-client

客户端代码:

@SpringBootApplication
@RestController
@EnableCircuitBreaker
@EnableEurekaClient
public class HystrixClient2Application {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
    @Resource
    HelloService helloService;

    @GetMapping("hystrix-hello")
    public String hello(){
        return helloService.hello();
    }
    public static void main(String[] args) {
        SpringApplication.run(HystrixClient2Application.class, args);
    }
}

@Component
public class HelloService {
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String hello(){
        String msg= restTemplate.getForEntity("http://hystrix-server/hello",String.class).getBody();
        return msg;
    }
    public String helloFallback(){
        return "hello , error";
    }
}

依次启动Eureka-server、hystrix-client服务


Eureka界面

此时调用http://localhost:9094/hystrix-hello,由于Hystrix-server并没有启动,所以在执行String msg= restTemplate.getForEntity("http://hystrix-server/hello",String.class).getBody()时,发生异常,继而执行helloFallback()方法。会打印出 hello , error

现在启动Hystrix-server服务,Eureka监控界面如下:


Eureka监控界面

再次调用http://localhost:9094/hystrix-hello,则会轮番打印出下面的信息

hello,this is :springcloud.hystrixserver2.HystrixServer2Application$$EnhancerBySpringCGLIB$$5b5dd2e6
hello,this is :springcloud.hystrixserver1.HystrixServer1Application$$EnhancerBySpringCGLIB$$92afab75

你可能感兴趣的:(【Spring Cloud Hystrix】快速入门(一))