提交 ef45b284 编写于 作者: Q qinyingjie

fix:turbine

上级 e84c3b59
...@@ -29,13 +29,15 @@ ...@@ -29,13 +29,15 @@
<module>springcloud-service-goods-9200</module> <module>springcloud-service-goods-9200</module>
<module>springcloud-service-goods-9300</module> <module>springcloud-service-goods-9300</module>
<module>springcloud-service-common</module> <module>springcloud-service-common</module>
<module>springcloud-service-portal</module> <module>springcloud-service-portal-8080</module>
<module>springcloud-service-portal-8081</module>
<module>springcloud-service-config</module> <module>springcloud-service-config</module>
<module>springcloud-service-config-client</module> <module>springcloud-service-config-client</module>
<module>springcloud-service-feign</module> <module>springcloud-service-feign</module>
<module>springcloud-service-zuul</module> <module>springcloud-service-zuul</module>
<module>springcloud-service-consul</module> <module>springcloud-service-consul</module>
<module>springcloud-service-hystrix-dashboard</module> <module>springcloud-service-hystrix-dashboard</module>
<module>springcloud-service-hystrix-turbine</module>
<module>springcloud-service-eureka</module> <module>springcloud-service-eureka</module>
</modules> </modules>
......
<?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>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>springcloud-service-hystrix-turbine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-hystrix-turbine</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--springboot开发自动热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</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-turbine</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.kwan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
/**
* http://localhost:9999/hystrix
* <p>
* <p>
* <p>
* http://localhost:9999/turbine.stream
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/14 15:33
*/
@EnableTurbine
@EnableEurekaClient
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication.class, args);
}
}
#端口号
server:
port: 9999
spring:
application:
name: hystrix-turbine-service #服务名称
#服务提供者
eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
instance:
lease-renewal-interval-in-seconds: 2 #每间隔2s,向服务端发送一次心跳,证明自己依然"存活”
lease-expiration-duration-in-seconds: 10 #告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我踢出掉
prefer-ip-address: true #告诉服务端,服务实例以IP作为链接,而不是取机器名
instance-id: springcloud-service-turbine-9999 #告诉服务端,服务实例的id,id要是唯一的
# 开启Feign对Hystrix的支持
feign:
hystrix:
enabled: true
client:
config:
default:
connectTimeout: 5000 # 指定Feign连接提供者的超时时限
readTimeout: 5000 # 指定Feign从请求到获取提供者响应的超时时限
# 开启actuator的所有web终端
management:
endpoints:
web:
exposure:
include: "*"
# 设置服务熔断时限
hystrix:
dashboard:
proxy-stream-allow-list: "localhost"
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
turbine:
app-config: portal-service
cluster-name-expression: new String("default")
combine-host-port: true
instanceUrlSuffix: /actuator/hystrix.stream #turbine默认监控actuator/路径下的端点,修改直接监控hystrix.stream
#cluster-name-expression: metadata['cluster']
#aggregator:
# cluster-config: ribbon
#instanceUrlSuffix: /hystrix.stream #turbine默认监控actuator/路径下的端点,修改直接监控hystrix.stream
\ No newline at end of file
package com.kwan.springcloud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HystrixTurbineApplicationTests {
@Test
void contextLoads() {
}
}
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
<artifactId>springcloud-service-parent</artifactId> <artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
</parent> </parent>
<artifactId>springcloud-service-portal</artifactId> <artifactId>springcloud-service-portal-8080</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-portal</name> <name>springcloud-service-portal-8080</name>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
</properties> </properties>
......
...@@ -20,11 +20,11 @@ import org.springframework.context.annotation.Bean; ...@@ -20,11 +20,11 @@ import org.springframework.context.annotation.Bean;
@EnableHystrix @EnableHystrix
@EnableEurekaClient @EnableEurekaClient
@SpringBootApplication @SpringBootApplication
public class PortalApplication { public class PortalApplication8080 {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(PortalApplication.class, args); SpringApplication.run(PortalApplication8080.class, args);
} }
@Bean @Bean
......
...@@ -11,6 +11,11 @@ eureka: ...@@ -11,6 +11,11 @@ eureka:
client: client:
service-url: service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
instance:
lease-renewal-interval-in-seconds: 2 #每间隔2s,向服务端发送一次心跳,证明自己依然"存活”
lease-expiration-duration-in-seconds: 10 #告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我踢出掉
prefer-ip-address: true #告诉服务端,服务实例以IP作为链接,而不是取机器名
instance-id: springcloud-service-portal-8080 #告诉服务端,服务实例的id,id要是唯一的
#开启feign开始hystrix的支持 #开启feign开始hystrix的支持
feign: feign:
......
<?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>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>springcloud-service-portal-8081</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-portal-8081</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-common</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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-openfeign-core</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.kwan.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
/**
*
* http://localhost:8081/portal/hystrix/1
*@version : 2.2.0
*@author : qinyingjie
*@date : 2023/1/14 16:07
*/
@EnableFeignClients
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class PortalApplication8081 {
public static void main(String[] args) {
SpringApplication.run(PortalApplication8081.class, args);
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
package com.kwan.springcloud.controller;
import com.kwan.springcloud.response.Result;
import com.kwan.springcloud.service.PortalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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;
/**
* portal控制器
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/8 11:12
*/
@RestController
@RequestMapping("/portal")
public class PortalController {
@Autowired
private PortalService portalService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping(value = "/hystrix/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result hystrix(@PathVariable String id) {
return portalService.hystrix(id);
}
@GetMapping(value = "/limit/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result limit(@PathVariable String id) {
return portalService.limit(id);
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping(value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result selectOne(@PathVariable String id) {
return portalService.addService(id);
}
@GetMapping(value = "/test", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result test() {
return Result.ok("portal 访问成功");
}
}
\ No newline at end of file
package com.kwan.springcloud.service;
import com.kwan.springcloud.response.Result;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Slf4j
@Service
public class PortalService {
/**
* http直接访问
*/
private static final String GOODS_URL = "http://localhost:9100/kwanGoodsInfo/";
/**
* eureka访问
*/
private static final String GOODS_URL_2 = "http://goods-service/kwanGoodsInfo/";
@Autowired
RestTemplate restTemplate;
public Result addService(String id) {
//调用远程的controller
return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody();
}
@HystrixCommand(
fallbackMethod = "addServiceFallback",
commandProperties = {
@HystrixProperty(name = "execution.timeout.enabled", value = "true"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "6000")}
)
public Result hystrix(String id) {
//1·服务超时,会降级
// try {
// Thread.sleep(6000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// //2.服务异常,会降级
// String str = null;
// if (str == null) {
// throw new RuntimeException("服务异常了.");
// }
return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody();
}
public Result addServiceFallback(String id) {
log.info("失败id={}", id);
// return Result.error("请求超时 服务不可用 请求失败");
return Result.error("请求异常 服务不可用 请求失败");
}
@HystrixCommand(
fallbackMethod = "addServiceFallback",
threadPoolKey = "goods",
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "2"),
@HystrixProperty(name = "maxQueueSize", value = "1"),
@HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "100")
}
)
public Result limit(String id) {
return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody();
}
}
#端口号
server:
port: 8081
spring:
application:
name: portal-service #服务名称
#服务提供者
eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
instance:
lease-renewal-interval-in-seconds: 2 #每间隔2s,向服务端发送一次心跳,证明自己依然"存活”
lease-expiration-duration-in-seconds: 10 #告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我踢出掉
prefer-ip-address: true #告诉服务端,服务实例以IP作为链接,而不是取机器名
instance-id: springcloud-service-portal-8081 #告诉服务端,服务实例的id,id要是唯一的
#开启feign开始hystrix的支持
feign:
hystrix:
enabled: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000 #hystrix超时时间
timeout:
enabled: true #开启hystrix超时管理
ribbon:
ReadTimeout: 2000 #请求超时时间
http:
client:
enabled: true #开启ribbon超时管理
ConnectTimeout: 2000 #连接超时时间
management:
endpoints:
web:
exposure:
include: "*"
\ No newline at end of file
package com.kwan.springcloud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PortalApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册