SpringCloud Alibaba Nacos

Chapter 1 Spring Cloud Alibaba

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-hqABLGvR-1680350855533)(assets/image-20220131130504896.png)]

1.1 Overview

Spring Cloud AlibabaCommitted to providing a one-stop solution for microservice development, including the necessary components for developing distributed application microservices, just add some annotations and a small amount of configuration, you can connect the Spring Cloud application to the Ali microservice solution, through the Ali middleware software to quickly build distributed application systems.

On October 31, 2018, Spring Cloud Alibaba officially entered the Spring Cloud official incubator and released the first version in the Maven Central Library.

Why does SpringCloud alibaba appear?

Spring Cloud NetflixThe project has entered the maintenance phase, that is, SpringCloud Netflix will no longer develop new components in the future, and focus on maintenance.

Reference link:

https://spring.io/blog/2018/12/12/spring-cloud-greenwich-rc1-available-now

Learning materials:

Official website: https://spring.io/projects/spring-cloud-alibaba#overview

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

English: https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html

1.2 Main functions

1) Service current limit downgrade

By default, it supports WebServlet , WebFlux , OpenFeign , RestTemplate , Spring Cloud Gateway , Zuul , Dubbo , and RocketMQ . You can modify the current limiting and downgrading rules in real time through the console at runtime, and also supports viewing the current limiting and downgrading Metrics (indicators) monitoring.

2) Service registration and discovery

It adapts to Spring Cloud service registration and discovery standards, and integrates Ribbon support by default.

3) Distributed configuration management

Supports externalized configuration in distributed systems, and automatically refreshes when configuration changes.

4) Message-driven capability

Build message-driven capabilities for microservice applications based on Spring Cloud Stream .

5) Distributed transactions

Use the @GlobalTransactional annotation to solve distributed transaction problems efficiently and with zero intrusion into the business.

6) Alibaba Cloud Object Storage

Alibaba Cloud provides massive, secure, low-cost, and highly reliable cloud storage services. Supports storing and accessing any type of data in any application, anytime, anywhere.

7) Distributed task scheduling

Provides second-level, accurate, highly reliable, and highly available timing ( 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 even distribution of sea quantum tasks to all Workers (schedulerx-client) for execution.

8) Alibaba Cloud SMS service

SMS services covering the world, friendly, efficient, and intelligent interconnected communication capabilities help companies quickly build customer access channels.

1.3 Components

Sentinel : Taking traffic as an entry point, it protects the stability of services from multiple dimensions such as traffic control, circuit breaker degradation, and system load protection.

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

RocketMQ : An open source distributed messaging system, based on highly available distributed cluster technology, provides low-latency, highly reliable message publishing and subscription services.

Dubbo : Apache Dubbo™ is a high-performance Java RPC framework.

Seata : Alibaba's open source product, an easy-to-use high-performance microservice distributed transaction solution.

Alibaba Cloud OSS : Alibaba Cloud Object Storage Service (OSS for short) 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, anywhere.

Alibaba Cloud SchedulerX : A distributed task scheduling product developed by the Alibaba middleware team, which provides second-level, accurate, highly reliable, and highly available scheduled (based on Cron expression) task scheduling services.

Alibaba Cloud SMS : SMS service covering the world, friendly, efficient, and intelligent interconnected communication capabilities, helping enterprises quickly build customer access channels.

Chapter 2 SpringCloud Alibaba Nacos Service Registration and Configuration Center

2.1 Introduction to Nacos

Nacos: Dynamic Naming and Configuration Service, the first four letters are the first two letters of Naming and Configuration, and the last s is Service.

A dynamic service discovery, configuration management and service management center that is easier to build cloud-native applications. Nacos is a combination of registration center + configuration center , equivalent to Eureka+Config+Bus

Official website: https://github.com/alibaba/Nacos

Official document: https://nacos.io/zh-cn/index.html

Comparison of various registries:
insert image description here

2.2 Install Nacos

[Step 1] Download Nacos from the official website

[Step 2] Unzip the installation package and directly run startup.cmd in the bin directory

[Step 3] After the command runs successfully, visit http://localhost:8848/nacos directly . The default account and password are both nacos

success page

insert image description here

2.3 Nacos as a service registration center

Premise: add spring-cloud-alibaba-dependenciesdependencies to the parent project pom file

<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  <version>2.1.0.RELEASE</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>

1. Service Provider

Requirement: Register the service provider to the registry

[Step 1] Create a new Module

cloudalibaba-provider-payment9001

[Step 2] Create a pom file

