cannot guarantee 100% availability of services. If there is a problem with a single service, there will be a network delay in calling this service. If there is a large influx of network at this time, it will form a cumulative task , leading to service paralysis and even service "avalanche".
To solve this problem, the circuit breaker model appears.
When calls to a specific service reach a threshold (hystric is 20 times in 5 seconds) the circuit breaker will be opened.
That is, when the service is unavailable, it will return to the value set by the circuit breaker, preventing long waits and retries.
When the circuit breaker is opened, the request will be re-allowed to attempt access after a certain period of time (default is 5s).
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
ribbon method:
@SpringBootApplication //spring boot starts the application //@EnableDiscoveryClient //Discovery Service" has multiple implementations, such as: eureka, consul, zookeeper. @EnableEurekaClient //Only for eureka @EnableHystrix // circuit breaker public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
package com.Service; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; //ribbon remote call service @Service("callService") public class CallService { final static Logger logger = LogManager.getLogger(CallService.class); @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "callServiceError") public String callService(String name) { //This just turns the IP and port number into the name of the service (remote service call via HTTP) return restTemplate.getForObject("http://eureka-Client/eurekaClient/Test/test",String.class); } public String callServiceError(String name) { return "hi,"+name+",sorry,error!"; } }
Feign method: (feign has its own circuit breaker)
server.port=8765 #logging.pattern.level=INFO #server path server.context-path=/feign # #eureka.instance.hostname=localHost # Indicates whether to register itself to the eureka server, because the current application is the eureka server, there is no need to register itself, so here is false. #eureka.client.registerWithEureka=false #fetchRegistry indicates whether to obtain registration information from the eureka server, same as above, no need here #eureka.client.fetchRegistry=false #Set the address where the eureka server is located. Both query services and registration services need to rely on this address. #eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eurekaServer/eureka/ spring.application.name=feign #Custom circuit breaker (there is falser on the Internet, but the actual test should use true) feign.hystrix.enabled=true
package com.Service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(value = "eureka-Client",fallback = CallServiceError.class) //Service name public interface CallService { @RequestMapping(value = "/eurekaClient/Test/test",method = RequestMethod.GET) String callService(); }
package com.Service; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @Component public class CallServiceError implements CallService{ @Override public String callService() { return "sorry Error!"; } }
Errata: Some people reported that feign's circuit breaker does not work, the springcloud version problem, use this:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>