SpringCloudAlibaba - Nacos configured as a service center

Nacos as a distribution center - Basic Configuration

New cloudalibaba-config-nacos-client3377
dependency:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>pers.zhang.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-config-nacos-client3377</artifactId>

    <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>
        <!--web + actuator-->
        <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.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>

</project>

Configuration:

Nacos and SpringCloud-config as initialization during the project, to ensure that the distribution center start to configure pull, pull after successful configuration, in order to guarantee the normal startup items.

Springboot loading sequence is present in the configuration file priorities,bootstrap优先级高于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 #指定格式
          
# #{spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}

application.yml:

spring:
  profiles:
    active: dev #指定为开发环境

Start categories:

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

Controller:

@RestController
@RefreshScope //支持Nacos的动态刷新
public class ConfigClientController {
    @Value("${config.info}")
    String configInfo;
    
    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }
}

Nacos add configuration information

Official website:https://nacos.in/zh-cn/docs/quick-start-spring-cloud.html

Nacos matching rules:

Description: The reason why you need to configure spring.application.name, because it is part of the configuration management constitute Nacos dataId field.

In Nacos Spring Cloud, a full format dataId as follows:

${prefix}-${spring.profile.active}.${file-extension}
  • prefixThe default is spring.application.namethe value, you can also configure the item spring.cloud.nacos.config.prefixto configure.
  • spring.profile.activeThe current environment is the corresponding profile, details can refer to the Spring Boot documentation. Note: - spring.profile.activeis empty, the corresponding connector -will not present, dataIdsplicing into the format ${prefix}.${file-extension}
    - file-exetensionis arranged content data format configuration item can spring.cloud.nacos.config.file-extensionbe configured. Currently only supports propertiesand yamltypes.

The final format:{spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}

New configuration:
Here Insert Picture Description

test:

Start 3377 micro-services, go to: localhost:3377/config/infosuccessfully obtain configuration:
Here Insert Picture Description

nacos comes with dynamic refresh:

Nacos modify the configuration file for the version = 2, call interface again:
Here Insert Picture Description
Here Insert Picture Description

Nacos as a distribution center - Classification Configuration

Multi-multi-project management to solve environmental problems.

Configuration Management:
Here Insert Picture Description

public as the default namespace reserved.

Namespace + Group + Data ID of the relationship:

Similar inside Java package and class name, the outermost layer can be used to distinguish namespace deployment environment, to distinguish between two targets on the Group Data ID and logical.
Here Insert Picture Description
Default:Namespace=public, Group=DEFAULT_GROUP,默认Cluster是DEFAULT

Nacos default namespace is public, Namespace is mainly used to achieve isolation.

For example, now has three environments: development, test and production environment, we can create three Namespace, between different Namespace is isolated.

Group default DEFAULT_GROUP, Group different microstructures can be classified into the same packet service inside.

Service is micro service; a Service can contain multiple Cluster (cluster), Nacos default Cluster is DEFAULT, Cluster is a virtual division of the designated micro-services. For example, disaster recovery, the Service micro-services are deployed in the engine room and Hangzhou, Guangzhou room, then you can give room Service in Hangzhou micro-services from a cluster name (HZ), the Service to Guangzhou room service from a micro cluster name (GZ), also allows room with a king i service call each other, in order to improve performance.

Instance is an example of micro-services.

Reading specified spring.profile.active different configurations of Data ID and profile to make different environments

  • + + Default spatial default grouping and test two new Data ID dev

Dev New Configuration Data ID:
Here Insert Picture Description
New test Configuration Data ID:
Here Insert Picture Description
Here Insert Picture Description
In this case, the reading can be carried out by a multi-environment configuration file spring.profile.active properties:
Here Insert Picture Description
Here Insert Picture Description

group grouping scheme:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Configuration to an increase in group config. Or may be configured to DEV_GROUP TEST_GROUP.
Here Insert Picture Description
Here Insert Picture Description

namespace program:

New dev / test of Namespace:
Here Insert Picture Description
Here Insert Picture Description
according to domain configuration to fill:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Modify bootstrap.yml, add a namespace:
Here Insert Picture Description
application.yml:
Here Insert Picture Description

Here Insert Picture Description

Published 843 original articles · won praise 2259 · Views 290,000 +

Guess you like

Origin blog.csdn.net/cold___play/article/details/105005462