SpringCloud twenty-three, Config configuration on the client obtained through GitHub Config server.

SpringCloud Config client configuration and test

① local D: \ yunweigongchengshi \ New File under microservicecloud-config path
microservicecloud-config-client.yml.

 

 

 

②microservicecloud-config-client.yml 内容.

The complete contents of 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 it as UTF-8 format.

 

 

③ will be submitted to the GitHub in the previous step.

git add. git add * and the difference between
git add. untrack all local files will have joined the staging area, and will do filtering based on .gitignore, but git add * will ignore any files are added to the .gitignore

1、git add .

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

3, git push origin master the local repository to upload to github

And then view the GitHub find remote database content changes.

 

 

 

④ New submodule microservicecloud-config-client-3355.

⑤ sub-module microservicecloud-config-client-3355's pom files.

Pom modify the contents of the file sub-module microservicecloud-config-client-3355 are:


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

 

 

Pom entire contents of the file sub-module microservicecloud-config-client-3355 are:

<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客户端 -->
		<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 bootstrap.yml is:

applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高

Spring Cloud会创建一个`Bootstrap Context`,作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootstrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。`Bootstrap`属性有高优先级,默认情况下,它们不会被本地配置覆盖。 `Bootstrap context`和`Application Context`有着不同的约定,
所以新增了一个`bootstrap.yml`文件,保证`Bootstrap Context`和`Application Context`配置的分离。

 

 

bootstrap.yml的完整内容是:

spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
      profile: dev   #本次访问的配置项
      label: master   
      uri: http://config-3344.com:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

 

 

 

 

⑦创建application.yml。

application.yml的完整内容是:

spring:
  application:
    name: microservicecloud-config-client

 

 

 

⑧windows下修改hosts文件,增加映射。127.0.0.1 client-config.com

 

⑨新建ConfigClientRest类,验证是否能从GitHub上读取配置。

ConfigClientRest.java的完整内容是:

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;
  }
}
 

 

 

 

 

⑩创建主启动类ConfigClient_3355_StartSpringCloudApp.java。

ConfigClient_3355_StartSpringCloudApp.java的完整内容是:

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);
	}

}

 

 

 

 

⑪测试。

启动Config配置中心3344微服务并自测:http://config-3344.com:3344/application-dev.yml

启动3355作为Client准备访问

bootstrap.yml里面的profile值是什么,决定从github上读取什么

假如目前是 profile: dev

dev默认在github上对应的端口就是8201

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

 

 

 

 

假如目前是 profile: test

test默认在github上对应的端口就是8202

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

 

发布了155 篇原创文章 · 获赞 1 · 访问量 1万+

Guess you like

Origin blog.csdn.net/lbh19630726/article/details/104356034