最近刚入职,公司使用了SpringCloud,之前有了解过SpringCloud,但是长时间不去搭建不去使用很容易就忘了,因此空闲时间重新复习一下SpringCloud。但是之前开的SpringCloud的版本可能有点低,公司现在用的 " Greenwich.RELEASE "的版本,SpringBoot使用了“ 2.1.x ”的版本,算是比较新了,因此在使用 Hystrix-Dashboard 的时候会有点坑,因此想把踩到的坑记录下来,让更多的人避开这个坑,废话不多说,开始了!
<?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>
server:
port: 10000
spring:
application:
name: springcloud-hystrix-dashboard
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
有读者可能会问:不是说玩 Hystrix-Dashboard 吗?为啥还要用到Eureka?
答:请耐心往下看,这也是我为什么题目说的坑...
<?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>
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
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
<?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>
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
//@EnableCircuitBreaker
//@EnableEurekaClient
//@EnableFeignClients
//@SpringBootApplication
// 以上四个注解可以用如下两个注解取代
@EnableFeignClients
@SpringCloudApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
@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;
}
}
@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;
}
}
将第一步的链接:http://localhost:10001/hystrix.stream ,复制到 Hystrix-Dashboard页面中
Hystrix-Dashboard会一直在 loading... 中,这就是第二个坑...
1:创建另一个服务(例如:springcloud-order)
2:将新的服务(order)注册到Eureka
3:在user服务写一个新的Api接口,通过 Feign 访问新服务(order)的Api接口
4:注意:经过本人测试通过RestTemplate的方式调用,仍然没有效果。
5:当通过Feign调用一次新服务(order)后,hystrix.stream 正常,效果如下:
@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:user服务和oder服务目录结构 和 相关代码
https://gitee.com/gxc6/gxc-springcloud-study
我透... 写完这个已经凌晨3点了... 溜了溜了...
SpringCloud之Hystrix-Dashboard监控,以及踩的坑...
原文:https://www.cnblogs.com/gxc6/p/11407601.html