当前位置:网站首页>Actual combat of Nacos configuration center, standard components of Pangu microservice development

Actual combat of Nacos configuration center, standard components of Pangu microservice development

2022-06-09 11:31:00 InfoQ

The configuration center is a standard component for distributed microservice development , There are many successful typical applications in the industry , Such as : Ctrip  Apollo  Distributed configuration center 、 Baidu  Disconf  Distributed configuration center, etc . The Pangu development framework configuration center is based on Alibaba  
Nacos
  Provide dynamic configuration services .

Given the readability of the document , Pangu tutorial and reference examples both use the local configuration method . This article will introduce how to enable Pangu applications to obtain the ability of dynamic configuration services based on the configuration center . in application , If there are no special requirements, we generally recommend using the configuration center to develop .

Configuration center introduction

Nacos  Dynamically configuring services allows you to centralize 、 Manage application configuration and service configuration of all environments in an external and dynamic way . Dynamic configuration eliminates the need to redeploy applications and services when configuration changes , Make configuration management more efficient and agile . Configuration centralized management makes it easier to implement stateless Services , Make it easier for services to scale elastically on demand . It also provides a simple and easy to use  UI  Help you manage the configuration of all services and Applications , Including configuration version tracking 、 Release of canary 、 A series of out of the box configuration management features such as one click rollback configuration , Help you manage configuration changes more safely in the production environment and reduce the risk of configuration changes .

  • Fast  QA: In the previous article  Nacos  For service registration , Why is the configuration center the same ?
  • Nacos  Is to build in “ service ” Modern application architecture centered  ( For example, the microservices paradigm 、 Cloud native paradigm )  Service infrastructure . Commit to discovering 、 Configure and manage microservices , It perfectly integrates the configuration center and the service registry . therefore ,Nacos  It is not only a service registry, but also a fully functional distributed configuration center .
  • Fast  QA: Can the development mode of single layered architecture also be configured using the configuration center ?
  • The configuration center is a strongly recommended mandatory standard component in the distributed microservice architecture development environment . But if you are developing on a single layered architecture , The configuration center can also be used . For these basic abilities , Whether it's a microservice or an individual , Pangu frames are perfectly fitted , Just rely on  
    pangu-spring-boot-starter
      You can use it out of the box .

Explain the related terms

Namespace
Used for tenant granularity configuration isolation . Under different namespaces , There can be the same  Group  or  Data ID  Configuration of .Namespace  One of the most common scenarios is the separation of configurations between different environments , For example, develop the resources of test environment and production environment ( Such as configuration 、 service ) Isolation etc. .
Configuration Management
System configuration editing 、 Storage 、 distribution 、 Change management 、 Historical version management 、 All configuration related activities such as change audit .
Configuration item
A specific configurable parameter and its value range , Usually, the  param-key=param-value  There is a form of . For example, we often configure the log output level of the system ( logLevel=INFO|WARN|ERROR ) It's a configuration item .
config set
A set of related or unrelated configuration items is called a configuration set . In the system , A configuration file is usually a configuration set , Including all aspects of the system configuration . for example , A configuration set may contain data sources 、 Thread pool 、 Log level and other configuration items .
config set ID
Nacos  Of a configuration set in  ID. config set  ID  It is one of the dimensions of organization division and configuration . A system or application can contain multiple configuration sets , Each configuration set can be identified by a meaningful name .( such as : Use the app name as  Data ID)
Configure snapshot
Nacos  The client of  SDK  A snapshot of the configuration will be generated locally . When the client cannot connect to  Nacos Server  when , You can use the configuration snapshot to display the overall disaster recovery capability of the system . The configuration snapshot is similar to  Git  Local in  commit, It's also similar to caching , Will be updated at the right time , But there is no cache expiration ( expiration ) The concept of .

Compare the local configuration with the configuration center

  • Local configuration ( The configuration file )
  • The configuration is decentralized 、 Coupling with application 、 Static configuration
  • No environment isolation, no version support , Easy to cause production accidents
  • No safety audit
  • Configuration center
  • Configuration set 、 Externalization 、 Dynamic and effective in real time
  • Multi environment isolation and multi version support , Safer
  • Configure permission control 、 Operation change audit

Actual combat center configuration

Here is an example of using the configuration center . Other examples based on local configuration can refer to this example and change to dynamic configuration based on configuration center .

Install relevant Pangu modules

  • Pangu  Parent

