Source code for this article: GitHub. Click here || GitEE. Click here
1. Introduction to Fuses
The architecture of micro-services is characterized by multiple services, multiple data sources, and supporting system applications.This leads to dependencies between micro-services.If one of these services fails, it may cause the system to shut down, which is called avalanche effect.
1. Service Rupture
When a microservice in a microservice architecture fails, it is necessary to shut down the service quickly, prompt the user, make subsequent requests, return without invoking the service, and release resources. This is the service crash.
When the fuse takes effect, a request is invoked after a specified time to test whether the dependency is restored, and the fuse is closed after the dependent application is restored.
2. Service Demotion
When the server is under high concurrency and the pressure increases sharply, some services and pages are strategically downgraded (which can be interpreted as turning off unnecessary services) based on business conditions and traffic to alleviate the pressure on server resources to keep core tasks running properly.
During the 11/11 period, Alipay will prompt for many functions, [during the 11/11 period, guarantee core transactions, delay in some service data].
3. Core Dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
4. Core Notes
- @EnableHystrix starts the class annotation control fuse function.
- @HystrixCommand method annotation, fuse control configuration.
5. Case Module Description
Demonstrating the melting of Ribbon-based services node03-consume-8001 Demonstrating the melting of a Feign-based service node03-consume-8002 Eureka Registry node03-eureka-7001 Two service providers node03-provider-6001 node03-provider-6002
2. Ribbon-based service melting down
1. Fuse Execution Method
/** * Service Fuse Call Method */ public String getDefaultInfo (){ return "Service is broken" ; }
2. Simple cases
/** * Simple configuration */ @RequestMapping("/showInfo1") @HystrixCommand(fallbackMethod = "getDefaultInfo") public String showInfo1 (){ return restTemplate.getForObject(server_name+"/getInfo",String.class) ; }
The default time-out for Hystrix is 1 second, and when the time-out period responds, a melt is executed and the fallback program is entered.Because of Spring's lazy loading mechanism, first requests tend to be slow and can be resolved by configuring Hystrix timeout.
3. Complex Cases
- Configuration timeout, concurrency, thread pool, specified exception fuse ignore
/** * Complex Configuration */ @RequestMapping("/showInfo2") @HystrixCommand( fallbackMethod = "getDefaultInfo", commandProperties={ // Downgrade Processing Timeout Settings @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"), // The maximum number of concurrencies allowed at any point in time.After exceeding this setting, the request is rejected. @HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "1000"), }, // Configuration Execution Line City Pool threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "20"), @HystrixProperty(name = "maxQueueSize", value = "-1"), }, // The exception does not execute the melt to execute its own logic thrown by the exception ignoreExceptions = {ServiceException.class} ) public String showInfo2 (){ String value = "" ; // Test configuration exception does not break // Response: {"code":500,"msg": "run exception"} if (value.equals("")){ throw new ServiceException("Run Exception") ; } // The anomaly was fused // if (value.equals("")){ // throw new RuntimeException("throw error"); // } return restTemplate.getForObject(server_name+"/getInfo",String.class) ; }
4. Startup class annotations
- @EnableHystrix
3. Fusion based on Feign service
1. Jar package description
By looking at the Fegin-dependent JARs, Fegin's Jar contains the Jar packages Hystrix needs, and there is no need to import the dependencies again.
2. Fuse configuration
Feign implements declarative Rest requests with interfaces, so the configuration is on top of the interfaces.
1), Interface Code
@FeignClient(value = "NODE02-PROVIDER",fallback = FallbackService.class) public interface GetAuthorService { @RequestMapping(value = "/getAuthorInfo/{authorId}",method = RequestMethod.GET) String getAuthorInfo (@PathVariable("authorId") String authorId) ; }
2) Fuse Execution Code
@Component public class FallbackService implements GetAuthorService { @Override public String getAuthorInfo(String authorId) { return "Service is broken"+authorId; } }
3), Profile
- Turn on the fuse function
feign: hystrix: enabled: true
3. Service class annotations
Since the interface and fuse code above are in different Jar modules, scan in the boot class @SpringBootApplication comment as follows.
@SpringBootApplication(scanBasePackages = {"cloud.node02.consume","cloud.block.code.service"}) @EnableEurekaClient // This service will be automatically registered with the eureka service after it is started @EnableDiscoveryClient // The basePackages attribute needs to be added because the package name paths are different @EnableFeignClients(basePackages={"cloud.block.code.service"}) public class Application_8002 { public static void main(String[] args) { SpringApplication.run(Application_8002.class,args) ; } }
4. Source Code Description
GitHub Address: Know a smile https://github.com/cicadasmile/spring-cloud-base Code Cloud Address: Know a smile https://gitee.com/cicadasmile/spring-cloud-base