Article directory
- Foreword: the foundation
-
- What is a microservice architecture?
- What is the difference between a microservices architecture and a centralized architecture?
- Progressive relationship between Spring family buckets
- Thinking: What are the problems in calling between microservices and microservices, and what technologies are needed?
- Thinking: Without a microservice architecture, can calls between services be implemented?
- Problems that cannot be solved by direct http calls
- How to solve?
- 1. What is Nacos?
- 2. Nacos Registry
- 3. Spring Cloud Alibaba Nacos Quick Start
- 4. Nacos source code compilation
- 5. Analysis of the key implementation of Nacos
-
- 5.1 How does Alibaba-Nacos realize automatic registration
- 5.2 What storage is used for nacos registered instance?
- 5.3 What is used to store nacos configuration data?
- 5.4 How does nacos ensure the synchronization of nacos clusters?
- 5.5 The specific process of nacos service registration: how does the service instance register with the memory registry?
Foreword: the foundation
What is a microservice architecture?
The microservice architecture is a decentralized architecture. For example, if our registry is down, services can still be transferred to the resources of other services (with cache), but the current gateway layer also tends to be centralized.
What is the difference between a microservices architecture and a centralized architecture?
The SOA we often see is a centralized architecture, such as ESB (Enterprise Message Bus), which is forwarded to other downstream services through nginx, but it has a single point of problem with nginx.
Progressive relationship between Spring family buckets
Spring: bean-based, javaconfig or xml-based way to load third-party framework beans into the Spring container, such as Mybatis' SqlSessionFacroty
Springboot: Appication-based auto-configuration of default beans for third-party frameworks that Spring relies on
SpringCloud: Based on Service, it provides common functions of microservice architecture (developed by different companies and integrated by SpringCloud)
netflix, alibaba, huawei (open source but not adopted by the SpringCloud community)
Thinking: What are the problems in calling between microservices and microservices, and what technologies are needed?
1. rpc call between services: openfeign/dubbo
2. Service registration and discovery: eureka, consul, nacos
3. Unified management of configuration: zk, redis, consul, nacos
4. Inspection of service links: skywalking, zipkin+sleush
5. Monitoring of microservices: prometheus
6. Fusing/limiting/downgrading of services: hystrix, sentinel (semi-finished product)
7. Distributed Transactions: Seata
8. External gateway: zuul, gateway
Thinking: Without a microservice architecture, can calls between services be implemented?
Without the SpringCloud technology stack, we can also make inter-service calls: RestTemplate, WebClient, HttpClient, okHttp3
Problems that cannot be solved by direct http calls
1. Client load balancing: List(localhost:8020, localhost:8040)
2. The client perceives whether the provider node is available
3. After the server node hangs up, remove the hanged node from the service list
How to solve?
Can introduce nacos registry optimization
1. What is Nacos?
Official documentation: https://nacos.io/en-us/docs/what-is-nacos.html
Nacos is dedicated to helping you discover, configure and manage microservices. Nacos provides a set of easy-to-use features to help you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management.
The key features of Nacos include:
1. Service discovery and service health monitoring
2. Dynamic configuration service
3. Dynamic DNS service
4. Service and its metadata management
Mainstream registry
1.1 Nacos Architecture
NamingService: Naming service, core interface of registry center
ConfigService: Configuration service, core interface of configuration center
OpenAPI documentation: https://nacos.io/zh-cn/docs/open-api.html
nacos version: v1.1.4 Upgrade to v1. 4.1
1.2 Nacos Server Deployment
Download the source code and compile
the source code download address: https://github.com/alibaba/nacos/
cd nacos/
mvn -Prelease-nacos clean install -U
Download the installation package
Download address: https://github.com/alibaba/Nacos/releases
1.2.1 Stand-alone mode
Official document: https://nacos.io/zh-cn/docs/deployment.html
Unzip, enter the nacos directory
, start nacos on a single machine, and execute the command
bin/startup.sh -m standalone
You can also modify the default startup method
Access the management side of nacos: http://192.168.3.14:8848/nacos , the default username and password are nacos/nacos
1.2.2 Cluster Mode
Official website documentation: https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
Cluster Deployment Architecture Figure
1) Build a pseudo-cluster on a single machine, copy the nacos installation package, and modify it to nacos8849, nacos8850, nacos8851
2 ) Take nacos8849 as an example, enter the nacos8849 directory
2.1) Modify the configuration of conf\application.properties and use an external data source
#使用外置mysql数据源
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=root
2.2) Change conf\cluster.conf.example to cluster.conf and add node configuration
# ip:port
192.168.3.14:8849
192.168.3.14:8850
nacos8850, nacos8851 are configured in the same way.
3) Create a mysql database, sql file location: conf\nacos-mysql.sql
4) Modify the jvm parameters of the startup script (bin\startup.sh)
5) Start nacos8849, nacos8850, and nacos8851 respectively,
take nacos8849 as an example, enter the nacos8849 directory, start nacos
bin/startup.sh
6) Test
Log in to http://192.168.3.14:8849/nacos , both username and password are nacos
7) Official recommendation, nginx reverse proxy
access: http://192.168.3.14:8847/nacos
1.3 Prometheus+grafana monitors Nacos
https://nacos.io/zh-cn/docs/monitor-guide.html
Nacos version 0.8.0 improves the monitoring system and supports monitoring the running status of Nacos by exposing metrics data to a third-party monitoring system.
1. Nacos expose metrics data
management.endpoints.web.exposure.include=*
Test: http://localhost:8848/nacos/actuator/prometheus
2. Prometheus collects Nacos metrics data
Modify the configuration file prometheus.yml to collect Nacos metrics data
metrics_path: '/nacos/actuator/prometheus'
static_configs:
- targets: ['{ip1}:8848','{ip2}:8848','{ip3}:8848']
Start the prometheus service
prometheus.exe --config.file=prometheus.yml
Test: http://localhost:9090/graph
3.grafana displays metrics data
Test: http://localhost:3000/
2. Nacos Registry
2.1 Evolution of the registry and its design ideas
2.2 Nacos registry architecture
2.3 Core functions
Service registration : Nacos Client will register its own service with Nacos Server by sending a REST request, and provide its own metadata, such as ip address, port and other information. After Nacos Server receives the registration request, it will store the metadata information in a double-layered memory Map.
Service heartbeat : After the service is registered, Nacos Client will maintain a regular heartbeat to continuously notify Nacos Server, indicating that the service is always available and prevents it from being deleted. By default, a heartbeat is sent every 5s.
Service synchronization : Nacos Server clusters will synchronize service instances with each other to ensure the consistency of service information. leader raft
Service discovery : When the service consumer (Nacos Client) calls the service of the service provider, it will send a REST request to the Nacos Server to obtain the list of services registered above, cache it locally in the Nacos Client, and open a local one in the Nacos Client. Scheduled tasks regularly pull the latest registry information from the server and update it to the local cache
Service health check : Nacos Server will start a scheduled task to check the health of registered service instances. For instances that have not received the client's heartbeat for more than 15s, its healthy attribute will be set to false (the client service will not be discovered when the service is discovered). ), if an instance does not receive a heartbeat for more than 30 seconds, the instance will be removed directly (the removed instance will be re-registered if it resumes sending heartbeats)
2.4 Service registry structure
2.5 Service Domain Model
2.6 Service instance data
3. Spring Cloud Alibaba Nacos Quick Start
3.1 Spring Cloud Alibaba version selection
3.2 Build Nacos-client service
1) Introduce dependencies
. The parent Pom supports spring cloud & spring cloud alibaba, and introduces dependencies
<dependencyManagement>
<dependencies>
<!--引入springcloud的版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Introduce dependencies into the current project pom
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
- Placed in application.properties
server.port=8002
#微服务名称
spring.application.name=service-user
#配置 Nacos server 的地址
More configuration: https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-discovery
3) Start the springboot application and check the nacos management interface to see if the registration is successful
4) Test
Use RestTemplate to make service calls, you can use the microservice name (spring.application.name)
String url = "http://service-order/order/findOrderByUserId/"+id;
Note: Need to add @LoadBalanced annotation
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
4. Nacos source code compilation
Source code download address: https://github.com/alibaba/nacos/Version
: Nacos 1.4.1
1) Enter the nacos directory and execute the compile command
# 编译1.4.1 增加 -Drat.skip=true 参数 ,跳过licensing 检查
mvn -Prelease-nacos -Dmaven.test.skip=true -Drat.skip=true clean install -U
After the compilation is successful, nacos-server-1.4.1.tar.gz will be generated in the distribution/target directory
2) Start nacos and
enter the console module, find the startup class com.alibaba.nacos.Nacos, and execute the main method
3) Configure the startup parameters for a
single machine Mode execution requires specifying nacos.standalone=true
-Dnacos.standalone=true -Dnacos.home=D:\code\java_yuanma\nacos-1.4.1
Create nacos_config database (distribution/conf/nacos-mysql.sql)
and enable mysql configuration in application.properties
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=root
4) Enter http://localhost:8848/nacos , the username and password default to nacos
5. Analysis of the key implementation of Nacos
5.1 How does Alibaba-Nacos realize automatic registration
To actually call nacos-client' to register a service instance with nacos-server, you need to manually call the api
// NamingService 注册中心核心服务
NamingService namingService = NamingFactory.createNamingService("localhost:8848");
String serviceName = "mall-user";
namingService.registerInstance(serviceName,"localhost",8040);
It's just that SpringCloudAlibaba helped us do this step:
When the service starts, Springboot will first call the refresh method to register the bean in the Spring container, and then start the onApplication monitoring event. SpringCloudAlibaba inherits the abstract class AbstractAutoServiceRegister and implements the Spring extension point ApplicationListener#onApplication to call This step of namingService.registerInstance(name, ip, host) automatically registers our service instance with nacos-server.
5.2 What storage is used for nacos registered instance?
Temporary nodes exist in memory, and persistent nodes will persist to disk: id of data/nameing/namespace, and instance storage supports AP and CP modes. When nacos pulls an instance, it pulls both the temporary instance and the persistent instance at one time. There is no switching between AP and CP (both)
Temporary instance: AP architecture, the registration center does not have high requirements for consistency
Persistent Instances: CP Architecture
5.3 What is used to store nacos configuration data?
Where is configuration data stored? derby or mysql
5.4 How does nacos ensure the synchronization of nacos clusters?
The nacos cluster ensures the CP architecture and is implemented based on the raft algorithm
5.5 The specific process of nacos service registration: how does the service instance register with the memory registry?
//Map(namespaceId, Map(group::serviceName, Service)).
private final Map<String, Map<String, Service>> serviceMap = new ConcurrentHashMap<>();
// namespace: 命名空间,起到隔离instances的作用,可以以环境隔离也可以以项目隔离 "No instances available for mall-order"
// group: 分组,也起到隔离instances和隔离配置的作用,对于instances建议用同一个group,对于配置中心根据环境不用选择用不同group
比如现在有Service: mall-order, mall-user
private Map<String, Cluster> clusterMap = new HashMap<>(); // 集群,保证能够互通,尽可能同一集群提升性能(ribbon负载均衡算法,优先使用同一集群的节点),集群下都是同一服务的实例
@JsonIgnore
private Set<Instance> persistentInstances = new HashSet<>(); // 持久实例
@JsonIgnore
private Set<Instance> ephemeralInstances = new HashSet<>(); // 临时实例
instance: 8020 8021 mall-order
ephemeral: false // 配置持久实例,默认是true
Explain the above picture:
1. First, the namespaceId will be used as the key to distinguish, and the value is a Group object (also a map), which is a map structure.
2. Then use groupId + serviceName as the key to distinguish, and the value is a Cluster object (also a map), which is also a map structure.
3. There are two properties in the Cluster object, persistentInstances and ephemeralInstances, which are two lists, which store persistent nodes and temporary nodes respectively. Every time a service is pulled from the registry, all persistent nodes and temporary nodes belonging to this serviceName will be pulled down.