019 Build config configuration center

1 config configuration center

    1-1 pom dependencies

        It is enough to introduce spring-cloud-config-server here, spring-boot-starter-security is just to add an access verification to the configuration center, you can ignore this reference:

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
	</dependencies>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

    1-2 Configuration file application.yml

    When developing a project, the company will definitely have several sets of environments, such as production, testing, development, local, etc., so our configuration files should also correspond to each other. The following figure is an example: config-file is a remote git repository, test It is the first-level folder under the project. There are two folders under test to store the configuration files of the prd and dev environments, application.properties is the public configuration file, and config-test in config-test.properties is one of the microservices. Name, so that the common configuration of microservices is placed in application.properties, and the personalized configuration of microservices is in their respective configuration files. Note: Personalized configuration for a microservice overrides the same configuration in application.properties.

    The following configuration loads the configuration under the remote warehouse test/dev by default. If the password contains special characters, you can add the escape character "\", or directly enclose the password with single quotation marks " ' ", where the user name defaults to user, Password: 12345

spring:
  cloud:
    config:
      server:
        git:
          uri: https://code.aliyun.com/995586041/config-file.git
          searchPaths: test/dev
          username: [email protected]
          password: ',******'
          repos:
            prd:
              pattern: "*/prd*"
              uri: https://code.aliyun.com/995586041/config-file.git
              searchPaths: test/prd
              username: [email protected]
              password: \,******
            dev:
              pattern: "*/dev*"
              uri: https://code.aliyun.com/995586041/config-file.git
              searchPaths: test/dev
              username: [email protected]
              password: ',******'
server:
  port: 8888
security:
  user:
    password: ${CONFIG_SERVICE_PASSWORD:12345}

    1-3 Startup class

        Just add the @EnableConfigServer annotation

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigApplication.class, args);
	}
}

    1-4 visit

        http://localhost:8888/test/application, if the access password is set above, you will be prompted to enter the user name and password, fill in the relevant information for verification, and then you can see the relevant configuration information:

2 Test Configuration Center

    1-1 pom file

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

    1-2 Configuration file bootstrap.properties

        The configuration file here uses bootstrap.properties. When the project starts, bootstrap.properties will be loaded first and then application.properties. Here we specify prd, so that we can specify the configuration file. Note that the name of the microservice here is: config-test, will be used below

server.port = 8082
spring.profiles.active = prd
spring.application.name = config-test

spring.cloud.config.uri = http://127.0.0.1:8888

spring.cloud.config.fail-fast = true
spring.cloud.config.username = user
spring.cloud.config.password = 12345
spring.cloud.config.profile=prd

goldleaf.test01 = test01 ${spring.profiles.active} bootstrap.properties
goldleaf.test05 = test05 ${spring.profiles.active} bootstrap.properties
goldleaf.test06 = test06 ${spring.profiles.active} bootstrap.properties

    1-3 Startup class and test interface

        As you can see, we did not add any special annotations about the configuration center here, I just wrote a test interface

@SpringBootApplication
@RestController
public class UserApplication {

	@Value("${goldleaf.test01}")
	private String test01;

	@Value("${goldleaf.test02}")
	private String test02;

	@Value("${goldleaf.test03}")
	private String test03;

	@Value("${goldleaf.test05}")
	private String test05;

	@Value("${goldleaf.test06}")
	private String test06;
	
	public static void main(String[] args) {
		SpringApplication.run(UserApplication.class, args);
	}
	
	@GetMapping("/test")
	public String getConfig() {
		StringBuilder builder = new StringBuilder();
		builder.append("test01:" + test01 + "\r\n");
		builder.append("test02:" + test02 + "\r\n");
		builder.append("test03:" + test03 + "\r\n");
		builder.append("test05:" + test05 + "\r\n");
		builder.append("test06:" + test06 + "\r\n");
		return builder.toString();
	}
}

    1-4 Test

        Now we have three configuration files: bootstrap.properties in the project, application.properties in the remote and config-test.properties in the remote. Configuration instructions: 1). Test01, test02, and test03 are defined in the above three files, respectively, to show that the configurations in the three configuration files are effective; 2). Test05 is also defined in the above three files , used to illustrate the priority of the three files; 3). Test06 is defined in both bootstrap.properties and application.properties to determine the priority of the two. After starting the project, now let's check the results by accessing the test interface of config-test:

        

        test01-03, indicating that the configurations in the three files are loaded; test05 indicating that the personalized configuration of the microservice will overwrite the previous configuration, with the highest priority; test06 indicating that by default, the application.properties of the remote warehouse has a higher priority than bootstrap. properties

        Priority order: remote config-test > remote application.properties > local bootstrap.properties

3 Local override remote configuration

    Add the following configuration to the git repository of the remote repository:

# 允许本地配置覆盖远程配置
spring.cloud.config.allowOverride=true
spring.cloud.config.overrideNone=true
spring.cloud.config.overrideSystemProperties=false

    Then access the test interface: http://127.0.0.1:8082/test, test05 and test06 load the configuration information in the local bootstrap.properties:

4 Manually refresh the configuration

    During development, it is inevitable to change some configurations. If the service is republished every time the configuration is changed, it is a bit overwhelming, so we manually refresh the configuration after changing the configuration file.

    Add the spring-boot-starter-actuator dependency to the pom file on the non-config side:

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>

    After modifying the configuration file of the git repository, call the /refresh endpoint of the service by POST method, such as: http://127.0.0.1:8082/refresh This will return which configuration points have been modified:

    View Results:

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325690183&siteId=291194637