Skip to content

Commit

Permalink
resilience4j + cloud 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed May 21, 2020
1 parent 6b75d9a commit 7a83a81
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
48 changes: 48 additions & 0 deletions labx-24/labx-24-resilience4j-demo02/pom.xml
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>
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);
}

}
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");
// }

}
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.
1 change: 1 addition & 0 deletions labx-24/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<modules>
<module>labx-24-user-service</module>
<module>labx-24-resilience4j-demo01</module>
<module>labx-24-resilience4j-demo02</module>
</modules>

</project>

0 comments on commit 7a83a81

Please sign in to comment.