【springcloud】config 搭建 client 端
一、当前的项目
二、在github上再上传一个config-client.yml 文件
eureka:
client:
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka
---
server:
port: 8201
spring:
profiles: dev
application:
name: springcloud-config-client
eureka:
client:
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka
---
server:
port: 8202
spring:
profiles: test
application:
name: springcloud-config-client
eureka:
client:
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka
三、搭建 client 端
1. pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<junit.version>4.12</junit.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 将微服务provider侧注册进eureka -->
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<!--spring cloud的依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 什么是bootstrap.yml
application.yml 是用户级的资源配置项。
bootstrap.yml 是系统级的,优先级更高。
springcloud 会创建一个Bootstrap Context ,作为Spring应用的Application context 的父上下文。初始化的时候,BootStrap Context 负责从外部资源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment 。Bootstarp 属性有高优先级,默认情况下,它们不会本地覆盖。Boogstrap context 和 Application context 有着不同的约定,所以新增了一个 bootstrap.yml,保证了Bootstrap context 和 Application Context 配置的分离。
3. bootstrap.yml
spring:
cloud:
config:
name: spring-config-client #需要从github上读取的资源名称,注意没有yml后缀名
profile: test #本次访问的配置项
label: master
uri: http://www.config3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
4. application.yml
spring:
application:
name: springcloud-config-client # 没啥用 写点儿东西留个念想
5. controller
@RestController
public class ConfigClientController {
@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);
return str;
}
}
6. 启动类不用增加东西,默认的就好。
四、启动测试
1. 启动 config3344 的 config 服务器项目。
访问 http://www.config3344.com:3344/application-dev.yml
2. 启动 config-client-3355 的 config 客户端项目。
访问 http://www.cfgclient.com:8201/config