Writing in the front
Notes on Java growth from inception to architecture
In this article we demonstrate the implementation of downgrading in Hystrix as the first way to address avalanche effects
Hystrix – relegated
I. Introduction to the scene
Let’s start with a normal service invocation
When the consumer has a problem calling the provider service:
At this point we degrade the consumer’s service call
Ii. Case demonstration
1. Create projects
Create a SpringCloud project
2. Add dependencies
Add Hystrix dependencies to call load balancing through the ribbon
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> < version > 1.4.5. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - eureka < / artifactId > < version > 1.4.6. RELEASE < / version > < / dependency > < the dependency > < groupId > org, apache httpcomponents < / groupId > < artifactId > httpclient < / artifactId > < version > 4.5.8 < / version > < / dependency > <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.3.2.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Copy the code
3. Modify the configuration
Modify the application.properties file to set the name, port, and registry information
Name =eureka-ribbon-consumer-hystrix server.port=9091 Point to another registry eureka.client.serviceUrl.defaultZone=http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/Copy the code
4. Modify the startup class
Add comments to the startup class to turn on the circuit breaker. @EnableCircuitBreaker
@enablecircuitbreaker @enableeurekaclient @SpringBootApplication Public class SpringcloudEurekaConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudEurekaConsumerApplication.class, args); }}Copy the code
5. Modify the service layer
In the business layer code, use the ribbon in the getUsers method to get the load-balancing address, use the RestTemplate method to call the service, and add the @hystrixCommand annotation in the method header. The fallbackMethod property specifies that the fallback method is the fallback method when the provider method called is abnormal, and then returns the base data in its method.
@service public class UserService {/** * Ribbon load balancing * LoadBalancerClient Obtains Service information by Service name IP port */ @autoWired private LoadBalancerClient loadBalancerClient; @hystrixCommand (fallbackMethod = "fallBack") public List<User> getUsers(){// ServiceInstance Encapsulates basic service information, such as the IP address and port number ServiceInstance si = this.loadBalancerClient.choose("eureka-ribbon-provider"); StringBuilder sb = new StringBuilder(); sb.append("http://") .append(si.getHost()) .append(":") .append(si.getPort()) .append("/user"); System.out.println(" service address :"+ sb.tostring ()); // SpringMVC RestTemplate RestTemplate rt = new RestTemplate(); ParameterizedTypeReference<List<User>> type = new ParameterizedTypeReference<List<User>>() {}; ResponseEntity<List<User>> response = rt.exchange(sb.tostring (), httpmethod.get,null,type); List<User> list = response.getBody(); return list; } @return */ public List<User> fallBack(){List<User> List = new ArrayList<>(); List. add(new User(3," I am the data base ",22)); return list; }}Copy the code
The control layer code simply calls the methods of the business layer
Test 6.
Start the Eureka registry, and then start the Consumer service. The provider service does not need to be started, so that when we access the consumer service, there will be an exception. When our browser sees the bottom data, it means that the downgrade is successful.
Operation successful ~