博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud Config
阅读量:4676 次
发布时间:2019-06-09

本文共 5224 字,大约阅读时间需要 17 分钟。

1, 建立配置文件工程,git地址为:

2, 建立config服务工程  

@EnableConfigServer@SpringBootApplicationpublic class ConfigServiceApplication {        public static void main(String[] args) {        SpringApplication.run(ConfigServiceApplication.class, args);    }}
server.port=8888# 配置git仓库地址spring.cloud.config.server.git.uri=http://Gitlab.xxx.com/xx/xx_configuration.gitspring.cloud.config.server.git.username=xxxspring.cloud.config.server.git.password=xxxx#Git分支spring.cloud.config.label=master#spring.cloud.config.server.git.search-paths=src/main/resources/config
4.0.0
com.example
marketing-config
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
UTF-8
1.8
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Finchley.SR2
pom
import
org.springframework.boot
spring-boot-maven-plugin
View Code

3, 配置Client工程

@SpringBootApplicationpublic class ConfigClientApplication {    public static void main(String[] args) {        SpringApplication.run(ConfigClientApplication.class, args);    }}@RefreshScope@RestControllerclass MessageRestController {    @Value("${message1:Hello default}")    private String message;    @RequestMapping("/message")    String getMessage() {        return this.message;    }}
server.port=8889#对应仓库中ymlspring.application.name=client1management.endpoints.web.exposure.include=*spring.cloud.config.profile=dev spring.cloud.config.label=masterspring.cloud.config.uri=http://localhost:8888
4.0.0
com.example
configuration-client
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Finchley.SR2
pom
import
org.springframework.boot
spring-boot-maven-plugin
View Code

3,验证

先启动Service工程,访问  ,如果能看到配置的属性说明服务启动成功。

再启动Client工程,访问:

Config Server启动以后,我们可以通过它暴露的端点获取配置文件内容,http请求地址与配置文件映射关系如下:

# 映射{application}-{profile}.properties文件/{application}/{profile}/[{label}]/{label}/{application}-{profile}.properties/{application}-{profile}.properties/{label}/{application}-{profile}.yml/{application}-{profile}.yml

{application}通常使用微服务名称,对应Git仓库中文件名的前缀;

{profile}对应{application}-后面的dev、pro、test等;
{label}对应Git仓库的分支名,默认为master。

 

4,自动刷新

  修改client-dev.yml的属性,然后再用postman以post方式请求http://localhost:8889/actuator/refresh。

  再次访问, 可以看到内容变化。

 

参考资料:

                 

 

转载于:https://www.cnblogs.com/season2009/p/11163083.html

你可能感兴趣的文章
字节数组java加密与解密
查看>>
矩形运算
查看>>
php 备份mysql数据库(joomla数据库可直接使用,其他数据库稍作修改即可)
查看>>
使用HttpSessionListener接口监听Session的创建和失效
查看>>
Windows Phone XNAでアニメーション - ぐるぐる
查看>>
20181029 T2 寻宝游戏
查看>>
C++变量作用域、生存期、存储类别
查看>>
数据结构期末复习(四)
查看>>
最最简单的菜单代码
查看>>
js 俩组数据根据id合并
查看>>
POJ2987 Firing 最大权闭合图
查看>>
ItelliJ IDEA下载及获取注册码详解
查看>>
ASP.NET AjaxPro的应用 .AjaxPro使用中“XXX未定义”的一种解决方法(转载的)
查看>>
谷歌和HTTPS
查看>>
Linux 系统的IP与域名解析文件[局域网的DNS]
查看>>
各种实用类
查看>>
【LGP5161】WD与数列
查看>>
最近素数问题——C语言
查看>>
Oracle和Mysql的区别 转载
查看>>
GOF23设计模式
查看>>