<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency> 
<!--
         spring-boot-starter-web
         spring-boot-starter-actuator
         spring-boot-devtools
         lombok
         spring-boot-starter-test
-->
</dependencies>

NOTE: The load balancing function is enabled by default.

insert image description here

[Step 3] Write yml

server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址

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

[Step 4] Write the main startup class@EnableDiscoveryClient

@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain9001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(PaymentMain9001.class,args);
    }
}

[Step 5] Write business classes

@RestController
public class PaymentController
{
    
    
    @Value("${server.port}")
    private String serverPort;
    
    @GetMapping(value = "/payment/nacos/{id}")
    public String getPayment(@PathVariable("id") Integer id)
    {
    
    
        return "nacos registry, serverPort: "+ serverPort+"\t id"+id;
    }
}

[Step 6] Test

Test address 1: http://localhost:8848/nacos/ , service list

Test address 2: http://localhost:9001/payment/nacos/1

insert image description here

In order to demonstrate the load balancing of nacos, refer to 9001 to create a new 9002. After the creation is successful, nacos-payment-providerthere are two instances under the service

insert image description here

You can also implement multiple service providers by copying the virtual port mapping, 【Run】→【Edit Configurations】

insert image description here

2. Serving consumers

Requirement: Register the service consumer with Nacos, and realize the remote call of the consumer to the provider through the RestTemplate, and finally test the load balancing

[Step 1] Create a new Module

cloudalibaba-consumer-nacos-order83

[Step 2] Create a pom file

<dependencies>
    <!--SpringCloud ailibaba nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.example.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--
         spring-boot-starter-web
         spring-boot-starter-actuator
         spring-boot-devtools
         lombok
         spring-boot-starter-test
   -->
</dependencies>

[Step 3] Write yml

server:
  port: 83

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
service-url:
  nacos-user-service: http://nacos-payment-provider

[Step 4] Write the main startup class

@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain83
{
    
    
    public static void main(String[] args)
    {
    
    
        SpringApplication.run(OrderNacosMain83.class,args);
    }
}

[Step 5] Write business classes

ApplicationContextConfig.java

@Configuration
public class ApplicationContextConfig {
    
    
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
    
    
        return new RestTemplate();
    }
}

OrderNacosController.java

@RestController
public class OrderNacosController {
    
    
    @Autowired
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

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

[Step 6] Test

Check the nacos service registration center to see if the consumer service is registered

insert image description here

Test address: http://localhost:83/consumer/nacos/13 , 83 visits 9001|9002, the polling load is OK

insert image description here

3. Comparison of service registration centers

insert image description here

Service Registration and Discovery Framework CAP model console management Community activity
Eureka AP support Low (2.x version closed source)
Zookeeper CP not support middle
Consul CP support high
Nacos AP、CP support high

AP and CP selection

Generally speaking, if you do not need to store service level information and the service instance is registered through nacos-client and can keep heartbeat reporting, then you can choose the AP mode. The current mainstream services, such as Spring Cloud and Dubbo services, are suitable for the AP mode. The AP mode weakens the consistency for the availability of the service. Therefore, the AP mode only supports the registration of temporary instances.

If you need to edit or store configuration information at the service level, then CP is required, and K8S service and DNS service are suitable for CP mode. In CP mode, registration of persistent instances is supported. At this time, the Raft protocol is used as the cluster operation mode. In this mode, the service must be registered before registering the instance. If the service does not exist, an error will be returned.

2.4 Nacos as a service configuration center

The role of the configuration center: Multiple clusters read configuration files from the nacos remote configuration center to avoid repetitive modification of configuration files.

insert image description here

1. Nacos basic configuration

Reference document: https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

Configuration formula:

${prefix}-${spring.profiles.active}.${file-extension}
/**
说明:
(1)prefix默认为spring.application.name的值
(2)spring.profile.active既为当前环境对应的profile,可以通过配置项spring.profile.active 来配置
(3)file-exetension为配置内容的数据格式,可以通过配置项spring.cloud.nacos.config.file-extension配置
**/

Project combat

[Step 1] Create a new Module

cloudalibaba-config-nacos-client3377

[Step 2] Create a pom file

<dependencies>
    <!--nacos-config-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <!--nacos-discovery-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--
         spring-boot-starter-web
         spring-boot-starter-actuator
         spring-boot-devtools
         lombok
         spring-boot-starter-test
    -->
</dependencies>

[Step 3] Write yml

Nacos is the same as springcloud-config. When the project is initialized, the configuration must be pulled from the configuration center first. After the configuration is pulled, the normal start of the project can be guaranteed. The loading of configuration files in springboot has a priority order, and bootstrap has a higher priority than application

bootstrap.yml

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置

application.yml

spring:
  profiles:
    active: dev # 表示开发环境

[Step 4] Write the main startup class

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain3377 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(NacosConfigClientMain3377.class, args);
    }
}

