前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Turbine聚合Hystrix

Spring Cloud Turbine聚合Hystrix

作者头像
胖虎
发布2019-06-26 16:24:42
3960
发布2019-06-26 16:24:42
举报
文章被收录于专栏:晏霖

前言

前面文章提到了使用了Feign集成的Hystrix做了一个简单的实战练习,成功的进行了服务降级和失败快速返回。下面我要对熔断器监控进行页面化,并将多个服务的的熔断器监控页面进行聚合,方便管理,也是实际生产最典型的例子。当然这种做法最系统最合理,但是我个人以及周围的朋友在平时讨论的结果看,熔断器的聚合以及页面化管理应用概率并不多,即使在生产环境也很少使用,下面我边讲解边解释原因。

正文

首先,大家都知道一个服务需要依赖spring-cloud-starter-netflix-hystrix才可以实现熔断器功能,需要依赖spring-cloud-starter-netflix-hystrix-dashboard才可以使用熔断器监控页面。想要聚合所有服务熔断器的监控页面需要spring-cloud-starter-netflix-turbine。

第一步

还是跟以前一样,我们准备一个父项目,还有一个eureka-server,代码在上文已经贴出来过,没有什么变化。

第二步

准备两个客户端一个叫sc-hello-service,另一个叫sc-provider-service

下面贴出sc-hello-service项目的代码,首先我把项目的目录给大家,下面贴的代码比较多,每块代码都有类名,大家对照着类名把代码块对号入座(截图中的配置文件的代码我合成了一个代码块贴出来的)。

代码语言:javascript
复制
@RestController
public class HelloController {
 
	@Autowired
	private IHelloService userService;
    
    @GetMapping("/getProviderData")
    public List<String> getProviderData(){
        return userService.getProviderData();
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/helloService", method = RequestMethod.GET)
    public String getHelloServiceData() {
    	return "hello Service";
    }
}
代码语言:javascript
复制
@FeignClient(name = "sc-provider-service")
public interface ProviderService {
	
	@RequestMapping(value = "/getDashboard", method = RequestMethod.GET)
    public List<String> getProviderData();
 
}
代码语言:javascript
复制
@Component
public class HelloService implements IHelloService{
	
    @Autowired
    private ProviderService dataService;
 
	@Override
	public List<String> getProviderData() {
		return dataService.getProviderData();
	}
 
}
代码语言:javascript
复制
public interface IHelloService {
    public List<String> getProviderData();
}
代码语言:javascript
复制
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients
public class HelloServiceApplication {
	
    public static void main(String[] args) {
        SpringApplication.run(HelloServiceApplication.class, args);
    }
   
}
代码语言:javascript
复制
server:
  port: 9091
spring:
  application:
    name: sc-hello-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8761}/eureka/
  instance:
    prefer-ip-address: true
management:
  security:
    enabled: false
  endpoints:
    web:
      exposure:
        include: hystrix.stream
feign:
  hystrix:
    enabled: true
ribbon:
  ConnectTimeout: 6000
  ReadTimeout: 6000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 0
hystrix:
  command:
    default:
      execution:
        timeout:
        isolation:
          thread:
            timeoutInMilliseconds: 15000
代码语言:javascript
复制
<dependencies>
        <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</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-openfeign</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
</dependencies>

下面贴出sc-provider-service项目的代码,首先我把项目的目录给大家,下面贴的代码比较多,每块代码都有类名,大家对照着类名把代码块对号入座(截图中的配置文件的代码我合成了一个代码块贴出来的)。

代码语言:javascript
复制
@RestController
public class ProviderController {
	@Autowired
	private ConsumerService consumerService;
 
    @GetMapping("/getDashboard")
    public List<String> getProviderData(){
    	List<String> provider = new ArrayList<String>();
    	provider.add("hystrix dashboard");
        return provider;
    }
@GetMapping("/getHelloService")
    public String getHelloService(){
        return consumerService.getHelloServiceData();
    }
}
代码语言:javascript
复制
@FeignClient(name = "sc-hello-service")
public interface ConsumerService {
	
	@RequestMapping(value = "/helloService", method = RequestMethod.GET)
    public String getHelloServiceData();
}
代码语言:javascript
复制
server:
  port: 8099
spring:
  application:
    name: sc-provider-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8761}/eureka/
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true
management:
  security:
    enabled: false
  endpoints:
    web:
      exposure:
        include: hystrix.stream
代码语言:javascript
复制
<dependencies>
        <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</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-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
    </dependencies>
代码语言:javascript
复制
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ProviderServiceApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ProviderServiceApplication.class, args);
    }
    
}

第三步

创建一个聚合工程sc-turbine-dashboard,代码很简单除了一个启动类就是一个配置文件

下面依次贴出来,重点就是启动类加入类加入@EnableTurbine,

代码语言:javascript
复制
turbine:
  appConfig: sc-hello-service,sc-provider-service的意思是把这两个应用的服务聚合在一起,在一个页面显示。
代码语言:javascript
复制
@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbine
@EnableHystrixDashboard
public class TurbineApplication {
    public static void main(String[] args) {
        SpringApplication.run(TurbineApplication.class, args);
    }
}
代码语言:javascript
复制
server:
  port: 9088
spring:
  application:
    name: sc-turbine-dashboard
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8761}/eureka/
  instance:
    prefer-ip-address: true
management:
  security:
    enabled: false
  endpoints:
    web:
      exposure:
        include: hystrix.stream
turbine:
  appConfig: sc-hello-service,sc-provider-service
  clusterNameExpression: "'default'"

第四步

依次启动eureka-service,两个客户端sc-hello-service,sc-provider-service最后启动sc-turbine-dashboard,

然后在浏览器输入http://localhost:9088/hystrix显示如图,屏幕中间有三个地址,分别是监控方式:

第一个是默认集群监控,第二个是指定的集群,第三个是单个应用监控。

这时你进入的页面应该是loading中,这是你访问一下http://localhost:9091/getProviderData和http://localhost:8099/getHelloService

,这回再回头看你的聚合监控页面

这里面有很多参数,我不做讲解了,有时间我会单独发文说明这些参数代表说明意思。

下面我们来色是一下被调用的服务宕掉的 情况,可以发现页面参数的变化

在你访问http://localhost:9091/getProviderData和http://localhost:8099/getHelloService时,由于注册中心的定时任务,服务还没有注册在eureka上,或者两个都启动后,手动停掉一个服务,你访问的时候会看到有点方法出现100%,然后前面有一个1,说明失败请求这个方法1次。反复请求,1会增加到2……,但是停止访问,过一会又变回0和0.0%。这里告诉大家这里记录的都是失败的请求和最近10秒内的错误比率,曲线是2分钟内流量的变化趋势,所以前言我们说,很多时候这个聚合监控页面很少使用的原因:

1.错误信息保留时间短,甚至有的时候没来得及反应就消失了。

2.即使出现错误也不能自己修复和解决,hystrix已经有修复功能了,要你何用,用来看吗?

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 晏霖 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档