提交 73476c30 编写于 作者: Q qinyingjie

fix:添加备注

上级 1a5e52a0
......@@ -35,6 +35,7 @@
<module>springcloud-service-feign</module>
<module>springcloud-service-zuul</module>
<module>springcloud-service-consul</module>
<module>springcloud-service-hystrix-dashboard</module>
<module>springcloud-service-eureka</module>
</modules>
......
......@@ -6,6 +6,16 @@ import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
*
*
* http://localhost:8081/feign/1
*@version : 2.2.0
*@author : qinyingjie
*@date : 2023/1/14 16:13
*/
@EnableCircuitBreaker
@EnableFeignClients
@EnableEurekaClient
......
......@@ -7,7 +7,6 @@ import java.io.Serializable;
@Component
public class ComputeClientHystrix implements ComputeClient {
@Override
public Result selectOne(Serializable id) {
return Result.error("feign方式请求 服务不可用 请求失败");
......
package com.kwan.springcloud.service;
import com.kwan.springcloud.api.ComputeClient;
import com.kwan.springcloud.response.Result;
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 FeignService {
/**
* 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;
private ComputeClient computeClient;
// @HystrixCommand(fallbackMethod = "addServiceFallback")
public Result addService(String id) {
//调用远程的controller
return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody();
}
public Result addServiceFallback(String id) {
log.info("失败id={}", id);
return Result.error("ribbon方式请求 服务不可用 请求失败");
public Result addService(String id) {
return computeClient.selectOne(id);
}
}
......@@ -11,3 +11,7 @@ eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
feign:
hystrix:
enabled: true
\ No newline at end of file
<?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-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-hystrix-dashboard</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>
</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.hystrix.dashboard.EnableHystrixDashboard;
/**
*
* http://localhost:9909/hystrix
*@version : 2.2.0
*@author : qinyingjie
*@date : 2023/1/14 15:33
*/
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
#端口号
server:
port: 9909
spring:
application:
name: hystrix-dashboard-service #服务名称
\ 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 HystrixDashboardApplicationTests {
@Test
void contextLoads() {
}
}
......@@ -29,6 +29,17 @@
<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.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
<build>
......
......@@ -3,8 +3,17 @@ 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.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
*
* http://localhost:8080/portal/hystrix/1
*@version : 2.2.0
*@author : qinyingjie
*@date : 2023/1/14 16:07
*/
@EnableFeignClients
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class PortalApplication {
......
......@@ -24,6 +24,17 @@ 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);
}
/**
* 通过主键查询单条数据
*
......@@ -40,4 +51,6 @@ public class PortalController {
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -9,26 +10,42 @@ 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")
public Result hystrix(String id) {
//1·服务超时,会降级
try {
Thread.sleep(2000);
} 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("请求异常 服务不可用 请求失败");
}
}
......@@ -11,3 +11,15 @@ eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
feign.hystrix.enabled=true
hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMi
1liseconds=5000
如果hystrix.command.default.execution.timeout.enabled为true,则会有两
个执行方法超时的配置,一个就是ribbon的ReadTimeout,一个就是熔断器
I
hystrix的timeoutInMilliseconds,此时谁的值小谁生效;
如果hystrix.command.default.execution.timeout.enabled为false,则熔断
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册