Architecture Advanced Study Notes: Thorough Analysis of Spring Cloud Microservice Architecture

Introduction to Spring Cloud Microservice Architecture

The goal of Spring Cloud is to provide Spring developers with a set of easy-to-use tools to build distributed systems. It mainly wraps other implementation stacks, starting with the Netflix OSS stack. These stacks will then be consumed through familiar tools for annotation-based configuration, Java configuration, and template-based programming. Let's look at a few Spring Cloud components.

Spring Cloud configuration server

Spring Cloud Config Server (configuration server) provides a horizontally scalable centralized configuration service. It uses a pluggable repository layer as its data storage, and currently supports local storage, Git and Subversion. By using the version control system as configuration storage, developers can easily version and review configuration changes.

The configuration is expressed as a Java attribute or YAML file. Config Server merges these files into environment objects and presents the understanding model of Spring properties and configuration files as REST API. This REST API can be directly queried by any application to obtain configuration data, but smart client binding can be added to Spring Boot applications. These applications will automatically coordinate any local configuration with the configuration received by the configuration server.

Spring Cloud bus

Spring Cloud Config Server is a powerful mechanism for uniformly distributing configuration among a set of application instances. However, for now, we are currently limited to updating such configurations when the application starts. After pushing the new value of the property to Git, we need to manually restart each application process to get the new value. What we want is the ability to refresh the application configuration without restarting.

Spring Cloud Bus adds a management backplane to your application instance. It is currently implemented as a client bound to a set of AMQP exchanges and queues, but this backend is also designed to be pluggable. Cloud Bus adds more management endpoints to your application. In the figure, we see a request POSTed to the /bus/refresh endpoint of App A to obtain the new value of Git's greeting attribute. This request triggers three events:

1. App A requests the latest configuration from the configuration server. Any Spring Bean annotated with @RefreshScope will be reinitialized to the new configuration.

2. Application A sends a message to the AMQP switch, indicating that it has received the refresh event.

3. Apps B and C participating in Cloud Bus by listening to the appropriate AMQP queue receive messages and update their configuration in the same way as App A. Now we can refresh the configuration of the application without restarting.

Spring Cloud Netflix

Spring Cloud Netflix provides several packages of Netflix components: Eureka, Ribbon, Hystrix and Zuul. These are discussed in turn below.

Eureka is a flexible service registry implementation. The service registry is a mechanism for service discovery

Spring Cloud Netflix simply adds the spring-cloud-starter-eureka-server dependency to the Spring Boot application, and then uses @EnableEurekaServer to annotate the configuration class of the application.

Applications can participate in service discovery by adding spring-cloud-starter-eureka dependencies and annotating @EnableDiscoveryClient and its configuration class. This annotation provides the ability to inject a properly configured instance of DiscoveryClient into any Spring Bean. DiscoveryClient is an abstraction of service discovery. In this case, it happens to be implemented by Eureka, but it can also provide integration with other candidate technology stacks (such as Consul). DiscoveryClient can provide location information (such as network addresses) and provide other metadata for registering service instances in Eureka based on the service logic identifier.

The load balancing algorithm provided by Eureka is limited to round robin. Ribbon provides a complex client IPC library with configurable load balancing and fault tolerance. The polling list can be derived from the dynamic server list obtained by the Eureka server. Spring Cloud Netflix provides polling Ribbon integration by adding spring-cloud-starter-ribbon Spring dependencies to Spring Boot. This additional library also provides the ability to inject a properly configured instance of LoadBalancerClient into any Spring Bean, which will enable the client to obtain load balancing (Figure 4).

In addition to other tasks, the use of Ribbon can also implement additional load balancing algorithms, such as availability filtering, weighted response time, and availability zones are closely related.

Spring Cloud Netflix will further enhance the use of Ribbon by Spring developers by automatically creating a Ribbon enhanced instance of RestTemplate that can be injected into any Spring Bean. Developers can simply pass the logical service name in the provided URL to RestTemplate:

