This is the 11th day of my participation in the Gwen Challenge.More article challenges
This paper is based on Hystrix+ Ribbon circuit breaker mode
Environment:
IDEA
JDK18.
Spring Cloud Hoxton.M3
Spring Boot 2.2. 0
Copy the code
I. Introduction to Hystrix
Background: In the microservices architecture, services are divided into one service after another according to the business. Services can call each other (RPC). In Spring Cloud, services can be called with RestTemplate+Ribbon and Feign. To ensure high availability, individual services are typically deployed in clusters. Due to network reasons or its own reasons, the service cannot guarantee 100% availability. If a single service has problems, the invocation of this service will be blocked. At this time, if a large number of requests flood in, the thread resources of the Servlet container will be consumed and the service will break down.
Solution: Netflix open-source the Hystrix component to implement the circuit breaker mode, and SpringCloud integrates it. In a microservice architecture, it is very common for a single request to invoke multiple services, which can lead to cascading failures if the lower level services fail. When the unavailability of calls to a particular service reaches a threshold (Hystric is 20 calls in 5 seconds) the circuit breaker will be opened. The fallback method directly returns a fixed value to ensure that the system is still available if one or more dependencies fail at the same time.
2. Create a project
1. File —– New —–Project
2. Spring Initializr —– Next
3. Waiting to create the project
4. Enter groups and artifacts
Click Next
Choose Hystric
6. Click Finish
Third, improve the project
Pom. XML adds dependencies
<! -- spring-cloud-starter-netflix-hystrix --><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
Copy the code
2. HystrixserverApplication add @enablehystrix to EnableHystrix and create the RestTemplate to enable client load balancing.
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class HystrixserverApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixserverApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return newRestTemplate(); }}Copy the code
3. Configuration files
server.port=8765Spring. The application. The name = # RibbonServer eureka. Registration to the service center address client. ServiceUrl. DefaultZone = HTTP://localhost:8761/eureka/Whether # had been registered to the server, eureka. Client. RegisterWithEureka =true# whether obtain registration information from eureka server eureka. Client. FetchRegistry =true# Whether to enable the self-protection mode. The default value istrue. eureka.server.enable-self-preservation=true
Copy the code
4, the Controller
@RestController
public class HystrixController {
@Autowired
private HystrixService hystrixService;
@RequestMapping("/gethystrix")
public String getHystrix(String name){
returnhystrixService.getHystrix(name); }}Copy the code
5, the Service
public interface HystrixService {
String getHystrix(String name);
}
Copy the code
6. Serviceimpl needs to inject the RestTemplate
Serviceimpl needs to inject the RestTemplate, add @hystrixCommand (fallbackMethod =”fallback”) to the method, specify fallbackMethod, write fallbackMethod, Set the return information. HystrixCommand This annotation creates a fuse function for the method.
@Service
public class HystrixServiceImpl implements HystrixService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod ="fallback")
@Override
public String getHystrix(String name) {
String result = restTemplate.getForObject("http://Client-Server1/test? name=" + name, String.class);
return result;
}
public String fallback(String name){
return "hi,"+name+",sorry,Hystrix Ribbon error!"; }}Copy the code
Iv. Running projects
1. Enter the project address and parameters
http://localhost:8765/gethystrix?name=123
In the case that both client-server1 and client-server2 projects are running properly, load balancing is implemented, and the following result is returned
2. Port 8762 and 8763 switch back and forth to achieve load balancing.
Now close client-server1 and client-server2 to access the interface again, return the interface as follows, enter the fallback method, achieve short circuit, reduce the thread blocking problem caused by abnormal connection, avoid the waste of container threads.
The above is a learning process of Spring Cloud realizing circuit breaker mode based on Hystrix+ Ribbon. Welcome to exchange and study.