SpringCloudAlibaba Nacos service registration and configuration center

Introduction to SpringCloud Alibaba

1. Why does SpringCloud Alibaba appear

Because the Spring Cloud Netflix project enters the maintenance mode, you can see the details Official website description.

What is maintenance mode?

Putting the module in maintenance mode means that the Spring Cloud team will no longer add new functionality to the module. We will fix block level bugs and security issues, and we will also consider and review small pull request s from the community.

2. SpringCloud Alibaba concept

Spring Cloud Alibaba is committed to providing a one-stop solution for microservice development. It contains the necessary components for developing distributed application microservices, so that developers can easily use these components to develop distributed application services through the Spring Cloud programming model.

Relying on Spring Cloud Alibaba, you only need to add some annotations and a few configurations to connect Spring Cloud applications to Alibaba microservice solutions and quickly build distributed application systems through Alibaba middleware.

3. Main functions

  • Service current limiting and degradation: by default, it supports the access of WebServlet, WebFlux, OpenFeign, RestTemplate, Spring Cloud Gateway, Zuul, Dubbo and RocketMQ current limiting and degradation functions. It can modify the current limiting and degradation rules in real time through the console at runtime, and it also supports the viewing of current limiting and degradation Metrics monitoring.
  • Service registration and discovery: it adapts to the Spring Cloud service registration and discovery standard and integrates Ribbon support by default.
  • Distributed configuration management: supports external configuration in distributed system, and automatically refreshes when the configuration changes.
  • Message driven capability: build message driven capability for microservice applications based on Spring Cloud Stream.
  • Distributed transaction: use @ GlobalTransactional annotation to solve distributed transaction problems efficiently and without business intrusion.
  • Alibaba cloud object storage: Alibaba cloud provides massive, secure, low-cost and highly reliable cloud storage services. Support the storage and access of any type of data in any application, any time and any place.
  • Distributed task scheduling: provide second level, accurate, highly reliable and highly available timed (based on Cron expression) task scheduling services. At the same time, it provides a distributed task execution model, such as grid tasks. Grid tasks support the uniform distribution of tasks to all workers (schedulerx clients) for execution.
  • Alibaba cloud SMS service: a global SMS service with friendly, efficient and intelligent interconnected communication capabilities helps enterprises quickly build customer access channels.

4. Download address

Official website: https://spring.io/projects/spring-cloud

Github Chinese version: https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md

Official website reference manual: https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html

5. Main components

  • Sentinel: take the flow as the entry point to protect the stability of services from multiple dimensions such as flow control, fuse degradation and system load protection.
  • Nacos: a dynamic service discovery, configuration management and service management platform that is easier to build cloud native applications.
  • RocketMQ: an open source distributed messaging system, based on highly available distributed cluster technology, provides low latency and highly reliable message publishing and subscription services.
  • Dubbo: Apache Dubbo ™ Is a high-performance Java RPC framework.
  • Seata: Alibaba open source product, an easy-to-use high-performance microservice distributed transaction solution.
  • Alibaba Cloud OSS: Alibaba cloud Object Storage Service (OSS) is a massive, secure, low-cost and highly reliable cloud storage service provided by Alibaba cloud. You can store and access any type of data in any application, anytime and anywhere.
  • Alibaba cloud scheduler X: a distributed task scheduling product developed by Alibaba middleware team, which provides second level, accurate, highly reliable and highly available timed (based on Cron expression) task scheduling services.
  • Alibaba Cloud SMS: a global SMS service with friendly, efficient and intelligent interconnected communication capabilities to help enterprises quickly build customer access channels.

Introduction to Nacos

1. Concept

A dynamic service discovery, configuration management and service management platform that is easier to build cloud native applications.

In short: Nacos is a combination of registration center and configuration center. Nacos = Eureka + Config + Bus

2. What can I do

  • Replace Eureka as the service registry
  • Replace Config as the service configuration center

3. Download address

Github address: https://github.com/alibaba/Nacos

Official website: https://nacos.io/zh-cn/

Download version (find the version you want): https://github.com/alibaba/nacos/tags

4. Comparison of various registries

