forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
May 21, 2020
1 parent
6b75d9a
commit 7a83a81
Showing
6 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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>lab-59</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>labx-24-resilience4j-demo02</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 引入 Spring Cloud CircuitBreaker Resilience4j 相关依赖,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId> | ||
<version>1.0.2.RELEASE</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
20 changes: 20 additions & 0 deletions
20
...-demo02/src/main/java/cn/iocoder/springcloud/labx24/resilience4jdemo/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cn.iocoder.springcloud.labx24.resilience4jdemo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@SpringBootApplication | ||
public class DemoApplication { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DemoApplication.class, args); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...c/main/java/cn/iocoder/springcloud/labx24/resilience4jdemo/config/Resilience4jConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cn.iocoder.springcloud.labx24.resilience4jdemo.config; | ||
|
||
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; | ||
import io.github.resilience4j.timelimiter.TimeLimiterConfig; | ||
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory; | ||
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder; | ||
import org.springframework.cloud.client.circuitbreaker.Customizer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.time.Duration; | ||
|
||
@Configuration | ||
public class Resilience4jConfig { | ||
|
||
@Bean | ||
public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() { | ||
return factory -> factory.configureDefault( | ||
id -> new Resilience4JConfigBuilder(id) | ||
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build()) | ||
.circuitBreakerConfig(CircuitBreakerConfig.custom().slidingWindow(5, 5, CircuitBreakerConfig.SlidingWindowType.COUNT_BASED).build()) | ||
.build()); | ||
} | ||
|
||
// @Bean | ||
// public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() { | ||
// return factory -> factory.configureDefault( | ||
// id -> new Resilience4JConfigBuilder(id) | ||
// .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build()) | ||
// .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults()) | ||
// .build()); | ||
// } | ||
|
||
// @Bean | ||
// public Customizer<Resilience4JCircuitBreakerFactory> slowCustomizer() { | ||
// return factory -> factory.configure(builder -> builder | ||
// .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(2)).build()) | ||
// .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults()), | ||
// "slow"); | ||
// } | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...c/main/java/cn/iocoder/springcloud/labx24/resilience4jdemo/controller/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cn.iocoder.springcloud.labx24.resilience4jdemo.controller; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
@RestController | ||
@RequestMapping("/demo") | ||
public class DemoController { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
@Autowired | ||
private CircuitBreakerFactory circuitBreakerFactory; | ||
|
||
@GetMapping("/get_user") | ||
public String getUser(@RequestParam("id") Integer id) { | ||
return circuitBreakerFactory.create("backendA").run(new Supplier<String>() { | ||
|
||
@Override | ||
public String get() { | ||
logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id); | ||
return restTemplate.getForEntity("http://127.0.0.1:18080/user/get?id=" + id, String.class).getBody(); | ||
} | ||
|
||
}, new Function<Throwable, String>() { | ||
|
||
@Override | ||
public String apply(Throwable throwable) { | ||
logger.info("[getUserFallback][id({}) exception({})]", id, throwable.getClass().getSimpleName()); | ||
return "mock:User:" + id; | ||
} | ||
|
||
}); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters