spring cloud config 使用
程序员文章站
2022-07-15 08:57:49
...
参考:http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html
server
- 创建保存配置文件的git仓库
https://gitee.com/jiyangM/spring-cloud-config/tree/master/config 添加配置文件
server端代码
pox.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
配置文件application.yml
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/jiyangM/spring-cloud-config
search-paths: config
username: 18600884376@163.com
password: 1993Jiyang
/**
* Created by jiyang on 15:16 2017/12/15
*/
@SpringBootApplication
@EnableConfigServer
public class Application {
/**
* application.java 文件不能直接放在main/java文件夹下,必须要建一个包把他放进去
* @param args
*/
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
启动,测试,访问 :
http://localhost:8888/neo-config/test
会返回配置信息。
注意: neo-config/test 对应git仓库中的neo-config-test.yml文件
client
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
spring:
application:
name: spring-cloud-config-client
server:
port: 8002
bootstrap.yml
spring:
cloud:
config:
name: neo-config
profile: dev
uri: http://localhost:8888/
label: master
测试类
/**
* Created by jiyang on 12:16 2017/12/19
*/
@RestController
public class HelloController {
@Value("${neo.hello}")
private String sayHello;
@RequestMapping("/hello")
public String sayHello(){
return this.sayHello;
}
}
启动,测试,访问 :http://localhost:8002/hello
推荐阅读
-
Spring Cloud Config实现分布式配置中心
-
详解Spring cloud使用Ribbon进行Restful请求
-
Spring-cloud 服务发现与消费(以ribbon为例)
-
Spring Cloud中FeignClient实现文件上传功能
-
详解Spring Cloud 熔断机制--断路器
-
spring boot Logging的配置以及使用详解
-
Spring-cloud 注册服务提供者搭建方法
-
Spring cloud Eureka注册中心搭建的方法
-
阿里Sentinel支持Spring Cloud Gateway的实现
-
Spring中property-placeholder的使用与解析详解