<parent>
 <groupId>com.gitee.pulanos.pangu</groupId>
 <artifactId>pangu-parent</artifactId>
 <version>latest.version.xxx</version>
 <relativePath/>
</parent>

  • Basic module

<dependency>
 <groupId>com.gitee.pulanos.pangu</groupId>
 <artifactId>pangu-spring-boot-starter</artifactId>
</dependency>

Local configuration

A local configuration file is also required for configuration based on the configuration center , But this configuration file is in a fixed template format . It is used to configure some basic class parameters related to data communication in the configuration center . As shown below .

spring.application.name=pangu-examples-config-remote-nacos
spring.profiles.active=${spring.profiles.active:dev}
nacos.config.bootstrap.enable=true
nacos.config.bootstrap.log-enable=true
nacos.config.auto-refresh=true
nacos.config.namespace=${nacos.namespace:pangu-dev}
nacos.config.server-addr=${nacos.server-addr:127.0.0.1:8848}
#nacos.config.type=yaml
nacos.config.type=properties
nacos.config.data-id=${spring.application.name}
Key configuration item description
  • nacos.config.auto-refresh
  • After the configuration of the configuration center is changed, it will be automatically refreshed to the configuration client
  • nacos.config.namespace
  • Namespace ( Note that it corresponds to the namespace  ID  value ), It is recommended to use namespaces to distinguish deployment environments
  • nacos.config.server-addr
  • Configure the communication address of the center
  • nacos.config.type
  • Configuration format used by configuration center ( properties、yaml  etc. )
  • nacos.config.data-id
  • Unique identification of a configuration set in the configuration center

Configuration center configuration

After the above local configuration , Our application can synchronize data with the configuration center . Now create the corresponding namespace and the same name in the configuration center according to the local configuration information  data-id  Configuration set of .

  • Fast  QA: How the client works with  Nacos  Configure data synchronization in the center ?
  • Client pass  http  Long polling mechanism pull  nacos server  The configuration data of the terminal , The configuration snapshot will be created locally ( disaster ). During the long polling timeout ,nacos server  If the configuration of the terminal changes , Will actively write the configuration to  response  And back to , Simulated “ push ” effect . Therefore, the client can perceive the configuration changes of the configuration center in real time .
Create a namespace
stay  Nacos  Management system 『 Namespace 』 Menu new  ID  by  
pangu-dev
  The namespace of ( this  ID  These are the above configuration items  nacos.config.namespace  Value ). As shown in the figure below .

Create configuration set
Multiple sets of configuration sets can be created under one namespace , Each configuration set consists of  data-id  To uniquely identify . stay  Nacos  Management system 『 Configuration Management - Configuration list 』 Menu new  data-id  The value is  
pangu-examples-config-remote-nacos
  Configuration set of . As shown in the figure below .

  • Configuration set list


  • newly added / Edit configuration set

Starting entrance

@SpringBootApplication
public class NacosConfigurationApplication {
 public static void main(String[] args) {
 PanGuApplicationBuilder.init(NacosConfigurationApplication.class).run(args);
 }
}

Use configuration

thus , We can use... In applications  
@NacosValue
  Annotation to get configuration items . As shown in the following code .

/**
 *  Turn on  autoRefreshed Configuration item ,  It can realize the dynamic refresh of parameters
 */
@NacosValue(value = &quot;${demo.app.id}&quot;)
private String appId;
@NacosValue(value = &quot;${demo.app.name}&quot;, autoRefreshed = true)
private String appName;
@NacosValue(value = &quot;${demo.app.author}&quot;, autoRefreshed = true)
private String appAuthor;

  • Fast  QA: The example demonstrates only a few simple variable parameters , Can the connection configuration information of middleware such as database be placed in the configuration center ?
  • Of course you can , Dry is finished .

Colored eggs : Log level hot switching based on configuration center

To enhance the observability of log output , Pangu basic module realizes the log level hot switching capability based on the configuration center . You only need to modify or add log level configuration in the configuration center , The corresponding log output level will take effect immediately .

# journal
logging.level.root=INFO
logging.level.com.gitee.pulanos.pangu=INFO

This article related example source code

  • pangu-examples-config-remote-nacos
    : Configuration center reference example
  • pangu-examples-log-dynamic
    : Reference example of log level hot switching based on configuration center

Colored eggs

  • Pangu open source homepage
  • Pangu development documents
原网站

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