当前位置:网站首页>Actual combat of Nacos as service configuration center

Actual combat of Nacos as service configuration center

2022-04-23 18:39:00 Hua Weiyun

@toc

1、Nacos As configuration center - Basic configuration

1.1 newly build cloudalibaba-config-nacos-client3377 modular

image-20220415005518008

1.2 pom.xml

 <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>        <!-- General basic configuration -->        <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.2 YML file

1.2.1 Why configure two yml file

  Nacos Same as springcloud-config equally , At project initialization , Make sure to pull the configuration from the configuration center first ,
After pulling the configuration , To ensure the normal start of the project .

  springboot There is a priority order for the loading of configuration files in ,bootstrap Priority over application.

1.2.2 bootstrap.yml

# nacos To configure server:  port: 3377spring:  application:    name: nacos-config-client  cloud:    nacos:      discovery:        server-addr: localhost:8848 #Nacos Address of service registration center       config:        server-addr: localhost:8848 #Nacos As the configuration center address         file-extension: yaml # Appoint yaml Format configuration   # ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}

1.2.3 application.yml

 spring:  profiles:    active: dev #  Represents the development environment 

1.3 Main startup class

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

1.4 Business class

  ConfigClientController

@RestController@RefreshScope // Add... In the controller class @RefreshScope Annotation enables the configuration under the current class to support Nacos Dynamic refresh of .public class ConfigClientController{    @Value("${config.info}")    private String configInfo;    @GetMapping("/config/info")    public String getConfigInfo() {        return configInfo;    }}

   By using Spring Cloud Primary annotation @RefreshScope Implement automatic configuration update .

1.5 stay Nacos Add configuration information to

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

image-20220415010204841

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

   We added a new configuration :

image-20220415010545203

image-20220415010552491

   The correspondence between the configuration in our current program and the configuration center file is as follows :

image-20220415010637208

1.6 test

   Before starting, you need to be in nacos client - Configuration Management - There is a corresponding... Under the configuration management column yaml The configuration file

   function cloud-config-nacos-client3377 The main startup class of

   Call the interface to view the configuration information :http://localhost:3377/config/info

image-20220415010844111

   It does use the information in the remote configuration center .

   Test dynamic refresh , Let's modify the configuration center first version=4

image-20220415010948780

   visit :http://localhost:3377/config/info, Found that the configuration has been updated , Than before Spring Cloud Congfig+eureka_Spring Cloud Bus The configuration of is much more convenient .

image-20220415011005942

2、Nacos As configuration center - Classification configuration

2.1 What's wrong with the above configuration ?

   problem 1:

   In development , Usually a system will prepare dev development environment ,test Test environment ,prod Production environment . How to ensure that the service can correctly read Nacos The configuration file of the corresponding environment ?

   problem 2:

   A large-scale distributed microservice system will have many microservice subprojects , Each microservice project will have a corresponding development environment 、 Test environment 、 Pretest environment 、 Formal environment … How to manage these micro service configurations ?

2.2 Nacos Graphical management interface of

   Configuration management interface :

image-20220415011253500

   Namespace interface :

image-20220415011305109

2.3 Namespace+Group+Data ID The relationship between the three ? Why is it designed this way? ?

2.3.1 What is it? ?

   similar Java Inside package And class name
   The outermost namespace Can be used to differentiate deployment environments ,Group and DataID Logically distinguish between two target objects .

2.3.2 The relationship between the three

image-20220415011504958

  ==Namespace=public,Group=DEFAULT_GROUP, Default Cluster yes DEFAULT==

  Nacos The default namespace is public,Namespace It is mainly used to realize isolation .

   For example, we now have three environments : Development 、 test 、 Production environment , We can create three Namespace, Different Namespace It's isolated .

  Group The default is DEFAULT_GROUP,Group Different micro services can be divided into the same group

  Service Micro services ; One Service It can contain more than one Cluster( colony ),Nacos Default Cluster yes DEFAULT,Cluster Is a virtual partition of a specified microservice .

   For example, for disaster recovery , take Service Microservices are deployed in Hangzhou computer room and Guangzhou computer room respectively , At this time, you can give it to Hangzhou computer room Service Microservice has a cluster name (HZ), For Guangzhou machine room Service Microservice has a cluster name (GZ), We can also make micro services in the same computer room call each other as much as possible , To improve performance .

   And finally Instance, This is an example of microservice .

2.4 Three schemes load configuration

2.4.1 DataID programme

   Appoint spring.profile.active And configuration files DataID To read different configurations in different environments

   Default space + Default group + newly build dev and test Two DataID.

   newly build dev To configure DataID

image-20220415011738702

   newly build test To configure DataID

image-20220415011816411

image-20220415011826972

   test

   First use dev Environmental testing

image-20220415011931203

   visit :http://localhost:3377/config/info

image-20220415011944418

   Reuse test Environmental testing

image-20220415012137069

   visit :http://localhost:3377/config/info

image-20220415012204625

2.4.2 Group programme

   adopt Group Achieve environmental differentiation , newly build Group

image-20220415012315515

   stay nacos Create a new configuration file on the GUI console DataID

image-20220415012348705

  bootstrap.yml

   stay config Add one more group Can be configured . It can be configured as DEV_GROUP or TEST_GROUP

image-20220415015634237

  application.yml

image-20220415015841440

   Access test :http://localhost:3377/config/info

image-20220415015947877

2.4.3 Namespace programme

   newly build dev/test Of Namespace, Note the following namespace ID:

image-20220415020032389

   Back to service management - Service list view

image-20220415020109138

   stay dev Namespace 3 Configuration items

image-20220415020209074

  bootstrap.yml, Here it is namespace Property to configure the namespace ID

image-20220415020250672

  application.yml

image-20220415020435218

   The above configuration represents that what we want to access here is dev Under the namespace ( The namespace configured above ID yes dev Of ),TEST_GROUP In group nacos-config.client-dev.yaml The configuration file .

   visit :http://localhost:3377/config/info

image-20220415020512860

   You can see , It is consistent with our analysis .

   Here we are ,Nacos As the configuration center, that's all , Another article will be written after cluster construction , In the middle of the night 2 O 'clock , The liver doesn't move .

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231836021833.html