在生产环境中,我们可能会将Config Server 与 Eureka等注册中心联合使用(注
意:目前Spring Cloud只支持与Eureka及Consul联合使用,不支持与Zookeeper联
合使用),下面讲解如何将Config Server与 Eureka 联合使用。
准备工作
microservice-discovery-eureka ;
microservice-config-clienteureka-dev.properties 。
服务器端代码示例:
首先新建一个Maven项目,在 pom.xml 中添加如下内容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht
tp://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m
aven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<artifactId>microservice-config-server-eurekaartifactId>
<packaging>jarpackaging>
<parent>
<groupId>com.itmuch.cloudgroupId>
<artifactId>spring-cloud-microservice-studyartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
dependencies>
project>
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerEurekaApplication.class, args);
}
}
server:
port: 8050
spring:
application:
name: microservice-config-server-eureka
cloud:
config:
server:
git:
uri: https://github.com/eacdy/spring-cloud-study/
search-paths: config-repo
username:
password:
eureka:
client:
serviceUrl:
defaultZone: http://discovery:8761/eureka/
客户端示例
创建一个Maven项目,在 pom.xml 中添加如下内容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht
tp://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m
aven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<artifactId>microservice-config-client-eurekaartifactId>
<packaging>jarpackaging>
<parent>
<groupId>com.itmuch.cloudgroupId>
<artifactId>spring-cloud-microservice-studyartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
project>
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientEurekaApplication.class, args);
}
}
编写测试Controller
/**
* 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
* @author eacdy
*/
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/hello")
public String hello() {
return this.profile;
}
}
配置文件:application.yml
server:
port: 8051
配置文件:bootstrap.yml
spring:
application:
name: microservice-config-client-eureka
cloud:
config:
profile: dev
label: master
discovery:
enabled: true # 默认false,设为true表示使用注册中心中的configserver配置而不自己配置configserver的uri
serviceId: microservice-config-server-eureka # 指定config server在服务发现中的serviceId,默认为:configserver
eureka:
client:
serviceUrl:
defaultZone: http://discovery:8761/eureka/
# 参考文档:https://github.com/spring-cloud/spring-cloud-config/bl
#ob/master/docs/src/main/asciidoc/spring-cloud-config.adoc#discov
#ery-first-bootstrap
从示例代码我们发现,想要将Config Server 与 注册中心联合使用,只需要在客户
端侧配置 spring.cloud.config.discovery.enabled=true
和
spring.cloud.config.discovery.serviceId 两个配置项即可。Eureka的配置
前文有讲到过,如有疑问,详见服务发现的相关章节。
注意:当服务发现是 Eureka
及 Consu
l 时,Config Server
支持与之联合使
用;如果是 Zookeeper
做服务发现,目前不支持与之联合使用。
注意点:
client需要添加以下依赖,否则访问/refresh将会得到404:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
client的controller需要添加@RefreshScope注解,否则配置无法刷新。
本文的 bootstrap.yml 文件中的内容不能放到 application.yml 中,否则
config部分无法被加载,因为config部分的配置先于 application.yml 被加
载,而 bootstrap.yml 中的配置会先于 application.yml 加载,
Config Server也可以支持本地存储或svn而不使用git,相对较为简单,故而本
文不作赘述,有兴趣的可以自行阅读Spring Cloud的文档。
参考文档:
Config Server与注册中心联合使用:https://github.com/spring-cloud/springcloudconfig/blob/master/docs/src/main/asciidoc/spring-cloudconfig.adoc#discovery-first-bootstrap
Config Server的高可用: https://github.com/spring-cloud/spring-cloudconfig/issues/87