1.设置git
https://git.oschina.net/rigid/hr.git
上传如下文件:
hr_config/my-client.yml
hr_config/my-client-uat.yml
2.增加config server
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class SpringCloudServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudServerApplication.class, args); } }
启动之后访问:
http://localhost:8888/my-client/master
3.增加config client
package com.example.demo; 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; @RestController @RefreshScope public class MySampleRestController { @Value("${my-config.appName}") private String appName; @RequestMapping("/app-name") public String getAppName() { return appName; } }
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class SpringCloudClientApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudClientApplication.class, args); } @Autowired void setEnvironment(Environment env) { System.out.println("my-config.appName from env: " + env.getProperty("my-config.appName")); } }
#本地访问
http://localhost:8080/app-name
#刷新
curl -X POST http://localhost:8080/refresh
时间: 2024-11-06 09:37:22