美文网首页编程杂货铺
使用RestTemplate和Ribbon来消费服务和Hystr

使用RestTemplate和Ribbon来消费服务和Hystr

作者: 会动的木头疙瘩儿 | 来源:发表于2018-09-17 17:08 被阅读9次

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

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

    eureka-server

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

    <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    

    启动类

    @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

    创建项目依赖

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    

    启动类

    @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
    游览器结果:

    j1.png

    client-order-ribbon

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    

    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

    j3.png j2.png

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

    最.png

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

    j5.png
    在输入框中输入 http://localhost:8766/hystrix.stream 点击Monitor Stream按钮
    j6.png

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


    3447292083-59db4162b0dd4_articlex.png

    相关文章

      网友评论

        本文标题:使用RestTemplate和Ribbon来消费服务和Hystr

        本文链接:https://www.haomeiwen.com/subject/thhrnftx.html