About Nacos Spring Cloud Please refer to :Nacos Config and Nacos Discovery.

adopt Nacos Server and spring-cloud-starter-alibaba-nacos-config Realize the dynamic change of configuration .

adopt Nacos Server and spring-cloud-starter-alibaba-nacos-discovery Implement service registration and discovery .

Prerequisite
First, download and install a nacos server, Then start nacos server. Download and install process view https://www.cnblogs.com/ying-z/p/14630738.html
 
The new configuration
There are two ways to create a new configuration .
Mode one : visit Nacos Server The page is created manually .
Log in first Nacos Server, The address is generally http://127.0.0.1:8848/nacos/index.html.
Then go to the configuration list , Click the new configuration button , Enter the new configuration page .

Be careful :

Here I create Data Id by nacos-config.properties Configuration file for , among Group Is the default DEFAULT_GROUP, The format of the configuration file is also selected accordingly Properties, Add configuration to configuration content nacos.config=hello_nacos, As shown in the figure .

Mode two : By calling Nacos Open API towards Nacos Server Release configuration :dataId by nacos-config.properties, The content is nacos.config=hello_nacos

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config.properties&group=DEFAULT_GROUP&content=nacos.config=hello_nacos"

Create service applications

(1) Use SpringBoot, Create a file called nacos-config The sub project of .

(2)pom.xml Add the following dependencies to the file .

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<!-- Statement Spring cloud The version is Hoxton.SR3-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Statement Spring cloud alibaba Version is 2.1.0.RELEASE -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Statement nacos-discovery rely on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Statement nacos-config rely on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>

remarks : If you don't need to register the service , It can also be removed nacos-discovery rely on .

Be careful : edition 2.1.x.RELEASE  The corresponding is Spring Boot 2.1.x edition . edition  2.0.x.RELEASE  The corresponding is Spring Boot 2.0.x edition , edition  1.5.x.RELEASE  The corresponding is Spring Boot 1.5.x edition .

Refer to for more version correspondence : Version Description Wiki

(3) modify application.properties Add related configuration

server.port=9002
spring.application.name=nacos-config
spring.application.cloud.nacos.discovery.server-addr=127.0.0.1:8848

(4) create profile , The file named bootstrap.properties. And in bootstrap.properties Middle configuration Nacos server Address and application name of .

Be careful : The file name is bootstrap.xxx, instead of application Or other . because Nacos Same as Spring Cloud 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.

The profile type here can be selected according to personal habits , When I use it here properties type , The configuration is as follows :

# The service name 
spring.application.name=nacos-config
# Specify the address of the registry . If you don't need to sign up for the service , You can also remove this , And delete discovery rely on .
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# Specify the address of the configuration center .
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# Specify the format of the configuration file in the configuration center .
#spring.cloud.nacos.config.file-extension=properties

Why configuration is needed spring.application.name, It's because it's made up of Nacos Configuration Management dataId Part of the field .

stay Nacos-Server Configuration Management , among Data ID It's defined by : 
${prefix}-${spring.profile.active}.${file-extension}

prefix: The default is spring.application.name Value , You can also configure items through spring.cloud.nacos.config.prefix To configure the .

spring.profile.active: That is, the current environment corresponds to profile, For details, please refer to  Spring Boot file . You can use the configuration item spring.profile.active To configure the . When spring.profile.active It's empty time , Corresponding connector - There will be no ,dataId The splicing format of becomes ${prefix}.${file-extension}.

file-exetension: To configure the data format of the content , You can use the configuration item spring.cloud.nacos.config.file-extension To configure the . Currently only supported properties and yaml type .

(5) Create external interfaces ConfigController, from nacos Middle read configuration .

@RefreshScope
@RestController
public class ConfigController {
@Value("${nacos.config}")
private String configValue; @RequestMapping("/config/get")
public String get() {
return configValue;
}
}

@RefreshScope:Spring Cloud The original annotation of , The configuration under the current class can support dynamic update .

@Value: adopt @Value annotation , Read out key by nacos.config The value of the configuration , And pass /config/get Interface to return .

The work for this code part has been completed .

Start the service for testing

(1) Guarantee Nacos-Server Has been launched .

(2) Check Nacos-Server Whether the corresponding configuration file has been added to the configuration list in .

(3) start-up nacos-config project .

(4) After the successful startup, in Nacos You can see our registered services in the console .

(5) Call the interface to test http://127.0.0.1:9002/config/get, You can see the return result .

This indicates that the configuration has been read successfully .

(6) Below I will Nacos-Server The configuration on is modified to hello_lars, See if you can dynamically update . modify Nacos-Server After configuration on , Refresh http://127.0.0.1:9002/config/get page , The result is updated to a new configuration .

You can see me through Nacos-server The console for configuration changes , Client services nacos-config Heat update accordingly .

