本文共 13786 字,大约阅读时间需要 45 分钟。
这里我用了两个生产者和两个消费者进行演示,如下图(画的不好看,凑活看看):
这里我就只讲下怎么注册到dashbord和相关的配置,提供者和消费者等代码可以去下载查看:
https://github.com/fengcharly/springCloud-ribbon-turbine.git
这里我将熔断器(或者称为断路器配置到了消费者端):
启动类:
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; } }; }}
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
输入
然后 我们访问,可以在仪表盘中看到如下的信息:
这时,我们的dashbord单个应用监控完毕,但是我们在实际应用中往往不止用到一个应用,这时就需要我们来监控多个应用,这边我们可以配置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,这时候我们在仪表盘再进行监控就可以看到多个应用的监控信息了:
配置监控的路径:
仪表盘显示状态:
附上源码地址:
转载地址:http://clxto.baihongyu.com/