[Step 5] Write business classes

@RestController
@RefreshScope  
public class ConfigClientController {
    
    
    @Value("${config.info}")
    private String configInfo;

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

@RefreshScope: SpringCloud’s native annotations enable the configuration under the current class to support the dynamic refresh function of Nacos.

[Step 6] Test

Interface address: http://localhost:9996/config/info

insert image description here

NOTE: Nacos comes with 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 classification configuration

Problem introduction:

In the actual development process, usually a large-scale distributed microservice system will have many microservice subprojects, and each subproject has three environments: dev (development environment), test (test environment), prod (production environment), so how to ensure When the system starts in the specified environment, can each microservice service correctly read the configuration file in the corresponding environment of the Nacos remote configuration center? Multi-environment and multi-project management

Namespace (namespace) + Group (grouping) + Data ID (specific configuration instance)

Default: Namespace= public, Group= DEFAULT_GROUP, default Cluster isDEFAULT

1) Namespace: Namespace, by default public, is mainly used to implement isolated environments, such as: development, testing, and production environments. Three Namespaces are established, and different Namespaces are isolated.

2) Group: Grouping. By default DEFAULT_GROUP, Group can divide different microservices into the same group. Service is a microservice, and a Service can contain multiple Clusters.

The default Cluster of Nacos is DEFAULT, and Cluster is a virtual division of specified microservices. For example, for disaster recovery, the Service
microservices are deployed in the Hangzhou computer room. At this time, a cluster name (HZ) can be given to the Service microservice in the Hangzhou computer room, and a cluster name (HZ) can be given to the Service
microservice in the Guangzhou computer room. Try to make microservices in the same computer room call each other to improve performance. Finally, Instance is an instance of microservices.

insert image description here

Three schemes to load configuration

1) DataID scheme

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

NOTE: Default space + default grouping + new dev and test two DataIDs

insert image description here

Modified cloudalibaba-config-nacos-client3377_application.yml

spring:
  profiles:
    active: test #测试环境

test:

insert image description here

2) Group plan

Create DEV_GROUP, DEV_GROUP groups, create each groupnacos-config-client-info.yaml

insert image description here

cloudalibaba-config-nacos-client3377Modify the configuration file application.yml,bootstrap.yml

application.yml

spring:
  profiles:
    active: info

bootstrap.yml

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置
        group: DEV_GROUP

test:

insert image description here

3) Namespace scheme

Target a specific configuration file for a specific group under a specific namespace

[Step 1] Click on the namespace to create three new namespaces: dev, test, and prod

insert image description here

[Step 2] Click the configuration list to create configuration files under the dev and test namespaces

insert image description here

insert image description here

[Step 3] Modify cloudalibaba-config-nacos-client3377the configuration file application.yml, bootstrap.yml.

application.yml

spring:
  profiles:
    #active: test #测试环境
    active: dev # 表示开发环境

bootstrap.yml

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置
        group: DEV_GROUP
        namespace: ab0c7cf2-bffa-4f41-972f-0d210265d7bd  #dev

[Step 4] Test

Modify namespace, group and profiles, test

insert image description here

3. Nacos cluster and persistent configuration

insert image description here

Nacos supports three deployment modes

1) Stand-alone mode - for testing and stand-alone trial.

2) Cluster mode - used in production environments to ensure high availability.

3) Multi-cluster mode - for multi-data center scenarios.

Cluster deployment reference document: https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html

Nacos persistent configuration

By default, Nacos uses its own embedded database derby to store data. Therefore, if multiple Nacos nodes with default configurations are started, there will be consistency problems in data storage.

In order to solve this problem, Nacos adopts a centralized storage method to support cluster deployment, and currently only supports MySQL storage.

Specific operation steps:

1) Install the database, version requirements: 5.6.5+
2) Initialize the mysql database, database initialization file: mysql-schema.sql
3) Modify conf/application.propertiesthe file, add support for mysql data source configuration (currently only supports mysql), add the url, user name and password of the mysql data source .

Refer to the official website: https://nacos.io/zh-cn/docs/deployment.html

Derby to mysql switching configuration steps

[Step 1] nacos-server-1.1.4\nacos\confFind the sql script [ nacos-mysql.sql] in the directory and execute the script