@Autowired
@LoadBalanced
private RestTemplate restTemplate;

@RequestMapping("/")
public String consume() {
ProducerResponse response = restTemplate.getForObject("http://producer", ProducerResponse.class);
return String.format("{"value": %s}", response.getValue());
}

Hystrix provides implementations of common fault tolerance modes (such as circuit breakers and partitions) for distributed systems. Circuit breakers are usually implemented as state machines, examples of which are found in Figure 5.

The circuit breaker can be placed between the service and its remote dependencies. If the circuit is closed, the call to the dependency is passed normally. If the call fails, the failure is calculated. If the number of failures reaches the threshold within a configurable time period, the circuit is tripped to open. In the open state, calls are no longer sent to dependencies, and the generated behavior is customizable (throwing an exception, returning dummy data, calling different dependencies, etc.).

Periodically, the state machine will transition to the "half-open" state to determine if the dependency is healthy again. In this state, the request is passed again. If the request is successful, the machine will return to the off state. If the request fails, the machine will switch back to the open state.

Spring Cloud applications can take advantage of Hystrix by adding spring-cloud-starter-hystrix dependencies and annotating @EnableCircuitBreaker and its configuration class. Then, you can use @HystrixCommand to add a circuit breaker to any Spring Bean method in the following way:

@HystrixCommand(fallbackMethod = "getProducerFallback")
public ProducerResponse getValue() {
return restTemplate.getForObject("http://producer", ProducerResponse.class);
}

This example specifies a fallback method named getProducerFallback. When the circuit breaker is open, this method will be used instead of getValue:

private ProducerResponse getProducerFallback() {
return new ProducerResponse(42);
}

In addition to providing state machine behavior, Hystrix also sends out an indicator stream from each circuit breaker, providing important telemetry such as request measurement, response time histogram, and the number of successful, failed, and short-circuit requests (Figure 6).

Zuul handles all incoming requests for Netflix edge services. It is used in conjunction with other Netflix components (such as Ribbon and Hystrix) to provide a flexible and elastic routing layer for Netflix services.

Netflix uses filters dynamically loaded into Zuul to perform the following functions:

1. Identity verification and security: Identify the identity verification requirements of each resource, and reject requests that do not meet them.

2. Insight and monitoring: Track meaningful data and statistics at the edge so that we can accurately understand the production situation.

3. Dynamic routing: dynamically route requests to different back-end clusters as needed.

4. Stress test: Gradually increase the traffic to the cluster to measure performance.

5. Unloading: Allocate capacity for each type of request and delete requests that exceed the limit.

6. Static response processing: Build some responses directly at the edge instead of forwarding them to the internal cluster.

7. Multi-regional flexibility: Route requests across AWS regions to diversify our ELB usage and bring our edge closer to our members.

In addition, Netflix uses Zuul's capabilities to perform external routing and stress testing through the canary version.

For very common use cases such as a UI application that wants to proxy calls to one or more back-end services, Spring Cloud has created an embedded Zuul proxy to simplify development. This function facilitates the user front-end to proxy the back-end services they need, avoiding the need for all back-ends to manage CORS (cross-origin resource sharing) and authentication issues separately. Zuul proxy is an important application of the API gateway pattern (Figure 7)

Spring Cloud enhances the embedded Zuul proxy, provides automatic processing of file uploads, and can easily provide OAuth2 SSO by adding Spring Cloud Security and provide token inheritance to downstream services. Zuul uses Ribbon as a load balancer for its clients and all outbound requests. Ribbon's dynamic server list is usually imported by Eureka, but Spring Cloud can import from other sources.

Entering the world of microservices brings us directly into the world of distributed systems. Distributed systems do not always "work normally", so we must assume that the behavior and location of our system's components will often change in unpredictable ways.

 

Guess you like

Origin blog.csdn.net/mrchaochao/article/details/108603120