一、构建Config Client
1.创建一个spring-boot项目,取名为microservicecloud-config-client其pom.xml:
<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/maven-v4_0_0.xsd">
<parent>
<artifactId>mall</artifactId>
<groupId>com.linjia</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-config-client</artifactId>
<packaging>maven-plugin</packaging>
<name>microservicecloud-config-client</name>
<url>http://maven.apache.org</url>
<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>
2.bootstrap.yml 配置
spring:
cloud:
config:
name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名,https://github.com/yangliuwilow/springcloud-config目录下的文件
profile: dev #本次访问的配置项
label: master
uri: http://localhost:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
#application.yml 是用户级别的资源配置,
#bootstarp.yml 是系统级别的,优先级别更高;
3.application.yml 配置
spring:
application:
name: microservicecloud-config-client
4.在github springcloud-config中创建 microservicecloud-config-client.yml配置文件(和bootstrap.yml中spring.cloud.config.name对应)
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev #开发环境
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://springCloudEureka7001.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test #测试环境
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://springCloudEureka7001.com:7001/eureka/test
5.启动类
package com.linjia.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);
}
}
6.创建Controller,获取配置信息
package com.linjia.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 ConfigClientRestController
{
@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;
}
}
启动,config-server(上一篇文章),config-client
访问:http://localhost:8201/config
applicationName: microservicecloud-config-client eurekaServers:http://springCloudEureka7001.com:7001/eureka/ port: 8201
修改
bootstrap.yml 配置中
profile: test
访问:http://localhost:8202/config