1. Thread isolation, service degradation (degradation by the consumer of the service)
When the service is busy, if there is an exception in the service, it is not a rude direct error report, but a friendly prompt will be returned. Although the user's access is denied, a result will be returned.
It's like going to buy fish. Usually the supermarket will give extra fish killing service. When the Spring Festival is over and busy, fish killing services may not be provided, which is the degradation of services.
When the system is very busy, some secondary services are temporarily interrupted, priority is given to ensuring the smooth flow of main services, and all resources are given priority to the use of main services. In the double 11 and 618, JD tmall will adopt such a strategy.
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
UserConsumerApplication.java
package cn.itcast.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
//@EnableDiscoveryClient
//@SpringBootApplication
//@Enable circuit breaker / / service meltdown
@SpringCloudApplication //Spring cloud application can replace the above three
public class UserConsumerDemoApplication {
@Bean
@LoadBalanced //load balancing
public RestTemplate restTemplate() {
// This time, we used the OkHttp client, which only needs to be injected into the factory
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
public static void main(String[] args) {
SpringApplication.run(UserConsumerDemoApplication.class, args);
}
}