Overview of Spring Cloud architecture

Overview of Spring Cloud architecture

theme: csdn
highlight: SpringCloud

I. Overview:

Spring Cloud provides tools for developers to quickly build some common patterns in distributed systems (for example, configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership elections, distributed sessions, cluster state). Coordination of distributed systems leads to boilerplate, and using Spring Cloud developers can quickly stand up and implement services and applications that implement these boilerplates. They will work well in any distributed environment, including developers' own laptops, bare-metal data centers, and hosted platforms such as Cloud Foundry.

2. Features

Spring Cloud focuses on providing a good out-of-the-box experience for typical use cases and extensibility mechanisms (among others).

1. Distributed/versioned configuration
2. Service registration and discovery
3. Routing
4. Service-to-service calls
5. Load balancing
6. Circuit breaker
7. Global lock
8. Leader Election and Cluster Status
9. Distributed messaging

三. Spring Cloud Netflix

3.1 Overview:

Spring Cloud Netflix provides ***Netflix OSS*** integration for Spring Boot applications by auto-configuring and binding to ***Spring Environment*** and other Spring programming model idioms. With a few simple annotations, you can quickly enable and configure common patterns inside your application and build large distributed systems using battle-tested Netflix components. Provided patterns include service discovery ( Eureka ), circuit breaker ( Hystrix ), smart routing ( Zuul ) and client-side load balancing ( Ribbon ).

3.2 Features:

Spring Cloud Netflix features:

1. Service discovery: Eureka instances can be registered, and clients can use Spring-managed beans to discover instances
2. Service discovery: Embedded Eureka servers can be created using declarative Java configuration
3. Circuit breakers: Hystrix clients can be built using simple annotation-driven method decorators
4. Circuit Breaker: Embedded Hystrix Dashboard with Declarative Java Configuration
5. Declarative REST client: Feign creates a dynamic implementation of an interface decorated with JAX-RS or Spring MVC annotations.
6. Client Load Balancer: Ribbon
7. External configuration: a bridge from Spring Environment to Archaius (using Spring Boot conventions to enable native configuration of Netflix components)
8. Routers and Filters: Auto-registration of Zuul filters, and simple configuration conventions for reverse proxy creation
Excerpt from: spring official website
3.3 Eureka

EurekeImpl.png

3.3.1 Detailed explanation of Eureka (refer to the above figure)
renewal: renewal
Eureka-Server: It is the service registration center (which can be a cluster), which exposes its own address to the outside world (similar to Dubbo's registration center).
Provider: Register your own information (address, service name, etc.) with Eureka after startup, and regularly renew the service
Consumer: The service caller will regularly go to Eureka to pull the service list, and then use the load balancing algorithm to select a service to call.
Heartbeat (renewal): The provider regularly refreshes its status to Eureka through http
First of all, Eureka is a registration center, which has 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 Eureke use (server)
1. Eureka registry required dependencies
<!-- Eureka服务端 -->
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
</dependencies>
2. Add an annotation to the startup class
@SpringBootApplication
//开启Eureka注册服务
@EnableEurekaServer
public class EurekaServer {
    
    
	public static void main(String[] args) {
    
    
		SpringApplication.run(EurekaServer.class, args);
	}
}
3. Edit the project configuration file (application.properties)
# 服务端口设置
server.port=8080
# 网关服务名称
spring.application.name=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.0.1
3.3.3 Eureke use (client)
1. Eureka registry required dependencies
<!-- Eureka客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. Add an annotation to the startup class
@SpringBootApplication
//开启Eureka注册服务
@EnableEurekaClient
public class EurekaServer {
    
    
	public static void main(String[] args) {
    
    
		SpringApplication.run(EurekaServer.class, args);
	}
}
Step 3: Visit http://127.0.0.1:8888 again to see that there are two services registered to Eureka
3.4 Zuul Gateway

ZuulImpl.png

3.4.1 Overview:
A request from a client or an internal call to a service. All requests for services will go through the Zuul gateway, and then the gateway will implement authentication, dynamic routing and other operations. A unified entry point for all Zuul services.
3.4.2 Using Zuul
1. The Zuul server needs to introduce dependencies
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
</dependencies>
2. The Zuul server needs to add an annotation to the startup class
@SpringBootApplication
// 开启Eureka注册服务
@EnableEurekaClient
// 开启Zuul的网关功能
@EnableZuulProxy 
// 开启Eureka的客户端发现
@EnableDiscoveryClient
public class EurekaServer {
    
    
	public static void main(String[] args) {
    
    
		SpringApplication.run(EurekaServer.class, args);
	}
}
3. Edit the project configuration file (application.properties)
# 添加路由前缀(这里是映射路径)
zuul.prefix.routes.test-service=/test-service/**
# 开启熔断机制,超过六秒即开启熔断机制,网关内的时间排序:zuul的通信时间 > hystrix熔断时间 > retry重试时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
test-service is a service registered on Eureka , and its mapping path is configured here. Requests whose path starts with /api/test-service will be dispatched to this service.
Zuul is a component Ribbon that integrates another component load balancing. Therefore, ifthere are two services named test-service on Eureka , load balancing can be performed automatically. The default strategy is polling
Fuse mechanism:
Zuul also integrates the Hystix fuse mechanism. But all the timeout strategies are the default values. For example, the timeout period of the fuse is only 6S. That is to say, when accessing a service, if there is no response in 6s, it will be judged as abnormal. We can also set the timeout period by ourselves.

Guess you like

Origin blog.csdn.net/weixin_43869435/article/details/115768412