三、Hystrix与Feign的结合使用以及服务监控

github地址 : https://github.com/JerrySunTao/microservicecloud

a.使用中的一些常用注解

前面几篇文章主要是单个介绍某些组件在Springcloud中的使用,下面我们来使用feign与Hystrix组合的方式来完成微服务之间的调用以及服务出现问题时的熔断降级!

1)eureka服务注解
@SpringBootApplication
@EnableEurekaServer

spring cloud 集成flume spring cloud 集成azure ad注解_github

2)服务提供端注解

@SpringBootApplication

@EnableEurekaClient

@EnableDiscoveryClient

spring cloud 集成flume spring cloud 集成azure ad注解_微服务_02


3)feign接口相关注解(服务消费端)

@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients(basePackages = “com.taosun.springcloud”)

@ComponentScan(“com.taosun.springcloud”)

@FeignClient(value = “MICROSERVICECLOUD-DEPT”,fallbackFactory =DeptClientServiceFallbackFactory.class)

spring cloud 集成flume spring cloud 集成azure ad注解_github_03


spring cloud 集成flume spring cloud 集成azure ad注解_微服务_04

因为feign本身集成了Hystrix所以需要在配置中开启

spring cloud 集成flume spring cloud 集成azure ad注解_spring_05

4)具体使用案例:

spring cloud 集成flume spring cloud 集成azure ad注解_github_06


如上图1处,服务只启动了一台eureka服务、一台消费服务,此时模拟服务提供方出现问题!feign接口信息:

spring cloud 集成flume spring cloud 集成azure ad注解_微服务_07


熔断方法如下:

spring cloud 集成flume spring cloud 集成azure ad注解_微服务_08


调用feign接口浏览器返回信息:

spring cloud 集成flume spring cloud 集成azure ad注解_github_09


可以看出此时熔断服务已经生效!

b.服务监控

1)注意事项!如下图:

spring cloud 集成flume spring cloud 集成azure ad注解_github_10


这个问题应该出现的概率比较高吧!

下面我们来说下Hystrix能监控我们服务的两个必要条件:

a. 项目中使用到了Hystrix;

b. 引入了相关注解

<!-- hystrix和hystrixDashboard相关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>

        <!-- actuator监控信息完善-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

以上条件缺一不可!

一下为我的springboot springcloud 版本信息:

<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.9.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

2)监控配置信息(服务消费者)

配置中开启hystrix:

spring cloud 集成flume spring cloud 集成azure ad注解_spring_11

启动类信息:

spring cloud 集成flume spring cloud 集成azure ad注解_spring_12

服务熔断降级:

spring cloud 集成flume spring cloud 集成azure ad注解_微服务_13


spring cloud 集成flume spring cloud 集成azure ad注解_github_14


(api具体的使用方法在这里就不废话了,可以自行百度或官网查看文档)

以上条件满足后我们启动服务!

spring cloud 集成flume spring cloud 集成azure ad注解_github_15


6001:服务消费者;

9001:监控服务;

8001、8002:服务提供者;

7001:eureka服务;访问http://localhost:9001/hystrix:

spring cloud 集成flume spring cloud 集成azure ad注解_微服务_16


访问需要监控的服务 http://localhost:6001/hystrix.stream:

spring cloud 集成flume spring cloud 集成azure ad注解_github_17

使用postman访问接口:

spring cloud 集成flume spring cloud 集成azure ad注解_spring_18


它会监控每个访问的接口,并在下方展示调用结果!不同颜色代表不同状!

上面的监控存在一定的缺陷,即只能对单台服务进行监控,无法满足单模块集群部署的情况,当有10台时,难道你开10个tab页面分别去看吗?

下面介绍使用Turbine聚合监控数据,来监控同一个微服务模块的数据情况!

(1)如下图所示服务:

spring cloud 集成flume spring cloud 集成azure ad注解_spring_19


两个消费者6001,6002,集群模式!

(2)配置Turbine

加入引用
<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-turbine</artifactId>
		</dependency>

配置信息:

server:
  port: 9002


spring:
  application:
    name: microservicecloud-turbine
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

  instance:
    instance-id: microservicecloudConsumerHystrixTurbine-9002
    prefer-ip-address: true

turbine:
  appConfig: microservicecloud-feign
  clusterNameExpression: "'default'"

appConfig:这个属性一定要注意!否则你会发现启动后控制台会出现:com.netflix.turbine.monitor.instance.instancemonitor$misconfiguredhostexception :。。。。。404.。。。。。等相关错误信息!
监控页面会一直处于loading的状态!

为什么会这样呢?因为appConfig后面的监控的微服务应用名你写错了!请检查!被监控的应用一定要满足上面提到的 a b 两个要求!

启动类信息:

spring cloud 集成flume spring cloud 集成azure ad注解_spring_20

启动服务:

spring cloud 集成flume spring cloud 集成azure ad注解_github_21


这里我消费服务修改了不同的端口号,启动了两次!6001,6002;检查eureka注册情况

spring cloud 集成flume spring cloud 集成azure ad注解_spring_22

Turbine是需要注册到eureka的!

访问监控地址:http://localhost:9002/turbine.stream

spring cloud 集成flume spring cloud 集成azure ad注解_微服务_23


使用postman修改端口访问!发现host参数发生变化!即此页面统计的为当前微服务集群的访问情况!

至此!hystrix使用结束!更多组合使用方案!大家可以自行发觉!