本文共 2245 字,大约阅读时间需要 7 分钟。
用服务的方式来实现
ConfigAppApplication.java
package com.packtpub.ConfigApp;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.config.server.EnableConfigServer;@EnableDiscoveryClient@EnableConfigServer@SpringBootApplicationpublic class ConfigAppApplication { public static void main(String[] args) { new SpringApplicationBuilder(ConfigAppApplication.class).web(true).run(args); }} application.properties
spring.application.name=config-serverserver.port=7001eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/spring.profiles.active=nativespring.cloud.config.server.native.searchLocations=file:D:/temp/
ConfigClientApplication.java
package com.packtpub.ConfigClient;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient@SpringBootApplicationpublic class ConfigClientApplication { public static void main(String[] args) { new SpringApplicationBuilder(ConfigClientApplication.class).web(true).run(args); }}
bootstrap.properties
spring.application.name=didispaceserver.port=7002eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/spring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=config-serverspring.cloud.config.profile=test#spring.cloud.config.label=master#spring.cloud.config.uri=http://localhost:7001/
TestController.java
package com.packtpub.ConfigClient;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RefreshScope@RestControllerpublic class TestController { @Value("${from}") private String from; @RequestMapping("/from") public String from() { return this.from; }}
转载地址:http://vuzvx.baihongyu.com/