本文仅供学习所用,如若侵权,请联系qq2935268135,立马删除。
本文是基于SpringBoot、idea、maven所做SpringCloud的断路器监控。
注册中心
jar包下载
百度云
链接:https://pan.baidu.com/s/1-0ZWPwAH2_7xysze9lAAQw
提取码:vprd
或本网下载
https://download.csdn.net/download/shen_yun2017/10904251
Hosts
修改C:\Windows\System32\drivers\etc\下的 hosts文件,添加如下代码
127.0.0.1 peer1
127.0.0.1 peer2
pom
pom文件全部代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lzy</groupId>
<artifactId>sleuth-client1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sleuth-client1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--熔断器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
yml
提供yml方式格式参考
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8001/eureka/
management:
endpoints:
web:
exposure:
include: hystrix.stream
properties
properties格式参考
server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-client1
个人推荐yml格式,清晰明了
启动类
package com.lzy.sleuthclient1;
import brave.sampler.Sampler;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
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.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.logging.Logger;
@SpringBootApplication
@RestController
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SleuthClient1Application {
/**
* 访问地址 http://localhost:8988/actuator/hystrix.stream
*/
@Autowired
private RestTemplate restTemplate;
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
private static final Logger logger = Logger.getLogger(SleuthClient1Application.class.getName());
public static void main(String[] args) {
SpringApplication.run(SleuthClient1Application.class, args);
}
@RequestMapping(value = "/hi")
@HystrixCommand(fallbackMethod = "hiError")
public String client1Hi(String name){
logger.info("hi"+name);
return restTemplate.getForObject("http://localhost:8989/hello?name="+"client1Hi "+name,String.class) ;
}
@RequestMapping(value = "/info")
public String info(String name){
logger.info("info"+name);
return name+" info";
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}
访问
启动服务运行jar
java -jar service-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar service-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2如上步骤
启动服务
访问地址:localhost:8988/hi?name=5653255626
访问:http://localhost:8988/actuator/hystrix.stream
访问:http://localhost:8988/hystrix
含义描述图片来源:https://my.oschina.net/xiaominmin/blog/1788456
参考学习有
[1]: https://blog.csdn.net/forezp/article/details/81041125
[2]: https://my.oschina.net/xiaominmin/blog/1788456