Preface:
Recently the newly recruited staff, the company uses a SpringCloud, before there to find out about SpringCloud, but for a long time not to set up not to use it is easy to forget, so the spare time to re-review the SpringCloud. But before the open version SpringCloud may be a bit low, the company is now using version "Greenwich.RELEASE" of, SpringBoot use the "2.1.x" version is relatively new, so when using Hystrix-Dashboard will be a little pit Therefore I wanted to step on the pit recorded, so that more people away from the pit, ado, started!
First, create Hystrix-Dashboard monitoring services: springcloud-hystrix-dashboard
- Engineering pom-dependent
<?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 https://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.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gxc.test</groupId>
<artifactId>springcloud-hystrix-dashboard</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- yml profile
server:
port: 10000
spring:
application:
name: springcloud-hystrix-dashboard
- Add annotations @EnableHystrixDashboard on startup class
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
- start up! Access address browser: HTTP: // localhost: 10000 / hystrix , as shown below:
Second, create a registry Eureka: springcloud-eureka-server
Some readers may ask: play Hystrix-Dashboard is not to say it? Why have to use Eureka?
A: Please be patient to look down, which is why I say the title of the pit ...
- Engineering pom-dependent
<?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 https://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.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gxc.test</groupId>
<artifactId>springcloud-hystrix-dashboard</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- yml profile
server:
port: 9000
spring:
application:
name: springcloud-eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka
- Start class add annotations: @EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- start up! Browser access: HTTP: // localhost: 9000 / , as shown below:
Third, create a monitored service: springcloud-user
- Engineering pom-dependent
<?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 https://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.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gxc.test</groupId>
<artifactId>springcloud-user</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- yml profile
server:
port: 10001
spring:
application:
name: springcloud-user
# 将服务注册到Eureka注册中心
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9000/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}
feign:
hystrix:
enabled: true
- Add comment startup class
//@EnableCircuitBreaker
//@EnableEurekaClient
//@EnableFeignClients
//@SpringBootApplication
// 以上四个注解可以用如下两个注解取代
@EnableFeignClients
@SpringCloudApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
- Create a test class Controller
@RestController
public class UserController {
@GetMapping("/user")
public Object getUser() {
Map<String, Object> user = Maps.newHashMap();
user.put("username","GongXincheng");
user.put("age", 23);
user.put("birthday", new Date());
return user;
}
}
- start up! Browser access: HTTP: // localhost: 10001 / the User , as shown below:
Fourth, the test Hystrix-Dashboard to monitor user services
- Browser access: HTTP: // localhost: 10001 / hystrix.stream , there have been 404
Description: The first pit, before SpringBoot 2.0, just add the Actuator depend, will not be 404 errors
- 404 to solve the problem: Create a profile class, and register a Servlet
@Configuration
public class ServletConfigBean {
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean regist = new ServletRegistrationBean();
regist.setServlet(new HystrixMetricsStreamServlet());
regist.setName("hystrixMetricsStreamServlet");
regist.setLoadOnStartup(1);
regist.addUrlMappings("/hystrix.stream");
return regist;
}
}
- Browser access: HTTP: // localhost: 10001 / hystrix.stream , as shown below:
The first step of the link: HTTP: // localhost: 10001 / hystrix.stream , copied to Hystrix-Dashboard page
Hystrix-Dashboard will always loading ..., this is the second pit ...
- Solution:
1:创建另一个服务(例如:springcloud-order)
2:将新的服务(order)注册到Eureka
3:在user服务写一个新的Api接口,通过 Feign 访问新服务(order)的Api接口
4:注意:经过本人测试通过RestTemplate的方式调用,仍然没有效果。
5:当通过Feign调用一次新服务(order)后,hystrix.stream 正常,效果如下:
- Controller level code
@RestController
public class UserController {
private static final String ORDER_SERVER_URL = "http://springcloud-order";
@Resource private OrderFeign orderFeign;
@Resource private RestTemplate restTemplate;
@GetMapping("/user")
public Object getUser() {
Map<String, Object> user = Maps.newHashMap();
user.put("username","GongXincheng");
user.put("age", 23);
user.put("birthday", new Date());
return user;
}
@GetMapping("/feign/order")
public Object feignGetOrder() {
return orderFeign.getOrder();
}
@GetMapping("/rest/order")
public Object restGetOrder() {
return restTemplate.getForObject(ORDER_SERVER_URL + "/order", Object.class);
}
}
8: the User Services and oder service directory structure and the relevant code
Five: Code links
https://gitee.com/gxc6/gxc-springcloud-study
If there is something wrong place, welcome to correct me help! thank!
I have been through this ... to finish the ... 3:00 slipped slipped ...