Spring Cloud Netflix Structure Analysis of of summary

  • 2021-01-03 20:54:55
  • OfStack

Recently contacted the micro service this thing, has had some understanding to this aspect, takes out shares 1 with everybody.

1. Microservice framework Spring Boot+Spring Cloud

Spring Cloud is a micro service framework based on Spring Boot 1. It can be said that Spring Boot as the framework and Spring Cloud as the micro service constitute a new framework system that cannot be ignored. It provides configuration management, service discovery, circuit breaker, intelligent routing, microagent, control bus, global locking, decision campaign, distributed session and cluster state management components for microservice development, and is easy to use. Spring Cloud contains a lot of sub-frameworks, among which Spring Cloud Netflix is one of them. It mainly provides modules including: service discovery, circuit breaker and monitoring, intelligent routing, client load balancing and so on.

features

Eureka instances can register and discover bean managed with spring An embedded Eureka server can be created with a declarative Java configuration The Hystrix client can be driven with simple annotations The Java configuration enables the embedded Hystrix indicator panel Client load balancing

Spring Cloud Netflix components and deployment

(1) Eureka, Service registration and Discovery, which provides a service registry, a service discovery client, and a convenient interface for viewing all registered services. All services use Eureka's service discovery client to register themselves on Eureka's servers.

(2) Zuul, gateway, through which all client requests access the service in the background. He can use the 1-determined routing configuration to determine which service will handle a given URL. And get the registered service from Eureka to forward the request.

(3) Ribbon, or load balancing. When Zuul gateway sends a request to the application of a service, if a service starts multiple instances, it will send to a service instance through Ribbon through the specified load balancing policy.

(4) Feign, the service client. If the services need to access each other, they can use RestTemplate or Feign client to access. By default, it uses Ribbon for load balancing.

(5) Hystrix, monitoring and circuit breaker. We only need to add the Hystrix tag to the service interface to realize the monitoring and circuit breaker function of this interface.

(6) Hystrix Dashboard, monitoring panel, which provides an interface to monitor the time consumed by service invocation on each service.

(7) Turbine, monitoring aggregation, using Hystrix monitoring, we need to open the monitoring information of each service instance to see. Turbine, on the other hand, helps us aggregate monitoring information from all service instances into one place for viewing.

3. Spring Cloud Netflix component development

Can refer to the Chinese document: https: / / springcloud cc/spring - cloud - netflix. html

(1) Service Registration and Monitoring Center:


@SpringBootApplication
@EnableEurekaServer
@EnableHystrixDashboard
public class ApplicationRegistry {
 public static void main(String[] args) {
  new SpringApplicationBuilder(Application.class).web(true).run(args);
 }
}

Here the @SpringBootApplication tag spring boot is used to indicate that the current application is 1 spring boot application. This allows me to launch the application in IDE directly using the main function, or I can package it and launch it from the command line. You can also start the packaged war package with a server like tomcat. Using the TAB @EnableEurekaServer, you can start components of the Eureka service registry during startup. It listens on a port, which is 8761 by default, to receive the service registration. And provides 1 web page, after opening, you can see the registered service. Adding @EnableHystrixDashboard provides a monitoring page where we can enter the address of the service we want to monitor to see the invocation of the interface with Hystrix monitoring enabled. Of course, in order to use the above components, we need to add corresponding dependencies to es10106EN files, such as using ES108en-ES109en-ES110en-ES111en, relying on ES112en-ES113en-ES115en-ES116en and ES117en-ES118en-ES120en-ES121en, etc.

(2) Inter-service invocation:

There are two ways to make a service call, RestTemplate and FeignClient. Either way, it calls the service's http interface through the REST interface, with parameters and results serialized and deserialized by default via jackson. Because of the interface defined by RestController MVC, the returned data is serialized to json data via jackson.

Type 1: RestTemplate. You only need to define one Bean of RestTemplate and set it to LoadBalanced:


@Configuration
public class SomeCloudConfiguration {
 @LoadBalanced
 @Bean
 RestTemplate restTemplate() {
  return new RestTemplate();
 }
}

So we can inject this bean use where we need it:


public class SomeServiceClass {
 @Autowired
 private RestTemplate restTemplate;
 public String getUserById(Long userId) {
  UserDTO results = restTemplate.getForObject("http://users/getUserDetail/" + userId, UserDTO.class);
  return results;
 }
}

Where users is the service ID, and Ribbon will get one instance of this service from the service instance list, send the request, and get the result. Object UserDTO requires a sequence number, and its anti-sequence number is automatically completed.

Type 2: FeignClient


@FeignClient(value = "users", path = "/users")
public interface UserCompositeService {
 @RequestMapping(value = "/getUserDetail/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
 UserDTO getUserById(@PathVariable Long id);
}

We only need to use @ES159en to define one excuse, and Spring Cloud Feign will help us to generate an implementation of it and get data from the corresponding users service. Where, value in @ES164en (value = "users", path = "/users/getUserDetail") is the service ID, and path is the path prefix for the 1 set of interfaces. In the method definition below, it is as if interface 1 of Spring MVC was set, for which the corresponding URL is /users/getUserDetail/{id}. Then, when it is used, it is injected as a service like a 1 and then used:


public class SomeOtherServiceClass {
 @Autowired
 private UserCompositeService userService;
 public void doSomething() {
  // .....     
  UserDTO results = userService.getUserById(userId);
  // other operation...     
 }
}

(3) Circuit breaker:


// Circuit breaker: To solve the problem of calling a backup method instead of a failed method when a method call fails. Fault tolerance/cascading errors have been achieved 
//fallbackMethod Specify a backup method 
@HystrixCommand(fallbackMethod = "doStudentFallback")
@RequestMapping(value = "dostudent",method = RequestMethod.GET)
public String doStudent(){
 return "your name:secret,your age:secret!";
}

public String doStudentFallback(){
 return "your name:FEIFEI,your age:26!";
}

Among them, @ES186en is used to enable circuit breaker support, and Spring Cloud provides a console to monitor circuit breaker operation, which is enabled through the @ES189en annotation.


Related articles: