美文网首页
基于SpringBoot2.0.3 实现Hystrix Turb

基于SpringBoot2.0.3 实现Hystrix Turb

作者: running_house | 来源:发表于2018-07-18 17:07 被阅读0次

    一、开发注册中心eureka服务

    添加依赖

        <!--eureka依赖-->
        <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--actoator依赖-->
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    

    application.properties配置文件

      #应用名称
      spring.application.name=eureka-server
      #端口号
      server.port=8761
      #HOST地址
      spring.cloud.client.ipAddress=192.168.22.78
      #注册中心地址
      eureka.client.service-url.default-zone=http://${spring.cloud.client.ipAddress}:${server.port}/eureka/
      #不向注册中心注册自己
      eureka.client.register-with-eureka=false
      #不从注册中心获取注册信息
      eureka.client.fetch-registry=false
    

    启动类中增加启用注册中心服务

    //启用注册中心
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    

    二:开发服务提供者PROVIDER-USER

    添加依赖

        <!--actoator依赖-->
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--jpa依赖-->
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--web依赖-->
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
        <!--eureka依赖()-->
         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
         </dependency>
    

    application.properties配置文件

    #应用名称
    spring.application.name=provider-user
    #端口号
    server.port=8000
    #HOST地址
    spring.cloud.client.ipAddress=192.168.22.78
    #注册中心地址
    eureka.client.service-url.default-zone=http://192.168.22.78:8761/eureka/
    eureka.instance.prefer-ip-address=true
    eureka.instance.metadata-map.my-metadata:我自定义的元数据
    #数据库配置
    spring.jpa.generate-ddl=false
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    spring.datasource.platform=h2
    spring.datasource.schema=classpath:sql/schema.sql
    spring.datasource.data=classpath:sql/data.sql
    
    #配置日志
    logging.level.root=INFO
    logging.level.org.hibernate=INFO
    logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
    logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
    
    #配置info端点信息
    info.app.name=@project.artifactId@
    info.app.encoding=@project.build.sourceEncoding@
    info.app.java.source=${java.version}
    info.app.java.target=${java.version}
    

    启动类中增加启用注册中心服务

    @SpringBootApplication
    //启用注册中心
    @EnableDiscoveryClient
    public class ProviderUserApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProviderUserApplication.class, args);
        }
    }
    

    开发对外接口查询用户信息

    @RestController
    public class UserController {
        @Autowired
        private UserRepository userRepository;
        @GetMapping("/{id}")
        public User findById(@PathVariable Long id){
            Optional<User> op = userRepository.findById(id);
            return op.get();
        }
    }
    

    三:开发消费者CONSUMER-MOVIE

    添加依赖

        <!--actoator依赖-->
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</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>
        <!--web依赖-->
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
        <!--eureka依赖()-->
         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
         </dependency>
    

    application.properties配置文件

    #应用名称
    spring.application.name=consumer-movie
    #端口号
    server.port=8010
    #注册中心地址
    eureka.client.service-url.default-zone=http://192.168.22.78:8761/eureka/
    eureka.instance.prefer-ip-address=true
    

    启动类中增加启用注册中心服务

    
    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableCircuitBreaker
    public class ConsumerMovieApplication {
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
          // SpringBoot2.0以后,不提供 hystrix.stream节点,需要自己增加
        @Bean
        public ServletRegistrationBean getServlet(){
            HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
            registrationBean.setLoadOnStartup(1);
            registrationBean.addUrlMappings("/actuator/hystrix.stream");
            registrationBean.setName("HystrixMetricsStreamServlet");
            return registrationBean;
        }
        public static void main(String[] args) {
            SpringApplication.run(ConsumerMovieApplication.class, args);
        }
    }
    

    调用PROVIDER-USER接口,查询用户信息

    @RestController
    public class MovieController {
        private static final Logger logger = LoggerFactory.getLogger(MovieController.class);
    
        @Autowired
        private RestTemplate restTemplate;
    
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @GetMapping("/user/{id}")
        @HystrixCommand(fallbackMethod = "findByIdFallback")
        public User findById(@PathVariable Long id) {
            return this.restTemplate.getForObject("http://PROVIDER-USER/" + id, User.class);
        }
        public User findByIdFallback(Long id){
            User user = new User();
            user.setId(-1L);
            user.setName("默认用户");
            return user;
        }
        @GetMapping("/user-instance")
        public List<ServiceInstance> showInfo() {
            return this.discoveryClient.getInstances("PROVIDER-USER");
        }
    }
    

    四:开发消费者二CONSUMER-MOVIE-FEGIN-CLIENT

    具体步骤同第三步

    五、开发监控平台

    添加依赖

        <!--监控平台依赖-->
         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
         </dependency>
    

    application.properties配置文件

    #端口号
    server.port=8030
    

    启动类中增加启用注册中心服务

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

    六、开发聚合服务Turbine

    添加依赖

        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</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-turbine</artifactId>
        </dependency>
    

    application.properties配置文件

    #端口号
    server.port=8031
    #项目名称
    spring.application.name=hystrix-turbine
    #注册中心地址
    eureka.client.service-url.default-zone=http://192.168.22.78:8761/eureka/
    eureka.instance.prefer-ip-address=true
    #turbine聚合
    # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
    turbine.aggregator.cluster-config=default
    
    turbine.cluster-name-expression= new String("default")
    turbine.app-config=consumer-movie,consumer-movie-fegin-client
    #这里和被监控启动类里的 registrationBean.addUrlMappings("/hystrix.stream")一致
    turbine.instanceUrlSuffix=/actuator/hystrix.stream
    
    

    启动类中增加启用注册中心服务

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

    七、启动查看结果

    1、启动项目:eureka-server
    2、启动项目:provider-user
    3、启动项目:consumer-movie
    4、启动项目:consumer-movie-fegin-client
    5、启动项目:hystrix-dashboard
    6、启动项目:hystrix-turbine
    7、访问http://localhost:8010/user/1让consumer-movie微服务产生监控数据。
    8、访问http://localhost:8011/user/1让consumer-movie-fegin-client微服务产生监控数据。
    9、打开Hystrix Dashboard 首页 http://localhost:8030/hystrix,在URL一栏填入http://localhost:8031/turbine.stream,随意指定一个Title并点击Monitor Stream按钮,结果类似于下图

    image.png

    相关文章

      网友评论

          本文标题:基于SpringBoot2.0.3 实现Hystrix Turb

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