使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能

RestTemplate是Spring Resources中一个访问第三方RestFul Api接口的网络请求框架。
Ribbon是Netflix公司开源的一个负载均衡的组件。

创建三个项目 eureka-server、service-order、client-order-ribbon

eureka-server

eureka-server 的创建比较简单,依赖 spring-cloud-starter-netflix-eureka-server


         org.springframework.cloud
         spring-cloud-starter-netflix-eureka-server

启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

application.yml

spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
       defaultZone: http://${eureka.instance.hostname}:8762/eureka/
    register-with-eureka: false
    fetch-registry: false

启动之后,访问 http://localhost:8761

service-order

创建项目依赖


            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

启动类

@EnableEurekaClient
@SpringBootApplication
public class ServiceOrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceOrderApplication.class, args);
    }
}

application.yml

spring:
  application:
    name: service-order
server:
  port: 8764
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

创建一个OrderController类

@RequestMapping("/order")
@RestController
public class OrderController {

    @Value("${server.port}")
    String port;

    @RequestMapping("info")
    public String orderInfo(@RequestParam("order_id") String orderId) {
        return "order id is "+ orderId+ ", port: " + port;
    }
}

启动项目 访问 http://localhost:8764/order/info?order_id=123123
游览器结果:

使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能_第1张图片
j1.png

client-order-ribbon

创建client-order-ribbon项目所需依赖


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server


    org.springframework.cloud
    spring-cloud-starter-netflix-ribbon


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix-dashboard


    org.springframework.boot
    spring-boot-starter-actuator

application.yml

spring:
  application:
    name: client-order-ribbon
server:
  port: 8766
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

启动类

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class ClientOrderRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientOrderRibbonApplication.class, args);
    }
  
/**
     * springboot 版本如果是2.0则需要添加 ServletRegistrationBean
     * 因为springboot的默认路径不是 "/hystrix.stream",
     * 只要在自己的项目里配置上下面的servlet就可以了
     * 第一次访问hystrix.stream 会出现 Unable to connect to Command Metric Stream
     *
     */
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

只需要在程序的Ioc容器中注入一个resttemplate的Bean,并加上@LoadBalanced注解,这样RestTemplate就结合了Ribbon开启了负载均衡功能
创建一个RibbonConfig类:

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

创建一个OrderRibbonService类:

@Service
public class OrderRibbonService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "orderInfoError")
    public String orderInfo(String orderId) {
        return restTemplate.getForObject("http://service-order:8764/order/info?order_id="+orderId, String.class);
    }

    public String orderInfoError(String orderId) {
        return "sorry, error ribbon hystrix!";
    }
}

写一个OrderController类:

@RestController
public class OrderController {
    @Autowired
    OrderRibbonService orderRibbonService;
    @GetMapping("/order/info")
    public String orderInfo(@RequestParam("id") String orderId) {
        return orderRibbonService.orderInfo(orderId);
    }
}

依次启动eureka-server、service-order、client-order-ribbon项目,项目service-order启动两个端口分别是 8764、8864
在游览器多次访问 http://localhost:8766/order/info?id=123123

使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能_第2张图片
j3.png

使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能_第3张图片
j2.png

测试一下Hystrix熔断功能 ,停止两个service-order,再访问 http://localhost:8766/order/info?id=123123

使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能_第4张图片
最.png

查看Hystrix Dashboard 访问 http://localhost:8766/hystrix

使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能_第5张图片
j5.png

在输入框中输入 http://localhost:8766/hystrix.stream 点击Monitor Stream按钮
使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能_第6张图片
j6.png

下面这图引用网络的图片:


使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能_第7张图片
3447292083-59db4162b0dd4_articlex.png

你可能感兴趣的:(使用RestTemplate和Ribbon来消费服务和Hystrix熔断功能)