springcloud-config从git获取配置文件
配置文件git地址:https://github.com/1147549577/springCloud
1. springcloud-config-server
- maven包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 将@EnableConfigServer添加至启动类
- application.yml文件,我有使用Eureka。
server:
port: 1001
eureka:
client:
healthcheck:
enabled: true
service-url:
defaultZone: http://localhost:1000/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
appname: springcloud-config-server
spring:
application:
name: springcloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/1147549577/springCloud
searchPaths: '{application}'
'{application}'
的意思为 Eureka中注册的各个ID,即配置文件中 spring.application.name
名称。记得加’号,不然访问有问题的哦!
而searchPaths: '{application}'
这个的意思是:访问https://github.com/1147549577/springCloud这个项目下的命为 '{application}'
的文件夹。当然你也可以不使用'{application}'
占位符,如searchPaths: development
。
仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:
/{application}/{profile}[/{label}]
匹配规则:
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
访问:http://localhost:1001/随便写({application})/随便写({profile}) 会返回一个json文件,就表示成功了。
如http://localhost:1001/springcloud-test-server/dev会返回 springcloud-test-server-dev.yml(因为我就创了一个.yml文件)的json文件。
{"name":"springcloud-test-server","profiles":["dev"],"label":null,"version":"93c651cb0f70844a2b64a1cce8ffc2f826095fa3","state":null,"propertySources":[{"name":"https://github.com/1147549577/springCloud/springcloud-test-server/springcloud-test-server-dev.yml","source":{"server.port":8001,"foo":1234}}]}
当然 http://localhost:1001/随便写/随便写 这个访问,也会获取到json文件就是了,只是propertySources里没有东西罢了。
反正只要就返回json文件访问git就是成功的。
2. springcloud-config-client
- maven包,一定记得写啊,我就是漏了,导致无法获取到值。如果正常导入,idea中在编写bootstrap.yml的时候ALT+/能快速补全单词。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 在application.properties下新增一个bootstrap.yml文件,springcloud加载顺序是先加载bootstrap,然后加载application。
server:
port: 1996
spring:
application:
name: springcloud-test-server
cloud:
config:
discovery:
enabled: true
# 这个是去Eureka中获取命为springcloud-config-server的服务
service-id: springcloud-config-server
fail-fast: true
# 表明文件名是以dev结尾的
profile: dev
# git分支
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:1000/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
appname: springcloud-test-server
- 修改启动类
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
public class SpringCloudTestServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudTestServerApplication.class, args);
}
@Value("${server.port}")
String port;
@Value("${foo}")
String foo;
@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "Test") String name) {
return "hi " + foo + " ,i am from port:" + port;
}
}
然后这样就阔以了。