Spring cloud 23. The Config client obtains the configuration on GitHub through the Config server.

Spring cloud config client configuration and testing

① Create a new file in the local D: \ yunweigongchengshi \ microservicecloud config path
microservicecloud-config-client.yml.

 

 

 

② microservicecloud-config-client.yml content.

microservicecloud-config-client.yml.

spring:
  profiles:
    active:
    - dev
---
server: 
  port: 8201 
spring:
  profiles: dev
  application: 
    name: microservicecloud-config-client
eureka: 
  client: 
    service-url: 
      defaultZone: http://eureka-dev.com:7001/eureka/   
---
server: 
  port: 8202 
spring:
  profiles: test
  application: 
    name: microservicecloud-config-client
eureka: 
  client: 
    service-url: 
      defaultZone: http://eureka-test.com:7001/eureka/
 

Then save as UTF-8.

 

 

③ Commit the previous step to GitHub.

Difference between git add. And git add *
Git add. Will add all local untrack ed files to the staging area, and will filter according to. gitignore, but git add * will ignore. gitignore to add any files

1,git add .

2. git commit -m "microservicecloud-config-client.yml commit" the staging area is committed to the local workspace.

3. git push origin master uploads the local warehouse to github

Then look at GitHub to see if the remote library content changes.

 

 

 

④ Create a new sub module microservicecloud-config-client-3355.

⑤ pom file of sub module microservicecloud-config-client-3355.

The modified pom file of the sub module microservicecloud-config-client-3355 is as follows:

   <!-- SpringCloud Config Client -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency> 

 

 

The complete contents of the pom file of the sub module microservicecloud-config-client-3355 are as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.lss.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microservicecloud-config-client-3355</artifactId>
	<dependencies>
		<!-- SpringCloud Config Client -->
		<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>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<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-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>

Then update it.

 

⑥ Create bootstrap.yml.

What is bootstrap.yml:

applicaiton.yml is a user level resource configuration item
bootstrap.yml is system level with higher priority

Spring Cloud will create a 'Bootstrap context' as the parent context of spring application's' Application Context '. During initialization, 'Bootstrap context' is responsible for loading configuration properties from external sources and parsing configuration. The two contexts share an 'Environment' obtained from the outside. `Bootstrap attributes have high priority. By default, they are not overwritten by local configuration. `Bootstrap context and Application Context have different conventions,
So a new 'bootstrap.yml' file is added to ensure the separation of 'Bootstrap Context' and 'Application Context' configuration.

 

 

The complete contents of bootstrap.yml are:

spring:
  cloud:
    config:
      name: microservicecloud-config-client #The resource name that needs to be read from github, note that there is no yml suffix
      profile: dev   #Configuration items accessed this time
      label: master   
      uri: http://config-3344.com:3344 ා after starting the microservice, go to the 3344 service first, and get the service address of GitHub through SpringCloudConfig

 

 

 

 

⑦ Create application.yml.

The complete content of application.yml is:

spring:
  application:
    name: microservicecloud-config-client

 

 

 

⑧ Modify the hosts file and add the mapping under windows. 127.0.0.1 client-config.com

 

⑨ Create a new ConfigClientRest class to verify that the configuration can be read from GitHub.

The complete contents of ConfigClientRest.java are:

package com.lss.springcloud.rest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientRest {

  @Value("${spring.application.name}")
  private String applicationName;
  
  @Value("${eureka.client.service-url.defaultZone}")
  private String eurekaServers;
  
  @Value("${server.port}")
  private String port;
  
  @RequestMapping("/config")
  public String getConfig()
  {
   String str = "applicationName: "+applicationName+"\t eurekaServers:"+eurekaServers+"\t port: "+port;
   System.out.println("******str: "+ str);
   return "applicationName: "+applicationName+"\t eurekaServers:"+eurekaServers+"\t port: "+port;
  }
}
 

 

 

 

 

⑩ Create the main startup class configclient? Startspringcloudapp.java.

The complete contents of configclient ﹣ startspringcloudapp.java are:

package com.lss.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp {

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

}

 

 

 

 

Test.

Start Config configuration center 3344 microservice and self test: http://config-3344.com:3344/application-dev.yml

Start 3355 to prepare access as Client

What is the profile value in bootstrap.yml and what to read from github

If it is profile: dev at present

The default port of dev on github is 8201

http://client-config.com:8201/config

 

 

 

 

If it is profile: test at present

test the default port on github is 8202

http://client-config.com:8202/config

 

Published 155 original articles, won praise and 10000 visitors+
Private letter follow

Tags: Spring github git Maven

Posted on Sun, 16 Feb 2020 23:09:55 -0800 by mark_18