Service registration and discovery frameworkCAP modelConsole managementCommunity activity
EurekaAPsupportLow (2.x version off)
ZookeeperCPI won't support itin
ConsulCPsupporthigh
NacosAPsupporthigh

5. Install and run

There is a good Java8 + Maven environment locally

Download from the official website (I use version 1.1.4 here): 1.1.4

Unzip the installation package and directly run startup.cmd under the bin directory

After the command runs successfully, you can directly access: http://localhost:8848/nacos

The default username / password is nacos

Operation interface:

Nacos service registry

1. Nacos based service provider

  1. Create a new cloudalibaba provider payment9001 module
  2. POM
<dependencies>
    <!-- SpringCloud ailibaba nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--monitor-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--Hot deployment-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. YML
server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Configure Nacos address

management:
  endpoints:
    web:
      exposure:
        include: '*'  #monitor
  1. Main startup class
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {

    public static void main(String[] args) {
        SpringApplication.run(PaymentMain9001.class, args);
    }
}
  1. Business class
@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/nacos/{id}")
    public String getPayment(@PathVariable("id") Integer id){
        return "nacos registry,serverPort: "+ serverPort+"\t id"+id;
    }
}
  1. test

visit: http://localhost:9001/payment/nacos/31

The Nacos console can see the following:

Conclusion: Nacos service registry + service provider 9001 are ok

In order to facilitate the later demonstration of load balancing of Nacos, we build 9002 according to 9001
Module name: cloudalibaba provider payment9002

2. Nacos based service consumers

  1. Create a new cloudalibaba-consumer-nacos-order83 module
  2. POM
<dependencies>
    <!--Introduce self defined api General package, you can use payment payment Entity-->
    <dependency>
        <groupId>com.kuang</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Why does nacos support load balancing?

  1. YML
server:
  port: 83

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

#The name of the micro service to be accessed by the consumer (successfully registered with the micro service provider of nacos). If the accessed service is configured here, the business class does not need to define constants
service-url:
  nacos-user-service: http://nacos-payment-provider
  1. Main startup class
@SpringBootApplication
@EnableDiscoveryClient
public class OrderNacosMain83 {

    public static void main(String[] args) {
        SpringApplication.run(OrderNacosMain83.class, args);
    }
}
  1. Business class
@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced   //When RestTemplate is combined with Ribbon for load balancing, you must add @ LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
@RestController
@Slf4j
public class OrderNacosController {

    /*
    Because service url.nacos-user-service is configured in yml,
    There is no need to define the constant to access the microservice name, but read it directly through boot
     */
    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Long id) {
        return restTemplate.getForObject(serverURL + "/payment/nacos/" + id, String.class);
    }
}
  1. test

visit: http://localhost:83/consumer/payment/nacos/31

The Nacos console is as follows:

Conclusion: 83 visit 9001 / 9002 and the polling load is OK.

If the following error is reported on the page after accessing:

Note: @ LoadBalanced must be added when RestTemplate is combined with Ribbon for load balancing

3. Service registry comparison


Conclusion: Nacos supports AP and CP modes.

Nacos service configuration center

1. Nacos as configuration center - basic configuration

  1. Create a new cloudalibaba config Nacos client3377 module
  2. POM
<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. YML

Nacos is the same as spring cloud config. During project initialization, it is necessary to obtain the configuration from the configuration center. After pulling the configuration, the project can be started normally.

The loading of configuration files in spring boot has priority, and bootstrap has priority over application

bootstrap file

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos service registry address
      config:
        server-addr: localhost:8848 #Nacos as configuration center address
        file-extension: yaml  #Specifies the configuration of yaml format
        group: DEV_GROUP
        #group: DEV_GROUP
        namespace: e69dbe0a-233d-4b73-a173-89e9e8a1d041

# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file.extension}
# nacos-config-client-dev.yaml

application file

spring:
  profiles:
    active: dev #development environment 
    #active: test #testing environment
    #active: info
  1. Main startup class
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain3377 {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigClientMain3377.class, args);
    }
}
  1. Business class
@RestController
@RefreshScope   //The spring cloud native annotation supports the dynamic refresh function of Nacos
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }
}

