博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hystrix集群及集群监控turbine
阅读量:3961 次
发布时间:2019-05-24

本文共 11077 字,大约阅读时间需要 36 分钟。

目录

Hystrix集群及监控turbine

前面Dashboard演示的仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine。

turbine是基于Dashboard的。

演示集群服务监控

新建项目microservice-student-provider-hystrix

在microservice-student-provider-hystrix-1004项目的基础上再搞一个
代码和配置都复制一份,然后修改几个地方

==1、yml配置 ==

---server:  port: 1004  context-path: /spring:  datasource:    type: com.alibaba.druid.pool.DruidDataSource    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8    username: root    password: 123  jpa:    hibernate:      ddl-auto: update    show-sql: true  application:    name: microservice-student  profiles: provider-hystrix-1004eureka:  instance:    hostname: localhost    appname: microservice-student    instance-id: microservice-student:1004    prefer-ip-address: true  client:    service-url:      defaultZone: http://eureka2001.zxp.com:2001/eureka/,http://eureka2002.zxp.com:2002/eureka/,http://eureka2003.zxp.com:2003/eureka/info:  groupId: com.zxp.testSpringcloud  artifactId: microservice-student-provider-hystrix-1004  version: 1.0-SNAPSHOT  userName: http://zxp.com  phone: 123456hystrix:  command:    default:      execution:        isolation:          thread:            timeoutInMilliseconds: 1500---server:  port: 1005  context-path: /spring:  datasource:    type: com.alibaba.druid.pool.DruidDataSource    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8    username: root    password: 123  jpa:    hibernate:      ddl-auto: update    show-sql: true  application:    name: microservice-student  profiles: provider-hystrix-1005eureka:  instance:    hostname: localhost    appname: microservice-student    instance-id: microservice-student:1005    prefer-ip-address: true  client:    service-url:      defaultZone: http://eureka2001.zxp.com:2001/eureka/,http://eureka2002.zxp.com:2002/eureka/,http://eureka2003.zxp.com:2003/eureka/info:  groupId: com.zxp.testSpringcloud  artifactId: microservice-student-provider-hystrix-1005  version: 1.0-SNAPSHOT  userName: http://zxp.com  phone: 123456hystrix:  command:    default:      execution:        isolation:          thread:            timeoutInMilliseconds: 1500---server:  port: 1006  context-path: /spring:  datasource:    type: com.alibaba.druid.pool.DruidDataSource    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8    username: root    password: 123  jpa:    hibernate:      ddl-auto: update    show-sql: true  application:    name: microservice-student  profiles: provider-hystrix-1006eureka:  instance:    hostname: localhost    appname: microservice-student    instance-id: microservice-student:1006    prefer-ip-address: true  client:    service-url:      defaultZone: http://eureka2001.zxp.com:2001/eureka/,http://eureka2002.zxp.com:2002/eureka/,http://eureka2003.zxp.com:2003/eureka/info:  groupId: com.zxp.testSpringcloud  artifactId: microservice-student-provider-hystrix-1006  version: 1.0-SNAPSHOT  userName: http://zxp.com  phone: 123456hystrix:  command:    default:      execution:        isolation:          thread:            timeoutInMilliseconds: 1500

2、启动类配置

package com.zxp.microservicestudentproviderhystrix;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.domain.EntityScan;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableCircuitBreaker@EntityScan("com.zxp.*.*")@EnableEurekaClient@SpringBootApplicationpublic class MicroserviceStudentProviderHystrixApplication {    public static void main(String[] args) {        SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args);    }}

以上就有了 hystrix集群服务

新建项目microservice-student-consumer-hystrix-turbine-91
1、pom.xml加下依赖

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

2、application.yml

server:  port: 91  context-path: /eureka:  client:    service-url:      defaultZone: http://eureka2001.zxp.com:2001/eureka/,http://eureka2002.zxp.com:2002/eureka/,http://eureka2003.zxp.com:2003/eureka/turbine:  app-config: microservice-student   # 指定要监控的应用名称  clusterNameExpression: "'default'" #表示集群的名字为defaultspring:  application:    name: turbine

3、新建启动类MicroserviceStudentConsumerHystrixTurbine91Application 加注解:@EnableTurbine

package com.zxp.microservicestudentconsumerhystrixturbine91;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;import org.springframework.cloud.netflix.turbine.EnableTurbine;@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})@EnableTurbinepublic class MicroserviceStudentConsumerHystrixTurbine91Application {    public static void main(String[] args) {        SpringApplication.run(MicroserviceStudentConsumerHystrixTurbine91Application.class, args);    }}

测试

先启动三个eureka,然后把1004 1005 带hystrix的服务都启动
microservice-student-consumer-80这个也启动,方便测试
dashboard,turbine启动
这样的话 http://localhost/student/hystrix 就能调用服务集群

http://localhost:91/turbine.stream 可以监控数据,实时ping 返回data

在这里插入图片描述
输入http://localhost:90/hystrix进入仪表盘,输入地址
在这里插入图片描述
点击 进入集群监控仪表:
在这里插入图片描述

Feign、Hystrix整合

前面的代码,用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需要解耦,这我们讲下Feign Hystrix整合。

Feign Hystrix整合

1、microservice-student-provider-hystrix项目修改

StudentService加新的接口方法:

/** * 测试Hystrix服务降级 * @return */public Map
hystrix();

StudentServiceImpl写具体实现:

@Value("${server.port}")    private String port;    @Override    public Map
hystrix() { Map
map=new HashMap
(); map.put("code", 200); map.put("info","工号【"+port+"】正在为您服务"); return map; }

StudentProviderController正常调用service方法:

