In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
In the micro-service architecture, according to the business requirements, it is divided into small services, and then the services and services can be called each other RPC remotely. In Spring Cloud, you can use RestTemplate+Ribbon or Feign to make RPC remote calls. In order to ensure high availability of services, a single service is usually deployed in a cluster. Due to network reasons or its own reasons, the service cannot guarantee 100% availability. If there is a problem with the server, there will be thread blocking when calling the service. If there are a large number of requests, the service will be paralyzed. Then the circuit breaker came in handy.
When the unavailability of a call to a service reaches a threshold (Hystric defaults to 5 seconds 20 times) the circuit breaker will be opened automatically. When the circuit break is turned on, the fallback method can directly return a preset fixed value.
1. Create a new project sc-eureka-client-consumer-ribbon-hystrix, and the corresponding pom.xml file
4.0.0 spring-cloud sc-eureka-client-consumer-ribbon 0.0.1-SNAPSHOT jar sc-eureka-client-consumer-ribbon http://maven.apache.org org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.RELEASE pom import UTF-8 1.8 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud- Starter-netflix-ribbon org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-hystrix 2.0.1.RELEASE
Note: spring-cloud-starter-hystrix has been marked as expired in spring cloud 2.x. Spring-cloud-starter-netflix-hystrix is recommended
2. Create a new spring boot startup class ConsumerApplication.java
Package sc.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;@SpringBootApplication@EnableEurekaClient@EnableHystrixpublic class ConsumerHystrixApplication {public static void main (String [] args) {SpringApplication.run (ConsumerHystrixApplication.class, args);}}
You can see that this startup class just adds an additional annotation EnableHystrix (boot circuit breaker) to the startup class of "Service Discovery & Service Consumer Ribbon".
3. Write service classes and add circuit breaker annotations
Package sc.consumer.service.impl;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;import com.github.andrewoma.dexx.collection.ArrayList;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import sc.consumer.model.User;import sc.consumer.service.UserService;@Servicepublic class UserServiceImpl implements UserService {@ Autowired private RestTemplate restTemplate HystrixCommand (fallbackMethod = "getUserError") @ Override public Map getUser (Long id) {return restTemplate.getForObject ("http://sc-eureka-client-provider:8200/user/getUser/{1}", Map.class, id);} public Map getUserError (Long id) {Map map = new HashMap (); map.put (" code "," 000000 "); map.put (" msg "," ok ") User u = new User (); u.setId (- 1L); u.setUserName ("failName"); map.put ("body", u); return map;} @ HystrixCommand (fallbackMethod = "listUserError") @ Override public Map listUser () {return restTemplate.getForObject ("http://sc-eureka-client-provider:8200/user/listUser", Map.class) } public Map listUserError () {Map map = new HashMap (); map.put ("code", "000000"); map.put ("msg", "ok"); map.put ("body", new ArrayList ()); return map } @ HystrixCommand (fallbackMethod = "addUserError") @ Override public Map addUser (User user) {return restTemplate.postForObject ("http://sc-eureka-client-provider:8200/user/addUser", user, Map.class);} public Map addUserError (User user) {Map map = new HashMap (); map.put (" code "," 000000 "); map.put (" msg "," ok ") Map.put ("body", 0); return map;} @ HystrixCommand (fallbackMethod = "updateUserError") @ Override public Map updateUser (User user) {restTemplate.put ("http://sc-eureka-client-provider:8200/user/updateUser",user); Map map = new HashMap (); map.put (" code "," 000000 "); map.put (" msg "," ok ") Return map;} public Map updateUserError (User user) {Map map = new HashMap (); map.put ("code", "000000"); map.put ("msg", "ok"); map.put ("body", 0); return map } @ HystrixCommand (fallbackMethod = "deleteUserError") @ Override public Map deleteUser (Long id) {restTemplate.delete ("http://sc-eureka-client-provider:8200/user/deleteUser/{id}", id); Map map = new HashMap (); map.put (" code "," 000000 "); map.put (" msg "," ok "); return map } public Map deleteUserError (Long id) {Map map = new HashMap (); map.put ("code", "000000"); map.put ("msg", "ok"); map.put ("body", 0); return map;}}
Add a HystrixCommand annotation, and the corresponding parameter fallback method value is the name of the method that returns the default value when the mode server cannot call it.
4. Create new configuration files bootstrap.yml and application.yml
Bootstrap.yml
Server: port: 5600application.ymlspring: application: name: sc-eureka-client-consumer-ribbon-hystrixeureka: instance: hostname: 127.0.0.1 client: # because the application is a registry, it is set to false, which means that it does not register itself with the registry. RegisterWithEureka: true # since the registry is responsible for maintaining service instances, it does not need to retrieve services So it is also set to false fetchRegistry: true serviceUrl: defaultZone: http://127.0.0.1:5001/eureka/
5. Other project files participate in the following figure
6. Start the registry sc-eureka-server and the service provider sc-eureka-client-provider respectively
7. Start sc-eureka-client-consumer-ribbon-hystrix and verify whether the startup is successful
Way 1: check the log
Method 2: check whether the registry is registered successfully
8. Verify whether the circuit breaker works
(1) when the service provider accesses:
Http://127.0.0.1:5600/cli/user/getUser/4
Return the data in the database normally
(2) access when the service provider is closed:
Http://127.0.0.1:5600/cli/user/getUser/4
Comparing the two returned data, we can see that when the service provider shuts down, the returned data is the data written by the program, as shown in the following figure:
Other methods can be verified in accordance with the above methods.
Official account: java Paradise
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.