(1) Nacos series of Spring Cloud Alibaba

Nacos is a dynamic service discovery, configuration management and service management platform that makes it easier to build cloud-native applications.

1. Enter nacos official website to download nacos

Insert picture description here
Insert picture description here
After the download is complete, unzip, open the bin directory, open the command window and enter stratup.cmd to complete the operation

Insert picture description here
Visit http://127.0.0.1:8848/nacos/index.html#/login
username and password are both nacos

2. Nacos server registration

2.1 Create Parent Engineering Project

Insert picture description here

2.2 Introduce Maven coordinates in modules that need to be registered

		<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
       

2.3 View Nacos console

Insert picture description here

2.3 Nacos load balancing

Simulate two services
Insert picture description here
View Nacos consoleInsert picture description here
You can see the introduction of ribbon in nacos coordinates

2.4 Create service consumers

Insert picture description here
Add the url of the service consumer in yml.
Insert picture description here
Use RestTemplate to access the paymentService microservice.
Insert picture description here
Insert picture description here
Use the consumer to access the interface. It
Insert picture description here
Insert picture description here
can be seen that nacos comes with load balancing.

2.5 Comparison of registration centers

Insert picture description here

2.6 Nacos supports switching between AP and CP modes

C是所有节点在同一时间看到的数据是一样的;而A的定义是所有的请求都会收到响应

何时选择使用何种模式?
一般来说,如果不需要存储服务级别的信息且服务实例是通过nacos-client注册,并能够保持心跳上报,name就可
以选择AP模式.当前主流的服务如Spring Cloud和Dubbo 服务都适用于AP模式,AP模式为了服务的可能性而减弱 一致性,因此AP模式下只支持注册临时实例

如果需要在服务级别编辑或者存储配置信息,那么CP是必须,K8S服务和DNS服务适用于CP模式。
CP模式下支持注册持久化实例,此时则是以Raft协议为集群运行模式,该模式下注册实例之前必须先注册服务,如果服务
不粗在则会返回错误

3 Nacos Service Configuration Center-Basic Configuration

3.1. New configuration

Insert picture description here

3.2 Nacos interface configuration correspondence

公式: s p r i n g . a p p l i c a t i o n . n a m e − {spring.application.name}- spring.application.name{spring.profiles.active}-${spring.cloud.nacos.config.file-extension}

The prefix defaults to the value of spring.application.name

spring.profile.active is the profile corresponding to the current environment, which can be configured through the configuration item spring.profile.active

file-exetension is the data format of the configuration content, which can be configured through the configuration item spring.cloud.nacos.config.file-extension

3.3 Writing Nacos configuration management

Insert picture description here

3.4 Write bootstrap.yml

Note: Bootstrap.yml has a higher priority than application.yml

Insert picture description here

3.5 Write Controller to test ConfigService

Insert picture description here
Insert picture description here

4 Nacos Service Configuration Center-Classification Configuration

4.1 The relationship between Namespace+Group+Data ID? Why is it designed like this

  • What is it?
    Similar to the package name and class name in Java, the
    outermost namespace can be used to distinguish the deployment environment, Group and DataId logically distinguish two target objects

  • Three situations

Insert picture description here
Default:
Namespace=public, Group=DEFAULT_GROUP, the default Cluster is DEFAULT

  • The default namespace of Nacos is public, and Namespace is mainly used to achieve isolation.
    For example, we now have three environments: development, testing, and production environments. We can create three namespaces, and different namespaces are isolated.
  • Group defaults to DEFAULT_GROUP, Group can divide different microservices into the same group
  • Service is a microservice; a Service can contain multiple Clusters (clusters), Nacos default Cluster is DEFAULT, Cluster is a virtual division of designated microservices.
    For example, for disaster recovery, Service microservices are deployed in the Hangzhou computer room and In the Guangzhou computer room, at this time, we can assign a cluster name (HZ) to the Service microservice in the Hangzhou computer room and a cluster name (GZ) to the Service microservice in the Guangzhou computer room. We can also try to make the microservices in the same computer room call each other To improve performance
  • The last Instance is the instance of the microservice

5.Nacos cluster configuration and persistence

Guess you like

Origin blog.csdn.net/qq_43565087/article/details/108984546