/**     * 测试Hystrix服务降级     * @return     * @throws InterruptedException     */    @ResponseBody    @GetMapping(value="/hystrix")//    @HystrixCommand(fallbackMethod="hystrixFallback")    public Map
hystrix() throws InterruptedException{ Thread.sleep(100);// Map
map=new HashMap
();// map.put("code", 200);// map.put("info","工号【"+port+"】正在为您服务"); return this.studentService.hystrix(); }// public Map
hystrixFallback() throws InterruptedException{// Map
map=new HashMap
();// map.put("code", 500);// map.put("info", "系统【"+port+"】繁忙,稍后重试");// return map;// }

2、microservice-common项目新建FallbackFactory类,解耦服务熔断服务降级

StudentClientService接口,新增getInfo方法

/** * 服务熔断降级 * @return */@GetMapping(value="/student/hystrix")public Map
hystrix();

新建 StudentClientFallbackFactory 类,实现FallbackFactory接口

package com.zxp.microservicecommon.service;import com.zxp.microservicecommon.entity.Student;import feign.hystrix.FallbackFactory;import org.springframework.stereotype.Component;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @author笑笑 * @site www.xiaoxiao.com * @company * @create 2020-01-13 11:42 */@Componentpublic class StudentClientFallbackFactory  implements FallbackFactory
{ @Override public StudentClientService create(Throwable cause) { return new StudentClientService() { @Override public boolean save(Student student) { return false; } @Override public List
list() { return null; } @Override public Map
hystrix() { Map
map=new HashMap
(); map.put("code", 500); map.put("info", "系统繁忙,稍后重试"); return map; } @Override public Student get(Integer id) { return null; } @Override public boolean delete(Integer id) { return false; } @Override public String ribbon() { return null; } }; }}

StudentClientService接口的@FeignClient注解加下 fallbackFactory属性

package com.zxp.microservicecommon.service;import com.zxp.microservicecommon.entity.Student;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.stereotype.Service;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import java.util.List;import java.util.Map;/** * Student Feign接口客户端 * @author Administrator * */@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory=StudentClientFallbackFactory.class)//实现了 降级处理方法实现@Servicepublic interface StudentClientService {     /**     * 根据id查询学生信息     * @param id     * @return     */    @GetMapping(value="/student/get/{id}")    public Student get(@PathVariable("id") Integer id);         /**     * 查询学生信息     * @return     */    @GetMapping(value="/student/list")    public List
list(); /** * 添加或者修改学生信息 * @param student * @return */ @PostMapping(value="/student/save") public boolean save(Student student); /** * 根据id删除学生信息 * @return */ @GetMapping(value="/student/delete/{id}") public boolean delete(@PathVariable("id") Integer id); @RequestMapping("/student/ribbon") public String ribbon(); /** * 服务熔断降级 * @return */ @GetMapping(value="/student/hystrix") public Map
hystrix();}

3、microservice-student-consumer-feign-80修改 支持Hystrix

StudentConsumerFeignController新增方法调用

/** * Feign整合Hystrix服务熔断降级 * @return * @throws InterruptedException */@GetMapping(value="/hystrix")public Map
hystrix() throws InterruptedException{ return studentClientService.hystrix();}

4、microservice-student-consumer-feign-80的application.yml加上hystrix支持

feign:  hystrix:    enabled: true

5、microservice-student-consumer-feign-80的启动类上添加公共模块

@ComponentScan(basePackages = {"com.zxp.microservicecommon","com.zxp.microservicestudentconsumerfeign80"})

注意:

公共子项目与当前子项目的基包都要扫描到;
只指定公共子模块为基包会导致本子项目的springmvc功能失效;
只指定本子项目为基包会导致feign与Hystrix集成失败,从而导致服务熔断功能失效

测试

在这里插入图片描述
Bug:超过1.5秒的话,没有返回错误提示。

集群后超时设置

上面错误是什么原因呢,咱们明明在Hystrix中的application.yml中设置了

在这里插入图片描述
因为还有一个 feign 也有一个超时时间的设置,当然feign底层是 ribbon的封装,所以 直接配置ribbon,ribbon默认超时也是1秒。所以这里都是强制要求,ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义。
解决
microservice-student-consumer-feign-80上加个 ribbon超时时间设置

ribbon:  ReadTimeout: 10000  ConnectTimeout: 9000hystrix:  command:    default:      execution:        isolation:          thread:            timeoutInMilliseconds: 1500

测试

在这里插入图片描述
在这里插入图片描述

转载地址:http://kyrzi.baihongyu.com/

你可能感兴趣的文章
安装 docker-compose (实测可用,妈妈再也不用担心被墙了)
查看>>
docker下删除none的images
查看>>
Linux提权获取敏感信息方法
查看>>
Ubuntu 16.04开机A start job is running for Raise network interface(5min 4s)解决方法
查看>>
Ubuntu 16.04开机隐藏菜单缩短时间
查看>>
Ubuntu 更换国内源
查看>>
Ubuntu16.04下Docker pull connection refused 解决办法
查看>>
postgres基本操作(个人总结版)
查看>>
The AnimationClip 'Walk' used by the Animation component 'Pig' must be marked as Legacy.
查看>>
《Linux内核设计与实现》- Linux的进程
查看>>
inet_ntoa()
查看>>
POSIX消息队列mq_open问题
查看>>
两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];
查看>>
用户态切换到内核态的3种方式
查看>>
笔试常见的智力题(附答案)
查看>>
内核库函数
查看>>
Linux 系统内核空间与用户空间通信的实现与分析
查看>>
如何写好应用型学术论文
查看>>
如何查看进程的各种限制
查看>>
64位int类型用printf输出问题
查看>>