springcloud config 配置中心
2017-06-24 22:52
741 查看
1、环境&版本:
spring.boot.version = 1.5.4.RELEASE
spring.cloud.version = 1.3.1.RELEASE
jdk.version = 1.8
2、服务端搭建(demo-springcloud-config-server):
pom.xml 添加如下依赖:
修改配置文件:application.properties
在config文件夹下创建config-client-dev.properties 和 config-client-product.properties,内容分别如下:
config-client-dev.properties
启动服务(运行Application.java)
访问http://localhost:8888/config-client/product,结果如下:
访问http://localhost:8888/config-client-product.properties,结果如下:
配置匹配规则:
源码:https://github.com/xiaojianhx/demo-springcloud
spring.boot.version = 1.5.4.RELEASE
spring.cloud.version = 1.3.1.RELEASE
jdk.version = 1.8
2、服务端搭建(demo-springcloud-config-server):
pom.xml 添加如下依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>创建启动类:Application.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @EnableAutoConfiguration @EnableDiscoveryClient @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
修改配置文件:application.properties
server.context-path=/ server.port=8888 # 读取本地配置文件 spring.profiles.active=native # 自动抓取src/main/resources/config/文件夹下的配置 spring.cloud.config.server.native.searchLocations=classpath:/config
在config文件夹下创建config-client-dev.properties 和 config-client-product.properties,内容分别如下:
config-client-dev.properties
env.name=dev env.password=dev123456config-client-product.properties
env.name=product env.password=product123456
启动服务(运行Application.java)
访问http://localhost:8888/config-client/product,结果如下:
{ "name": "config-client", "profiles": [ "product" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "classpath:/config/config-client-product.properties", "source": { "env.password": "product123456", "env.name": "product" } } ] }
访问http://localhost:8888/config-client-product.properties,结果如下:
env.name: product env.password: product123456以上两种方式通用,都可以被客户端识别;修改config-client-product.properties文件,刷新上面两个url,都可立即看到变化;
配置匹配规则:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.yml /{label}/{application}-{profile}.properties其中:
application=config-client profile=product lable=""也可以认为是:
application=config profile=client-product lable=""3、客户端搭建(demo-springcloud-config-client): pom.xml 添加如下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>创建启动类(Application.java):
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }创建Controller:
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 HomeController { @Value("${env.name:World!}") private String bar; @RequestMapping("/app") String hello() { return "Hello " + bar + "!"; } }添加配置文件(application.properties):
server.context-path=/ server.port=8080 # default true management.security.enabled=false endpoints.restart.enabled=true spring.application.name:config-client spring.cloud.config.env:dev spring.cloud.config.profile:dev spring.cloud.config.uri:http://localhost:8888启动服务(运行Application.java),访问http://localhost:8080/app(需要服务端开启),可以读取在server的配置了。如果服务端配置发生变化,可以在客户端刷新,获取最新配置,POST请求接口:http://localhost:8080/refresh,客户端配置生效。
源码:https://github.com/xiaojianhx/demo-springcloud
相关文章推荐
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- spring cloud config整合gitlab搭建分布式的配置中心
- SpringCloud: 高可用的分布式配置中心(Spring Cloud Config)
- SpringCloud: 分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- Spring Cloud config之一:分布式配置中心入门介绍
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- SpringCloud Config-----2、配置中心集群
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- SpringCloud | 配置中心 config(八)
- SpringCloud Config-----1、搭建分布式配置中心
- spring cloud config整合gitlab搭建分布式的配置中心
- spring cloud config-server 高可用配置中心
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)