An overview of the SpringCloud architecture

theme: juejin
highlight: SpringCloud

I. Overview:

Spring Cloud provides developers with tools to quickly build some of the common patterns found in distributed systems (e.g., configuration management, service discovery, circuit breakers, intelligent routing, micro-proxies, control buses, one-time tokens, global locks, leadership elections, distributed sessions, cluster state). Coordination of distributed systems results in boilerplate styles, and using Spring Cloud developers can quickly get up and implement boilerplate services and applications. They will work well in any distributed environment, including developers’ own laptops, bare-metal data centers, and hosting platforms such as Cloud Foundry.

Characteristics of two.

Spring Cloud focuses on providing a good out-of-the-box experience for typical use cases and extensibility mechanisms, including other use cases.

1. Distributed/versioned configuration
2. Service registration and discovery
3. routing
4. Service to service calls
5. Load balancing
6. The circuit breaker
7. Global lock
8. Leadership election and cluster status
9. Distributed messaging

Spring Cloud

Summary of 3.1.

Spring Cloud Netflix provides Netflix OSS integration for Spring Boot applications by automatically configuring and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations, you can quickly enable and configure common patterns within your application and build large distributed systems with battle-tested Netflix components. Modes offered include service discovery (Eureka), circuit breakers (Hystrix), Intelligent Routing (Zuul) and client load balancing (Ribbon).

3.2 characteristics:

Spring Cloud Netflix features:

1. Service discovery: Instances of Eureka can be registered, and clients can use Spring-hosted beans to discover instances
2. Service discovery: You can create an embedded Eureka server using declarative Java configuration
3. Circuit breakers: Hystrix clients can be built using a simple annotation-driven method decorator
4. Breaker: Embedded Hystrix dashboard with declarative Java configuration
5. Declarative REST client: Feign creates a dynamic implementation of the interface decorated with JAX-RS or Spring MVC annotations.
6. Client load balancer: functional zone
7. External configuration: Bridge from Spring Environment to Archaius (using Spring Boot conventions to enable native configuration of Netflix components)
8. Routers and filters: Automatic registration of Zuul filters, and simple configuration conventions for reverse proxy creation
Using:Spring’s official website
3.3 had been

3.3.1 Details of Eureka (refer to the figure above)
Renewal: renew the contract
Eureka-server: A service registry (which can be a cluster) that exposes its own address (similar to Dubbo’s registry).
Providers: register their information (address, service name, etc.) with Eureka after startup, and renew the service periodically
Consumer: The service caller, who periodically goes to Eureka to pull the list of services and then selects a service to invoke using a load balancing algorithm.
Heartbeat (Renewal) : The provider periodically refreshes its status to Eureka via HTTP
First of all, Eureka is a registry with similar functions to ZooKeeper. The difference is that ZooKeeper is a software that can be used directly after startup, while Eureka needs to be developed by ourselves.
3.3.2 Using Eureke (On the Server)
1. Eureka registry required dependencies
<! - Eureka server - > < dependencies > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>Copy the code
2. Start the class with a comment
@SpringBootApplication
// Enable Eureka registration service
@EnableEurekaServer
public class EurekaServer {
	public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); }}Copy the code
3. Edit the project configuration file (application.properties)
Port = server.port=8080Name = demo-api-server # eureka eureka.instance.instance-id=${eureka.instance.ip-address}:${spring.application.name}:${eureka.instance.nonSecurePort} eureka.client.service-url.defaultZone=http:/ / 127.0.0.1:8888 / eureka
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=127.0. 01.
Copy the code
3.3.3 Using Eureke (On a Client)
1. Eureka registry required dependencies
<! - Eureka client - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>Copy the code
2. Start the class with a comment
@SpringBootApplication
// Enable Eureka registration service
@EnableEurekaClient
public class EurekaServer {
	public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); }}Copy the code
Step 3: Visit againhttp://127.0.0.1:8888 You can see that there are two services registered with Eureka
3.4 Zuul gateway

Summary of 3.4.1 track.
Requests from clients or internal service invocations. All requests to the service pass through the Zuul gateway, which then implements authentication, dynamic routing, and so on. A unified entry point for all Zuul services.
3.4.2 Zuul use
1. Zuul server needs to introduce dependencies
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
</dependencies>
Copy the code
2. The Zuul server needs to add an annotation to the startup class
@SpringBootApplication
// Enable Eureka registration service
@EnableEurekaClient
// Enable the Zuul gateway function
@EnableZuulProxy 
// Enable Eureka client discovery
@EnableDiscoveryClient
public class EurekaServer {
	public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); }}Copy the code
3. Edit the project configuration file (application.properties)
Zuul.prefix-routes. Test-service =/test-service/** # Switch on the circuit breaker if it takes more than 6 seconds to switch on the circuit breaker Zuul communication time > > hystrix fusing time retry mand. Retry time hystrix.com default. Execution. The isolation. Thread. TimeoutInMilliseconds = 6000Copy the code
test-serviceThis is inEurekaA service registered on, where its mapping path is configured. The path is going to be/api/test-serviceAny initial request will be sent to the service.
ZuulA component that integrates load balancing with another componentRibbonSo, ifEurekaThere are two names ontest-serviceCan automatically load balance, the default policy is polling
Circuit breaker mechanism:
ZuulAlso integratesHystixCircuit breaker mechanism. However, all the timeout policies are the default values. For example, the timeout period of fusing is only 6S. That is to say, if there is no response from 6S when accessing a service, it is considered as an exception