博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
feign中的hytrix和turbin配置
阅读量:6658 次
发布时间:2019-06-25

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

feign中的hytrix和turbin配置

这里我用了两个生产者和两个消费者进行演示,如下图(画的不好看,凑活看看):

img_2991a6088801857ab49310cdb69a5a2e.png
image.png

这里我就只讲下怎么注册到dashbord和相关的配置,提供者和消费者等代码可以去下载查看:

https://github.com/fengcharly/springCloud-ribbon-turbine.git

1.hystrix的配置:

这里我将熔断器(或者称为断路器配置到了消费者端):

启动类:

pom.xml:

4.0.0
com.consumer
stu-consumer-feign-hytrix
0.0.1-SNAPSHOT
jar
stu-consumer
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR5
pom
import
io.spring.platform
platform-bom
Brussels-SR9
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.2.0
mysql
mysql-connector-java
com.alibaba
druid
1.0.25
org.projectlombok
lombok
org.springframework.security
spring-security-jwt
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.session
spring-session
org.springframework.session
spring-session-data-redis
org.springframework.cloud
spring-cloud-starter-sleuth
org.springframework.cloud
spring-cloud-sleuth-zipkin
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-ribbon
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.boot
spring-boot-maven-plugin

StuConsumerApplication:

package com.consumer.stuconsumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication@EnableEurekaClient@EnableFeignClients@EnableCircuitBreakerpublic class StuConsumerApplication {    public static void main(String[] args) {        SpringApplication.run(StuConsumerApplication.class, args);    }}

控制层:

import com.consumer.stuconsumer.entity.Student;import com.consumer.stuconsumer.feign.UserFeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestControllerpublic class ConsumerController {    @Resource    private UserFeignClient userFeignClient;    @RequestMapping("/getAll/{id}")    public Student getAll(@PathVariable("id") Integer id) {        Student stu = userFeignClient.getAll(id);        return stu;    }}

Feign:(这个是配置断路器的主要配置)

ConsumerFeign:

import com.consumer.stuconsumer.entity.Student;import com.consumer.stuconsumer.feign.fallbackfactory.UserFallbackFactory;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;//使用FeignClient 告知发布方的应用名称 默认使用ribbon进行负载均衡@FeignClient(name = "stu-provide",fallbackFactory = UserFallbackFactory.class)public interface ConsumerFeign {    @RequestMapping(value = "/getAll/{id}",method = RequestMethod.GET)    public Student getAll(@PathVariable("id") Integer id);}

UserFeignWithFactory:

import com.consumer.stuconsumer.feign.ConsumerFeign;//这个是hystrix的类public interface UserFeignWithFactory extends ConsumerFeign {}

UserFallbackFactory:

import com.consumer.stuconsumer.entity.Student;import com.consumer.stuconsumer.feign.ConsumerFeign;import com.consumer.stuconsumer.feign.feignclientwithfactory.UserFeignWithFactory;import feign.hystrix.FallbackFactory;import org.springframework.stereotype.Component;//断路器设置//这个是hystrix的类@Componentpublic class UserFallbackFactory implements FallbackFactory
{ @Override public ConsumerFeign create(Throwable throwable) { return new UserFeignWithFactory(){ @Override public Student getAll(Integer id) { return null; } }; }}

2.建立dashbord

pom.xml:

4.0.0
com
stu-dashbord
0.0.1-SNAPSHOT
jar
stu-dashbord
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
Finchley.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-turbine
org.springframework.cloud
spring-cloud-netflix-turbine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

StuDashbordApplication:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.cloud.netflix.turbine.EnableTurbine;@SpringBootApplication@EnableHystrixDashboardpublic class StuDashbordApplication {    public static void main(String[] args) {        SpringApplication.run(StuDashbordApplication.class, args);    }}

application.yml:

server:  port: 8030

3.启动dashbord并访问::

img_d0dc9ec465e087c66cf8c0678eeb64c8.png
image.png

输入

img_447ff21c85d8dad4bdcff5d9c0cec2ba.png
image.png

然后 我们访问,可以在仪表盘中看到如下的信息:

img_be46cd5feac9a0f2bfc2bcaade9cfa31.png
image.png

这时,我们的dashbord单个应用监控完毕,但是我们在实际应用中往往不止用到一个应用,这时就需要我们来监控多个应用,这边我们可以配置turbine来进行应用的监控集群:

4.建立turbine

pom.xml:

4.0.0
com
stu-turbine
0.0.1-SNAPSHOT
jar
stu-turbine
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
Finchley.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-turbine
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

StuTurbineApplication:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.turbine.EnableTurbine;@EnableTurbine@EnableDiscoveryClient@SpringBootApplicationpublic class StuTurbineApplication {    public static void main(String[] args) {        SpringApplication.run(StuTurbineApplication.class, args);    }}

application.yml(重点关注):

server:  port: 8031spring:  application:    name: stu-hystrix-turbineeureka:  client:    serviceUrl:      defaultZone: http://user:password123@localhost:8761/eureka  instance:    prefer-ip-address: trueturbine:  aggregator:    clusterConfig: default  appConfig:  stu-consumer-feign-hytrix,stu-consumer  clusterNameExpression: "'default'"  instanceUrlSuffix: /hystrix.stream#turbine.instanceUrlSuffix=/xxx/hystrix.stream

我们这边一定要配置 instanceUrlSuffix: /hystrix.stream,当然可以少一个/,这边不配置这个路径的话会报路径错误,这个是指定他路径后缀用的.

然后我们启动turbine,这时候我们在仪表盘再进行监控就可以看到多个应用的监控信息了:

配置监控的路径:

img_5cfb64859303cb97f3db5083b451cc51.png
image.png

仪表盘显示状态:

img_ea2183eb3aa5fc8164d0cfb7e2037a7a.png
image.png

附上源码地址:

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

你可能感兴趣的文章
C++ static,extern ,静态,全局量
查看>>
Selenium在定位的class含有空格的复合类的解决办法整理
查看>>
性能测试基本知识
查看>>
python pip安装模块提示错误failed to create process
查看>>
SWT 初步试水
查看>>
信息奥赛初赛总结
查看>>
Java BigDecimal
查看>>
git创建项目报错:please tell me who you are
查看>>
IClone地形编辑器结合T4M插件在Unity3D使用
查看>>
配置静态文件
查看>>
MetaMask/safe-event-emitter
查看>>
linux下nginx配置ssl证书(https)
查看>>
Gulp-前端进阶A-2
查看>>
程序员应当注意的肢体语言
查看>>
【好文翻译】码农们:效率与质量,你选择哪个?
查看>>
PHP正则表达式
查看>>
从文件 I/O 看 Linux 的虚拟文件系统
查看>>
StarUML类图相关——关联、聚合、组合、泛化、依赖、实现
查看>>
LeetCode - 627. Swap Salary
查看>>
ie8、firfox关于(new Image()).onload的问题
查看>>