Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Lcry
SpringCloud2020
提交
2200a977
S
SpringCloud2020
项目概览
Lcry
/
SpringCloud2020
通知
20
Star
2
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringCloud2020
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2200a977
编写于
3月 15, 2020
作者:
Lcry
🖥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
P47-P64:完成服务降级和服务熔断Hystrix,更新Readme
上级
61ec0acf
变更
18
显示空白变更内容
内联
并排
Showing
18 changed file
with
549 addition
and
13 deletion
+549
-13
.gitignore
.gitignore
+6
-0
README.md
README.md
+17
-13
cloud-consumer-feign-hystrix-order80/pom.xml
cloud-consumer-feign-hystrix-order80/pom.xml
+65
-0
cloud-consumer-feign-hystrix-order80/src/main/java/com/lcry/springcloud/HystrixOrderMain80.java
...rc/main/java/com/lcry/springcloud/HystrixOrderMain80.java
+18
-0
cloud-consumer-feign-hystrix-order80/src/main/java/com/lcry/springcloud/controller/OrderHystirxController.java
...m/lcry/springcloud/controller/OrderHystirxController.java
+52
-0
cloud-consumer-feign-hystrix-order80/src/main/java/com/lcry/springcloud/service/PaymentFallbackService.java
.../com/lcry/springcloud/service/PaymentFallbackService.java
+19
-0
cloud-consumer-feign-hystrix-order80/src/main/java/com/lcry/springcloud/service/PaymentHystrixService.java
...a/com/lcry/springcloud/service/PaymentHystrixService.java
+20
-0
cloud-consumer-feign-hystrix-order80/src/main/resources/application.yml
...-feign-hystrix-order80/src/main/resources/application.yml
+11
-0
cloud-consumer-hystrix-dashboard9001/pom.xml
cloud-consumer-hystrix-dashboard9001/pom.xml
+50
-0
cloud-consumer-hystrix-dashboard9001/src/main/java/com/lcry/springcloud/HystrixDashboardMain9001.java
...n/java/com/lcry/springcloud/HystrixDashboardMain9001.java
+19
-0
cloud-consumer-hystrix-dashboard9001/src/main/resources/application.yml
...-hystrix-dashboard9001/src/main/resources/application.yml
+2
-0
cloud-provider-hystrix-payment8001/pom.xml
cloud-provider-hystrix-payment8001/pom.xml
+58
-0
cloud-provider-hystrix-payment8001/src/main/java/com/lcry/springcloud/HystrixPaymentMain8001.java
...ain/java/com/lcry/springcloud/HystrixPaymentMain8001.java
+43
-0
cloud-provider-hystrix-payment8001/src/main/java/com/lcry/springcloud/controller/PaymentController.java
...va/com/lcry/springcloud/controller/PaymentController.java
+48
-0
cloud-provider-hystrix-payment8001/src/main/java/com/lcry/springcloud/service/PaymentService.java
...ain/java/com/lcry/springcloud/service/PaymentService.java
+17
-0
cloud-provider-hystrix-payment8001/src/main/java/com/lcry/springcloud/service/impl/PaymentServiceImpl.java
...com/lcry/springcloud/service/impl/PaymentServiceImpl.java
+87
-0
cloud-provider-hystrix-payment8001/src/main/resources/application.yml
...er-hystrix-payment8001/src/main/resources/application.yml
+14
-0
pom.xml
pom.xml
+3
-0
未找到文件。
.gitignore
浏览文件 @
2200a977
...
...
@@ -119,3 +119,9 @@ cloud-provider-consul-payment8006/target/
cloud-provider-consul-payment8006/src/test/
cloud-consumer-feign-order80/target/
cloud-consumer-feign-order80/src/test/
cloud-provider-hystrix-payment8001/src/test
cloud-provider-hystrix-payment8001/target/
cloud-consumer-feign-hystrix-order80/src/test/
cloud-consumer-feign-hystrix-order80/target/
cloud-consumer-hystrix-dashboard9001/src/test/
cloud-consumer-hystrix-dashboard9001/target/
\ No newline at end of file
README.md
浏览文件 @
2200a977
...
...
@@ -15,10 +15,10 @@ MySQL:5.7及以上
#### 项目结构
| 项目名 | 用途 | 所用技术 |
| ---------------------------------
| :--------------------------------- |
----------------------------------------------- |
| ---------------------------------
--- | :---------------------------------------------- | -
----------------------------------------------- |
| cloud-api-commons | 公共代码部分(返回结果集、实体等) |
\
|
| cloud-consumer-consul-order80 | 消费方通过Consul调用服务方 | 注册中心(Consul)、远程调用(RestTemplate) |
| cloud-consumer-feign-order80
| 消费方通过feign远程调用服务方 | 服务调用(openfeign)、注册中心(EurekaClient)
|
| cloud-consumer-feign-order80
| 消费方通过feign远程调用服务方 | 服务调用(Openfeign)、注册中心(EurekaClient)
|
| cloud-consumer-order80 | 消费方通过Eureka调用服务方等示例 | 注册中心(Eureka)、远程调用(RestTemplate) |
| cloud-consumer-zk-order80 | 消费方通过Zk调用服务方 | 注册中心(Zk)、远程调用(RestTemplate) |
| cloud-eureka-server7001 | Eureka服务端1 | 注册中心(EurekaServer) |
...
...
@@ -27,6 +27,10 @@ MySQL:5.7及以上
| cloud-provider-payment8001 | 服务方通过Eureka注册服务 | 注册中心(Euraka) |
| cloud-provider-payment8002 | 服务方通过Eureka注册服务 | 注册中心(Euraka) |
| cloud-provider-zk-payment8004 | 服务方通过Zk注册服务 | 注册中心(Zk) |
| cloud-provider-hystrix-payment8001 | 服务方通过Hystrix服务降级和熔断 | 服务降级和熔断(Hystrix)、服务监控(Actuator) |
| cloud-consumer-feign-hystrix-order80 | 消费方消费方通过feign远程调用服务方带降级和服务 | 服务降级和熔断(Hystrix)、服务调用(Openfeign) |
| cloud-consumer-hystrix-dashboard9001 | Hystrix监控仪表盘 | 服务监控(Hystrix) |
...
...
cloud-consumer-feign-hystrix-order80/pom.xml
0 → 100644
浏览文件 @
2200a977
<?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"
>
<parent>
<artifactId>
2020SpringCloud
</artifactId>
<groupId>
com.lcry
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<!-- 消费方feign调用+hystrix服务降级案例-->
<artifactId>
cloud-consumer-feign-hystrix-order80
</artifactId>
<dependencies>
<!--openfeign-->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-openfeign
</artifactId>
</dependency>
<!--hystrix-->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-hystrix
</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
<!--引入自己定义的api调用包,可以使用Payment支付Entity-->
<dependency>
<groupId>
com.lcry
</groupId>
<artifactId>
cloud-api-commons
</artifactId>
<version>
${project.version}
</version>
</dependency>
<!--web-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
<!--一般基础通用配置-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
<scope>
runtime
</scope>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
cloud-consumer-feign-hystrix-order80/src/main/java/com/lcry/springcloud/HystrixOrderMain80.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.springcloud
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.netflix.hystrix.EnableHystrix
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
/**
* 2020年3月15日 12:12:25
*/
@SpringBootApplication
@EnableFeignClients
//启用OpenFeign
@EnableHystrix
//客户端启用Hystrix服务降级注解
public
class
HystrixOrderMain80
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
HystrixOrderMain80
.
class
,
args
);
}
}
cloud-consumer-feign-hystrix-order80/src/main/java/com/lcry/springcloud/controller/OrderHystirxController.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.springcloud.controller
;
import
com.lcry.springcloud.service.PaymentHystrixService
;
import
com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.annotation.Resource
;
/**
* @auther zzyy
* @create 2020-02-20 11:57
*/
@RestController
@Slf4j
//定义全局FallbackMethod
@DefaultProperties
(
defaultFallback
=
"payment_Global_FallbackMethod"
)
public
class
OrderHystirxController
{
@Resource
private
PaymentHystrixService
paymentHystrixService
;
@GetMapping
(
"/consumer/payment/hystrix/ok/{id}"
)
public
String
paymentInfo_OK
(
@PathVariable
(
"id"
)
Integer
id
)
{
String
result
=
paymentHystrixService
.
paymentInfo_OK
(
id
);
return
result
;
}
@GetMapping
(
"/consumer/payment/hystrix/timeout/{id}"
)
@HystrixCommand
(
fallbackMethod
=
"paymentTimeOutFallbackMethod"
,
commandProperties
=
{
//超时1.5s或者异常直接降级
@HystrixProperty
(
name
=
"execution.isolation.thread.timeoutInMilliseconds"
,
value
=
"1500"
)
})
//@HystrixCommand //如果没有指定走全局
public
String
paymentInfo_TimeOut
(
@PathVariable
(
"id"
)
Integer
id
)
{
int
age
=
10
/
0
;
String
result
=
paymentHystrixService
.
paymentInfo_TimeOut
(
id
);
return
result
;
}
public
String
paymentTimeOutFallbackMethod
(
@PathVariable
(
"id"
)
Integer
id
)
{
return
"我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o"
;
}
// 下面是全局fallback方法
public
String
payment_Global_FallbackMethod
()
{
return
"Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~"
;
}
}
cloud-consumer-feign-hystrix-order80/src/main/java/com/lcry/springcloud/service/PaymentFallbackService.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.springcloud.service
;
import
org.springframework.stereotype.Component
;
/**
* 根据Openfeign服务接口直接降级
*/
@Component
public
class
PaymentFallbackService
implements
PaymentHystrixService
{
@Override
public
String
paymentInfo_OK
(
Integer
id
)
{
return
"-----PaymentFallbackService fall back-paymentInfo_OK ,o(╥﹏╥)o"
;
}
@Override
public
String
paymentInfo_TimeOut
(
Integer
id
)
{
return
"-----PaymentFallbackService fall back-paymentInfo_TimeOut ,o(╥﹏╥)o"
;
}
}
cloud-consumer-feign-hystrix-order80/src/main/java/com/lcry/springcloud/service/PaymentHystrixService.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.springcloud.service
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
/**
* Openfeign调用服务方
*/
@Component
@FeignClient
(
value
=
"CLOUD-PROVIDER-HYSTRIX-PAYMENT"
,
fallback
=
PaymentFallbackService
.
class
)
public
interface
PaymentHystrixService
{
@GetMapping
(
"/payment/hystrix/ok/{id}"
)
public
String
paymentInfo_OK
(
@PathVariable
(
"id"
)
Integer
id
);
@GetMapping
(
"/payment/hystrix/timeout/{id}"
)
public
String
paymentInfo_TimeOut
(
@PathVariable
(
"id"
)
Integer
id
);
}
cloud-consumer-feign-hystrix-order80/src/main/resources/application.yml
0 → 100644
浏览文件 @
2200a977
server
:
port
:
80
eureka
:
client
:
register-with-eureka
:
false
service-url
:
defaultZone
:
http://127.0.0.1:7001/eureka/
feign
:
hystrix
:
enabled
:
true
\ No newline at end of file
cloud-consumer-hystrix-dashboard9001/pom.xml
0 → 100644
浏览文件 @
2200a977
<?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"
>
<parent>
<artifactId>
2020SpringCloud
</artifactId>
<groupId>
com.lcry
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<!-- hystrix服务监控平台dashboard -->
<!-- 被监控服务一定要引入web和actuator两个依赖 -->
<!--
监控地址:http://ip:端口/hystrix
被监控填写:http://ip:端口/hystrix.stream
-->
<artifactId>
cloud-consumer-hystrix-dashboard9001
</artifactId>
<dependencies>
<!-- hystrix-dashboard -->
<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-actuator
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
<scope>
runtime
</scope>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
cloud-consumer-hystrix-dashboard9001/src/main/java/com/lcry/springcloud/HystrixDashboardMain9001.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.springcloud
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard
;
/**
* HystrixDashboardMain9001
*
* @author lcry
* @date 2020/03/15 13:46
*/
@SpringBootApplication
@EnableHystrixDashboard
//启用注解
public
class
HystrixDashboardMain9001
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
HystrixDashboardMain9001
.
class
,
args
);
}
}
cloud-consumer-hystrix-dashboard9001/src/main/resources/application.yml
0 → 100644
浏览文件 @
2200a977
server
:
port
:
9001
\ No newline at end of file
cloud-provider-hystrix-payment8001/pom.xml
0 → 100644
浏览文件 @
2200a977
<?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"
>
<parent>
<artifactId>
2020SpringCloud
</artifactId>
<groupId>
com.lcry
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<!-- 服务熔断hystrix版实现服务方-->
<artifactId>
cloud-provider-hystrix-payment8001
</artifactId>
<dependencies>
<!--hystrix-->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-hystrix
</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
<!--引入自己定义的api调用包,可以使用Payment支付Entity-->
<dependency>
<groupId>
com.lcry
</groupId>
<artifactId>
cloud-api-commons
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
<scope>
runtime
</scope>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
cloud-provider-hystrix-payment8001/src/main/java/com/lcry/springcloud/HystrixPaymentMain8001.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.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.context.annotation.Bean
;
/**
* HystrixPaymentMain8001
*
* @author lcry
* @date 2020/03/15 0:01
*/
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
//服务端开启Hystrix或者使用@EnableCircuitBreaker
public
class
HystrixPaymentMain8001
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
HystrixPaymentMain8001
.
class
,
args
);
}
// 针对监控平台需要下面配置 监控/hystrix.stream
/**
* 此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
* ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
* 只要在自己的项目里配置上下面的servlet就可以了
*/
@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
;
}
}
cloud-provider-hystrix-payment8001/src/main/java/com/lcry/springcloud/controller/PaymentController.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.springcloud.controller
;
import
com.lcry.springcloud.service.PaymentService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.annotation.Resource
;
/**
* PaymentController
*
* @author lcry
* @date 2020/03/15 0:06
*/
@RestController
@Slf4j
public
class
PaymentController
{
@Resource
private
PaymentService
paymentService
;
@Value
(
"${server.port}"
)
private
String
serverPort
;
@GetMapping
(
"/payment/hystrix/ok/{id}"
)
public
String
paymentInfo_OK
(
@PathVariable
(
"id"
)
Integer
id
)
{
String
result
=
paymentService
.
paymentInfo_OK
(
id
);
log
.
info
(
String
.
format
(
"result:{%s}"
,
result
));
return
serverPort
+
result
;
}
@GetMapping
(
"/payment/hystrix/timeout/{id}"
)
public
String
paymentInfo_TimeOut
(
@PathVariable
(
"id"
)
Integer
id
)
{
String
result
=
paymentService
.
paymentInfo_TimeOut
(
id
);
log
.
info
(
String
.
format
(
"result:{%s}"
,
result
));
return
serverPort
+
result
;
}
//服务熔断
@GetMapping
(
"/payment/circuit/{id}"
)
public
String
paymentCircuitBreaker
(
@PathVariable
(
"id"
)
Integer
id
)
{
String
result
=
paymentService
.
paymentCircuitBreaker
(
id
);
log
.
info
(
String
.
format
(
"result:{%s}"
,
result
));
return
result
;
}
}
cloud-provider-hystrix-payment8001/src/main/java/com/lcry/springcloud/service/PaymentService.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.springcloud.service
;
import
org.springframework.web.bind.annotation.PathVariable
;
/**
* PaymentService
*
* @author lcry
* @date 2020/03/15 0:02
*/
public
interface
PaymentService
{
public
String
paymentInfo_OK
(
Integer
id
);
public
String
paymentInfo_TimeOut
(
Integer
id
);
public
String
paymentCircuitBreaker
(
@PathVariable
(
"id"
)
Integer
id
);
}
cloud-provider-hystrix-payment8001/src/main/java/com/lcry/springcloud/service/impl/PaymentServiceImpl.java
0 → 100644
浏览文件 @
2200a977
package
com.lcry.springcloud.service.impl
;
import
cn.hutool.core.util.IdUtil
;
import
com.lcry.springcloud.service.PaymentService
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
java.util.concurrent.TimeUnit
;
/**
* PaymentServiceImpl
*
* @author lcry
* @date 2020/03/15 0:03
*/
@Service
public
class
PaymentServiceImpl
implements
PaymentService
{
// 服务降级
/**
* 正常访问OK
*
* @param id
* @return
*/
@Override
public
String
paymentInfo_OK
(
Integer
id
)
{
return
"线程池: "
+
Thread
.
currentThread
().
getName
()
+
" paymentInfo_OK,id: "
+
id
+
"\t"
+
"O(∩_∩)O哈哈~"
;
}
/**
* 模拟超时3s
*
* @param id
* @return
*/
@Override
@HystrixCommand
(
fallbackMethod
=
"paymentInfo_TimeOutHandler"
,
commandProperties
=
{
//超时5s或者异常直接降级
@HystrixProperty
(
name
=
"execution.isolation.thread.timeoutInMilliseconds"
,
value
=
"5000"
)
})
public
String
paymentInfo_TimeOut
(
Integer
id
)
{
//int age = 10/0;
int
timenum
=
3
;
try
{
TimeUnit
.
SECONDS
.
sleep
(
timenum
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
return
"线程池: "
+
Thread
.
currentThread
().
getName
()
+
" id: "
+
id
+
"\t"
+
"O(∩_∩)O哈哈~"
+
" 耗时(秒): "
+
timenum
;
}
public
String
paymentInfo_TimeOutHandler
(
Integer
id
)
{
return
"线程池: "
+
Thread
.
currentThread
().
getName
()
+
" 8001系统繁忙或者运行报错,请稍后再试,id: "
+
id
+
"\t"
+
"o(╥﹏╥)o"
;
}
/**
* 以下内容属于服务熔断
*
* @param id
* @return
*/
@Override
@HystrixCommand
(
fallbackMethod
=
"paymentCircuitBreaker_fallback"
,
commandProperties
=
{
@HystrixProperty
(
name
=
"circuitBreaker.enabled"
,
value
=
"true"
),
// 是否开启断路器
@HystrixProperty
(
name
=
"circuitBreaker.requestVolumeThreshold"
,
value
=
"10"
),
// 请求次数
@HystrixProperty
(
name
=
"circuitBreaker.sleepWindowInMilliseconds"
,
value
=
"10000"
),
// 时间窗口期
@HystrixProperty
(
name
=
"circuitBreaker.errorThresholdPercentage"
,
value
=
"60"
),
// 失败率达到多少后跳闸
})
public
String
paymentCircuitBreaker
(
@PathVariable
(
"id"
)
Integer
id
)
{
if
(
id
<
0
)
{
throw
new
RuntimeException
(
"id 不能负数"
);
}
String
serialNumber
=
IdUtil
.
simpleUUID
();
return
Thread
.
currentThread
().
getName
()
+
"\t"
+
"调用成功,流水号: "
+
serialNumber
;
}
public
String
paymentCircuitBreaker_fallback
(
@PathVariable
(
"id"
)
Integer
id
)
{
return
"id 不能负数,请稍后再试,/(ㄒoㄒ)/~~ id: "
+
id
;
}
}
cloud-provider-hystrix-payment8001/src/main/resources/application.yml
0 → 100644
浏览文件 @
2200a977
server
:
port
:
8001
spring
:
application
:
name
:
cloud-provider-hystrix-payment
eureka
:
client
:
register-with-eureka
:
true
fetch-registry
:
true
service-url
:
defaultZone
:
http://127.0.0.1:7001/eureka
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
\ No newline at end of file
pom.xml
浏览文件 @
2200a977
...
...
@@ -19,6 +19,9 @@
<module>
cloud-provider-consul-payment8006
</module>
<module>
cloud-consumer-consul-order80
</module>
<module>
cloud-consumer-feign-order80
</module>
<module>
cloud-provider-hystrix-payment8001
</module>
<module>
cloud-consumer-feign-hystrix-order80
</module>
<module>
cloud-consumer-hystrix-dashboard9001
</module>
</modules>
<packaging>
pom
</packaging>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录