config-client配置
同config-server一样,在pom.xml的dependencies节点中新增spring-cloud-starter-eureka依赖,用来注册服务:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
在bootstrap.properties中,按如下配置:
server.port=7002 #对应前配置文件中的{application}部分 spring.application.name=ghghspace eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/#参数设置为true,开启通过服务来访问Config Server的功能 spring.cloud.config.discovery.enabled=true #对应前配置文件中的{profile}部分 spring.cloud.config.profile=dev #对应前配置文件的git分支 spring.cloud.config.label=master #配置中心的访问地址 spring.cloud.config.uri=http://localhost:7001/
通过eureka.client.serviceUrl.defaultZone参数指定服务注册中心,用于服务的注册与发现,再将spring.cloud.config.discovery.enabled参数设置为true,开启通过服务来访问Config Server的功能
在应用主类中,增加@EnableDiscoveryClient注解,用来发现config-server服务,利用其来加载应用配置
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
沿用之前我们创建的Controller来加载Git中的配置信息
@RefreshScope @RestController public