Note: the configuration is automatically updated through the spring cloud native annotation @ RefreshScope

  1. Adding configuration information rules to Nacos

Theory: the composition format of dataid in Nacos and the matching rules in SpringBoot configuration file.

In the Nacos Spring Cloud, the complete format of dataId is as follows( Official website description):

${prefix}-${spring.profiles.active}.${file-extension}

  • Prefix defaults to the value of spring.application.name, which can also be configured through the configuration item spring.cloud.nacos.config.prefix.
  • Spring.profiles.active is the profile corresponding to the current environment. For details, please refer to the Spring Boot document. Note: when spring.profiles.active is empty, the corresponding connector - will not exist, and the splicing format of dataId will become ${prefix}. ${file extension}
  • File exception is the data format of the configuration content, which can be configured through the configuration item spring.cloud.nacos.config.file extension. Currently, only properties and yaml types are supported.

Practical operation: our configuration here is as follows


Summary:

  1. test
  • Before startup, you need to have the corresponding yaml configuration file under the column of nacos client - Configuration Management - configuration management
  • Run the main startup class of cloud-config-nacos-client3377
  • Call the interface to view the configuration information: http://localhost:3377/config/info

Built in dynamic refresh:

Modify the yaml configuration file in Nacos, call the interface to view the configuration again, and you will find that the configuration has been refreshed

2. Nacos as configuration center - classified configuration

Question 1:
In actual development, usually a system will prepare dev development environment, test test environment and prod production environment.
How to ensure that the service can correctly read the configuration file of the corresponding environment on Nacos when the specified environment is started?
Question 2:
A large distributed microservice system will have a malicious microservice sub project,
Each microservice project has a corresponding development environment, test environment, advance environment
How to manage these micro service configurations?

1. Graphical management interface of naocs:

2. The relationship between namespace + group + dataid? Why is it so designed?

Default:

Namespace= public, Group=DEFAULT. _GROUP, the DEFAULT Cluster is DEFAULT

The default Namespace of Nacos is public, which is mainly used to realize isolation.
For example, we now have three environments: development, testing and production. We can create three namespaces. Different namespaces are isolated.

The default Group is default Group. Groups can divide different micro services into the same Group.

A Service is a micro Service. A Service can contain multiple clusters. The DEFAULT Cluster of Nacos is DEFAULT, and the Cluster is a virtual partition of the specified micro Service.
For example, for disaster recovery, Service microservices are deployed in Hangzhou computer room and Guangzhou computer room respectively,
At this time, a cluster name (HZ) can be given to the Service micro Service in Hangzhou computer room,
Give a cluster name (GZ) to the Service microservices in the Guangzhou computer room, and try to make the microservices in the same computer room call each other to improve performance.

3. Three loading configurations

1) DataID scheme

Specify spring.profile.active and the DataID of the configuration file to read different configurations in different environments

Default space + default grouping + new dev and test dataids


You can read configuration files in multiple environments through the spring.profile.active attribute

spring:
  profiles:
    active: dev #development environment 
    #active: test #testing environment

Test: http://localhost:3377/config/info

Conclusion: load whatever you configure. Here we configure test.

2) Group scheme

The environment difference is realized through groups. Here we create two new groups as follows:


Modify bootstrap + application:


Add a group configuration under config, which can be configured as DEV_GROUP or TEST_GROUP

3) Namespace scheme

  • Create a new Namespace for dev/test
  • Go back to service management - service list view
  • Fill in according to the domain name configuration
  • YML

Nacos cluster and persistence configuration

1. Introduction to the official website

Therefore, when open source, it is recommended that users put the list of all services under a vip and then hang it under a domain name

http://ip1:port/openAPI In the direct IP mode, the machine needs to modify the IP before it can be used.

http://SLB:port/openAPI Mount the SLB mode (the intranet SLB cannot be exposed to the public network to avoid security risks). You can directly connect to the SLB. The real ip of the server is hung below, which is not readable.

http://nacos.com:port/openAPI Domain name + SLB mode (intranet SLB, which can not be exposed to the public network to avoid security risks), good readability, convenient for changing ip, recommended mode.

