• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Using spring cloud config to configure server control configuration

King Wang

1 月 3, 2022

Use Spring Cloud Config Configuration server control configuration

The code involved in this article is in config-test in :https://github.com/Alexaccele/SpringCloudDemo

Spring Cloud Config Introduce

Spring Cloud Config yes Sping-Cloud Components for distributed configuration management , Divided into two roles Config-Server and Config-Client;

Config-Server End ( The configuration server ) Centralized storage / Manage profiles , And provide external interface convenience Config-Client visit , Interface to use HTTP To provide access to the outside world ;Config-Server Storage / Managed profiles can come from local files , long-range Git Warehouse and remote Svn Warehouse

Config-Client( Various microservices that need to obtain configuration files ) Get the configuration file through the interface , Then you can use it in your application ;

Spring Cloud The configuration server is based on REST Applications for , It’s not a stand-alone server , Developers can choose to embed it into existing springboot In the application , You can also start a new one in the server where it is embedded springboot project

structure Spring Cloud Configure server ————Config-Server

Add dependency

First of all to springcloud Configure the server to add maven rely on

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

About pom File details can be found at the beginning of the article GitHub Find .

Create a bootstrap class

by springcloud Configure the server to create a bootstrap class ConfigServerApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}

@EnableConfigServer Annotations make the application a spring cloud config The server

Add configuration file application.yml

spring:
application:
name: config-server
profiles:
active:
native
cloud:
config:
server:
native:
search-locations: classpath:configs/,configs/config-client
server:
port: 8888

It is pointed out here that the name of the service is config-server, But this kind of configuration should be written to bootstrap.yml In file , Because it will not affect the test function, it is written in application.yml in . The specific reasons will be explained later .

Specifies the configuration file as the file system , Read from the configuration server locally .

Then it points out that the read location of the local configuration file is classpath:configs/,configs/config-client

Finally, configure the default listening port of the server 8888

from Git The warehouse reads the configuration file remotely

If required Git As a remote repository of configuration files, the configuration files can be changed application.yml The corresponding property is changed to

spring.cloud.config.server.git.uri=https://github.com/XXX/XXX # Indicates the corresponding Git Repository's URL
spring.cloud.config.server.git.searchPaths=config-client # Express Git Find the path to the configuration file in the
#Git Username and password , If it's public Git The warehouse can be empty , If private Git The warehouse must fill in
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=

Get ready config-client Profile to use

In the local resources Create the directory involved in the previous step configs, stay configs Create a file config-client-dev.yml, The contents are as follows

test: "This is a spring cloud config test"

This is for testing , So define a configuration property , Will be for config-client In preparation for injection of attributes .
Similar configuration files can have config-client.yml / config-client-pro.yml / config-client-test.yml

Start configuration server

Now you can start the configuration server , You can use it from the command line in the directory where the project is located mvn spring-boot:run function .
At this time, you can view the configuration file information in a variety of ways in the browser , for example http://localhost:8888/config-client/dev, The results are as follows , It means success .

The supported viewing methods are as follows :

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
  • application Corresponding to the name of the corresponding service , This case refers to config-client, The following article creates config-client When you serve bootstrap.yml The document states ;
  • profile A configuration file indicating which environment to use , This could be dev,test,pro, Even default It means the one mentioned above config-client.yml file , There is no suffix for the environment ;
  • label It’s an optional tag ,git Warehouse default value master,svn The default value of the warehouse is trunk;

config-client End of the building

Introduce dependencies

In the new config-client Project pom Add the following dependencies to the file

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

create profile bootstrap.yml

spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8888
fail-fast: true
profiles:
active: dev
server:
port: 8080

In the configuration, the name of the application and the path of the configuration file are pointed out respectively , And the environment of the configuration file is dev Development environment . That is, corresponding to the types described above . Open listening port 8080

Notice the spring.application.name Properties and spring.cloud.config.uri/fail-fast Attribute configuration is written in bootstrap.yml It’s normative in the document , If the spring.cloud.config.uri/fail-fast Attribute write application.yml In file , Will cause the project to report errors during the start-up process , The reason is that the corresponding configuration file cannot be found , Here’s the above config-client-dev.yml.

write in bootstrap.yml Why

take spring.application.name Properties and spring.cloud.config.uri Attribute configuration is written in bootstrap.yml The reason for the file is during startup , load bootstrap.yml The order of the documents should take precedence over application.yml.

For detailed reasons, please refer to this article :https://www.cnblogs.com/BlogNetSpace/p/8469033.html

stay config-client Add API Interface

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ConfigClientApplication {
@Value("${test}")
String test;
@GetMapping("/test")
public String test(){
return test;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}

@Value Read… In the configuration file test attribute , That is to say, the above is in config-server Configuration file in config-client-dev.yml Medium test attribute .

test

You can see the results returned in the browser , Consistent with the content of the configuration file we want to read .

This is it. Spring Cloud Config A simple demonstration of , The complete separation of the configuration file from the application is achieved by just a few simple steps , The role of this in actual production is enormous .

Personal blog address :https://alexaccele.github.io/

发表回复