[Step 2] nacos-server-1.1.4\nacos\confFind in the directory and application.properties insert image description here
add the following code

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=123456

[Step 3] Start Nacos, you can see a brand new empty record interface, which used to be recorded in derby

insert image description here
add namespace, group,Data ID

insert image description here

View mysql database, config_info table

insert image description here

Nacos cluster configuration

Linux version Nginx+Nacos+Mysqlproduction environment configuration, including 1 Nginx, 3 Nacos registration centers, and 1 mysql.

Nacos cluster configuration reference document: https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html

Prepare environment

1) 64 bit OS Linux/Unix/Mac, Linux system is recommended.

2) Mysql has been installed

3) Nginx has been installed

4) Only 3 or more Nacos nodes can form a cluster.

[Step 1] Download and install the Linux version of nacos

Download address: https://github.com/alibaba/nacos/releases/tag/1.1.4 nacos-server-1.1.4.tar.gz

#解压
tar -zxvf nacos-server-1.1.4.tar.gz

[Step 2] MySQL database configuration on Linux server

Enter the config directory,

insert image description here

Start mysql, nacos_mysql.sqlcreate nacos_configa database and table according to the file, and the creation results are as follows

insert image description here

[Step 3] application.propertiesConfiguration

vim application.properties

Configure the following content

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=123456

[Step 4] Nacos cluster configuration cluster.conf on the Linux server

copy cluster.conf, modify cluster.conffile

cp cluster.conf.example cluster.conf
vim cluster.conf

Add the following

#########nacos cluster############
192.168.10.130:3333
192.168.10.130:4444
192.168.10.130:5555

NOTE: This IP cannot be written as 127.0.0.1, it must be an IP that can be recognized by the Linux command hostname -i, where ip is the ens33 ip address

hostname -i

insert image description here

[Step 5] Edit the startup script of Nacos startup.shso that it can accept different startup ports

1)单机版的启动,命令 ./startup.sh
2)集群启动,类似其它软件的shell命令,传递不同的端口号启动不同的nacos实例。
命令:./startup.sh -p 3333 表示启动端口号为3333的nacos服务器实例,和上一步的cluster.conf配置的一致。

Modify bin/startup.shthe file, enter: set nu, display the number of lines

vim startup.sh

insert image description here

After modification,

while getopts ":m:f:s:p:" opt
do
    case $opt in
        m)
            MODE=$OPTARG;;
        f)
            FUNCTION_MODE=$OPTARG;;
        s)
            SERVER=$OPTARG;;
        p)
            PORT=$OPTARG;;
        ?)
        echo "Unknown parameter"
        exit 1;;
    esac
done

nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &

The nacos cluster starts,

./startup.sh -p 3333

insert image description here

[Step 6] Configure Ngnix as a load balancer

nginx installation

#1、下载
wget http://nginx.org/download/nginx-1.22.0.tar.gz
#2、解压
tar -xvf nginx-1.22.0.tar.gz
#3、切换到新建的nginx目录
#4、执行命令 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
#5、执行make命令
make
#6、执行make install命令
make install
#7、进入 /usr/local/nginx目录
cd /usr/local/nginx

Enter /usr/local/nginx/configthe directory and modify the configuration file of nginxnginx.conf

#gzip  on;
      upstream cluster{
    
    
         server 127.0.0.1:3333;
         server 127.0.0.1:4444;
         server 127.0.0.1:5555;
     }
     server {
    
    
         listen       1111;
         server_name  localhost;
         #charset koi8-r;
         #access_log  logs/host.access.log  main;      
         location / {
    
    
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://cluster;
         }
       }

Enter /usr/local/nginx/sbinthe directory and start nginx

./nginx -c /usr/local/nginx/conf/nginx.conf

[Step 7] Test

Prerequisite: start nacos cluster, start nginx

Test 1: Persistence

Access nacos through nginx, test address: http://192.168.10.130:1111/nacos/#/login , enter the nacos login page

Add 1 DataID,

insert image description here

Check the mysql database on linux to see if the tablenacos_config records in the database are added successfully.config_info

insert image description here

Test 2: nacos service registration and discovery

cloudalibaba-provider-payment9002Requirement: Register microservices into the nacos cluster

Modify cloudalibaba-provider-payment9002microservicesapplication.yml

server:
  port: 9002

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        #server-addr: localhost:8848 #配置Nacos地址
        server-addr: 192.168.10.130:1111   #linux下nginx的ip

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

Start payment9002, visit: http://192.168.10.130:1111/nacos/#/login

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44490884/article/details/129902225