The default Nacos uses an embedded database to store data. Therefore, if multiple Nacos nodes under the default configuration are started, the database has consistency problems.
In order to solve this problem, Nacos adopts centralized storage to support cluster deployment. At present, it only supports cluster deployment.

2. Nacos persistence configuration explanation

Nacos comes with embedded database derby by default: https://github.com/alibaba/nacos/blob/develop/config/pom.xml

Configuration steps for switching from derby to mysql: (take windows as an example)

  1. Find the sql script in the directory of nacos-server-1.1.4\nacos\conf

Copy the contents of the nacos-mysql.sql script to Navicat for execution.

  1. application.properties can be found in the nacos-server-1.1.4\nacos\conf directory

Copy the following code to the end of the file, and modify the following code according to your database name and password to add it to application.properties.

spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
  1. Start Nacos and you can see a new empty recording interface, which was previously recorded into derby

3. Nacos+MySQL production environment configuration for Linux

  1. Nacos Download for Linux

Download the Linux version through the official website Nacos. We know the following:

Therefore, it is estimated that 1 Nginx + 3 Nacos registries + 1 Mysql is required.

  • Download the nacos-server-1.1.4.tar.gz file
  • Upload Nacos to / opt under Linux
  • Unzip the. Tar.gz file: tar -zxvf nacos-server-1.1.4.tar.gz
  • Copy the extracted files to the folder you created: cp -r nacos /mynacos/
  1. Cluster configuration steps

1) mysql database configuration on Linux server

  • Or find the nacos-mysql.sql file in the nacos/conf folder
  • Paste a copy of Mysql on your Linux

2) application.properties configuration

  • The configuration is the same as that of windows

3) Cluster configuration of nacos on Linux server cluster.conf

  • Sort out the different service port numbers of three nacos clusters and set them as 3333, 4444 and 5555 respectively
  • Copy cluster.conf to prevent error correction: cp cluster.conf.example cluster.conf
  • Write three different port number configurations in cluster.conf

    Note: 127.0.0.1 is not required for this IP. It must be an IP recognized by the Linux command hostname -i

4) Edit the startup script startup.sh of Nacos so that it can accept different startup ends

Questions to consider:

There is startup.sh in the / mynacos/nacos/bin directory
Usually, the startup of the stand-alone version is. / startup.sh.

However, for cluster startup, we hope to start different nacos instances by passing different port numbers similar to the shell commands of other software.
Command:. / startup.sh -p 3333 indicates to start the nacos server instance with port number 3333, which is consistent with the cluster.conf configuration in the previous step.

Modified content:

Execution method:

5) Nginx is configured as a load balancer

  • Go to the nginx configuration file directory: under / usr/local/nginx/conf
  • Find the nginx.conf file and modify it
  • Start nginx service.

6) As of here, 1 Nginx+3 nacos registries + 1 mysql

Start three Naocs: start under / mynacos/nacos/bin.
View several Nacos to start: ps -ef|grep nacos|grep -v grep | wc -l
Start nginx: start it under / usr/local/ngix/sbin. / nginx -c /usr/local/nginx/conf/nginx.conf
Check whether nginx is started: ps -ef|grep nginx
Test access to Nacos via nginx: http://192.168.111.144:1111/nacos/#/login
Add a configuration in the configuration list of nacos, and click config in the mysql data_ You can see this record in the info table

4. In project testing

The microservice cloudalibaba provider payment9002 is started and registered in the nacos cluster

Modify the YML configuration file as follows:

server:
  port: 9002

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        #Configure Nacos address
        #server-addr: localhost:8848
        # Change to port 1111 of nginx for clustering
        server-addr: 192.168.144:1111

management:
  endpoints:
    web:
      exposure:
        include: '*'  #monitor

At this point, start 9002 and you can see it in nacos.

5. High availability summary

If there is harvest!!! I hope the old fellow will come to three links, praise, collect and forward.
It's not easy to create. Don't forget to point a praise, which can let more people see this article and encourage me to write a better blog

Tags: Spring Cloud Nacos

Posted on Sun, 19 Sep 2021 11:29:54 -0400 by Mordred