SpringCloud+Nacos Realize the service configuration center (Hoxton edition ) More articles about

  1. Spring Cloud introduction -Nacos Implement registration and configuration center (Hoxton edition )

    List of articles Abstract Nacos brief introduction Use Nacos As a registry Install and run Nacos Create app registration to Nacos Load balancing function Use Nacos As configuration center establish nacos-config-client modular stay ...

  2. Spring Cloud Alibaba series ( Two )nacos As a service configuration center

    Nacos For storing configuration and other metadata key/value Storage , Provide server-side and client-side support for external configuration in distributed systems . Use Spring Cloud Alibaba Nacos Config, You can ...

  3. Nacos note 01—— Use Nacos As SpringCloud The project's service registry

    Preface Just learned SpringCloud When using eureka As a service registry , With Netflix eureka2.x No more updates , And the recent internship in the company SpringCloud The project is to use Nacos To be a service registry , therefore ...

  4. 【 Microservices 】 A third : Starting from scratch , Easy to handle SpringCloud Microservices - Configuration center

    In the whole microservice system , In addition to the importance of a registry , There's also a registry . Registry serves as an important carrier to manage the configuration files and dynamic parameters of the whole project group .Spring Cloud In the subprojects of the system ,Spring Clou ...

  5. SpringCloud Series of distributed configuration center quick introduction and practice

    SpringCloud Series of distributed configuration center quick introduction and practice @ Catalog 1. Introduction to distributed configuration center 2. What is? SpringCloud Config? 3. Example experimental environment preparation 4.Config Server Code reality ...

  6. SpringCloud Learning Series 4 ----- Configuration center (Config) The use of,

    Preface This article mainly introduces SpringCloud Distributed configuration center in (SpringCloud Config) Related to the use of tutorials . SpringCloud Config Config Introduce Spring Clou ...

  7. SpringCloud04 Service configuration center 、 The message bus 、 Remote configuration dynamic refresh

    1 Environmental statement JDK:1.8 MAVENT:3.5 SpringBoot:2.0.5.RELEASE SpringCloud:Finchley.SR1 2 Create service registry (Eureka Server side ) explain ...

  8. The actual combat of micro service configuration center :Spring + MyBatis + Druid + Nacos

    In discussing service discovery and configuration in combination with scenarios, we talked about Nacos Three typical application scenarios of configuration center , Including how to be in Spring Boot Use in Nacos The configuration center controls the database connection information , And in the “ Native ” Of Spring ...

  9. SpringCloud Service configuration center

    SpringCloud Config brief introduction Spring Cloud Config yes Spring Cloud A new project created by the team , It is used to provide centralized external configuration support for infrastructure and microservice applications in distributed systems ...

  10. Use Alibaba Of Nacos As SpringCloud Registration and configuration center for , And combine Sentinel+Nocos Dynamic current limiting and fusing

    I'm learning Ali's recently Nacos Components and Sentinel Components , I've made a little demo. Git Address :https://github.com/yangzhilong/nacos-client Anyone who is interested can ...

Random recommendation

  1. log4j Relative path not found , processing method

    http://blog.csdn.net/u012345283/article/details/40821833?utm_source=tuicool&utm_medium=referral

  2. javaScript In the use of ActiveXObject To create FileSystemObject Operation file

    notes : If you use javascript Read local files , Security issues .  It needs to be set in the browser , as follows :     Tools —> Internet Options -> Security -> Custom level -> Enable “ There is no sign of security A ...

  3. HDFS Basic shell operation ,hadoop fs Operation command

    (1) distributed file system With more and more data , There's no room in an operating system , Then allocate to more operating system managed disks , But it's not easy to manage and maintain , So there is an urgent need for a system to manage files on multiple machines , This is distributed file management ...

  4. pure css Implement the carousel

    The implementation principle of carousel graph is relatively simple for instance <div class="main"> <div class="div-main"></ ...

  5. About security :(XSS,csrf,cors,jsonp, The same-origin policy )

    About security :(XSS,csrf,cors,jsonp, The same-origin policy ) Ajax It's a way to get data from the server without refreshing the page . Its core object is XHR, The homologous strategy is ajax It's a kind of constraint , It sets the same protocol for communication ...

  6. Learn safety from scratch ( Forty )● Upload files MIME Type bypasses vulnerability defense

    MIME Detection principle Server side MIME Type detection is done by checking http Bag Content-Type Field to determine whether the upload file is legal . php Sample code : if($_FILES['userfile']['type' ...

  7. mysql Structure interpretation ~mysql Multi source replication of

    One Scenario requirements Multi source copy version 5.7, The target host 5.6.21 4 individual DB Some databases of the machine need data summary for query Two To set up   1 Export the corresponding destination library      mysqldump -uuser -ppass ...

  8. 【Spark】SparkStreaming-Checkpoint- Fault tolerance

    SparkStreaming-Checkpoint- Fault tolerance Spark Streaming How to use checkpoint Fault tolerance - CSDN Blog spark/JavaRecoverableNetworkWo ...

  9. Uniformity Hash Algorithm principle and C# Code implementation

    One . Uniformity Hash Algorithm principle Basic concepts Consistent hashes organize the entire hash space into a virtual ring , Suppose a hash function H The value space of is 0-2^32-1( That is, the hash value is a 32 Bit unsigned shaping ), The whole hash space loop is as follows : The whole space is in order ...

  10. Python The number of data types

    Numbers ( The number ) Integers :123 (int type ) Floating point numbers : 0.25( A number with a decimal point is a floating-point number ,Float type ) Boolean value :False,True( namely 0 and 1,bool type ) The plural ( No information available ,complex type ) whole ...