[SpringCloudAlibaba] Nacos service registration and configuration center cooperates with nginx load

Overview

Nacos: Dynamic Naming and Configuration Service
Nacos = Eureka+Config +Bus
Nacos is a combination of registration center + configuration center,
a dynamic service discovery, configuration management and service management platform that makes it easier to build cloud native applications.

Official website
https://github.com/alibaba/Nacos
https://nacos.io/zh-cn/index.html
https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring -cloud-alibaba.html#_spring_cloud_alibaba_nacos_discovery

Registration center

POM

parent module

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

This module

<!--SpringCloud ailibaba nacos -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

YML

server:
  port: 9001

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

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

Startup class

@EnableDiscoveryClient

CAP

C means that the data seen by all nodes at the same time is consistent; and the definition of A is that all requests will receive responses.

When do you choose which mode to use?
Generally speaking,
if there is no need to store service level information and the service instance is registered through nacos-client and can maintain heartbeat reporting, then you can choose AP mode. Current mainstream services such as Spring cloud and Dubbo services are all suitable for AP mode. AP mode weakens consistency for the sake of service possibility, so AP mode only supports registration of temporary instances.

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

curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP'

Configuration center

POM

<!--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>

YML

Nacos is the same as springcloud-config. When initializing the project, you must first pull the configuration from the configuration center. Only
after pulling the configuration can the normal startup of the project be guaranteed.
There is a priority order for loading configuration files in springboot, bootstrap has higher priority than application

#bootstrap
# nacos配置
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格式的配置
spring:
  profiles:
    active: dev # 表示开发环境

Startup class

@EnableDiscoveryClient

ConfigClientController

@RefreshScope //在控制器类加入@RefreshScope注解使当前类下的配置支持Nacos的动态刷新功能。
public class ConfigClientController
{
    
    
    @Value("${config.info}")
    private String configInfo;

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

Matching rules in Nacos

Insert image description here

${
    
    spring.application.name}-${
    
    spring.profiles.active}.${
    
    spring.cloud.nacos.config.file-extension}

Three options for loading configuration

  1. DataID scheme
  2. Group plan
  3. Namespace scheme

The relationship between the three

Insert image description here

Example

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格式的配置
        group: DEV_GROUP
        namespace: 7d8f0f5a-6a53-4785-9686-dd460158e5d4

application.yml

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

Startup class

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

Cluster deployment

Overview

Official website:
https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
Official website architecture diagram The actual mode
Insert image description here
comes with the embedded database derby by default
https://github.com/alibaba/nacos /blob/develop/config/pom.xml

Deployment mode

  1. Standalone mode - for testing
  2. Cluster Mode - High Availability
  3. Multi-cluster mode - multiple data centers

Modify derby to mysql

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

Configuration

cluster.conf

Insert image description here
Insert image description here
Insert image description here
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
Insert image description here
Insert image description here

Edit Nacos startup script startup.sh so that it can accept different startup ports

./startup.sh -p 3333 means starting the nacos server instance with port number 3333, which is consistent with the cluster.conf configuration in the previous step.
Insert image description here
Insert image description here

Nginx configuration, using it as a load balancer

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;
        }
        .......省略

Insert image description here
As of now, 1 Nginx + 3 nacos registration centers + 1 mysql

test

yml

server:
  port: 9002

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        #配置Nacos地址
        #server-addr: localhost:8848
        # 换成nginx的1111端口,做集群
        server-addr: 192.168.111.144:1111


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

Insert image description here

Guess you like

Origin blog.csdn.net/qq_45742250/article/details/132408891