Spring Cloud 采用Consul做配置中心

-----------------pom.xml依赖,主要是spring-cloud-starter-consul-config

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!--服务发现依赖-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!--用于consul配置-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.6</version>
</dependency>

-----------------bootstrap.yml

server:port: 9201spring:application:name: springtest-serviceprofiles:active: devcloud:consul:host: 10.129.63.40port: 9001discovery:register: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}service-name: ${spring.application.name}port: 9201healthCheckPath: /actuator/healthhealthCheckInterval: 15sconfig:enabled: trueformat: YAMLprefix: configdefaultContext: applicationprofileSeparator: ','data-key: data

注:discovery段是添加到consul注册中心的

config段说明:

enabled: true允许配置中心

format:YAML 表示consul中的key-value中的value内容,采用YAML格式,据说有四种 YAML PROPERTIES KEY-VALUE FILES

prefix: config 表示consul用于存储配置的文件夹根目录名为config

defaultContext: application 表示配置文件对应的默认应用名称(优先获取当前服务名称配置,没有的到application里找)

profileSeparator: ',' 表示如果有多个profile(eg: 开发环境dev,测试环境test...) ,则key名中的profile与defaultContext之间,用什么分隔符来表示(例如config/springtest-service,dev/data)

data-key: data 表示最后一层节点的key值名称,一般默认为data

 

---------------TestConfig.java

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;@ConfigurationProperties(prefix = "test-config")
@Configuration
@Data
public class TestConfig {private String testValue;
}

--------------App.java

@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class SpringtestServerApplication {@Autowiredprivate DiscoveryClient discoveryClient;@Value("${test.testValue}")private String testValue;@Autowiredprivate TestConfig  testConfig;public static void main(String[] args) {SpringApplication.run(SpringtestServerApplication.class, args);}@RequestMapping("/test")public String test(String id) {return "test"+id+"/"+testValue+"/"+testConfig.getTestValue();}
}

这里面采用了两种获取配置的方式

@Value

@ConfigurationProperties

都能进行配置获取,但是后者可以实现配置更新后动态更新

 

-------------配置

consul页面上添加key为:

config/springtest-service/data

value为:

test:testValue:  consul--asdf34
testConfig:test-value: consul123--asdf34

 

-------------配置优先级

config/testApp,dev/

config/testApp/

config/application,dev/

config/application/

 

示例代码:

https://github.com/wanghongqi/springcloudconsul_test/tree/master/springtest_server

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/416989.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(2354):image组件的基本使用

pages.json <template><view><!-- <view class""><text>唱歌跳舞</text></view><view seletable><text>唱歌跳舞</text></view><view seletable space"ensp"><text>唱歌 …

vasp服务器中断,求助VASP能带计算的中断原因

我也遇到类似情况&#xff0c;发现服务器内存没有用到多少&#xff1f;是不是并行环境有问题&#xff1f;谢谢&#xff01;DA part: xc-table for Pade appr. of PerdewPOSCAR, INCAR and KPOINTS ok, starting setupWARNING: small aliasing (wrap around) errors must be exp…

Java多线程(一)

线程问题&#xff0c;或称并发 创建线程一般有一下方法 继承Thread类&#xff0c;重写run方法 1 public class TestThread extends Thread{ 2 public void run() { 3 System.out.println("Hello World"); 4 } 5 public static void m…

git 大文件报错Out of memory, malloc failed、 The remote end hung up unexpectedly

git clone出现 "fatal: The remote end hung up unexpectedly" 设置 git config --global http.postBuffer 104857600000 后git clone报错 fatal: Out of memory, malloc failed (tried to allocate 1778384896 bytes) 又把postBuffer改了回去 git config --glo…

前端学习(2355):uni里面的样式学习

style.vue <template><view><view class"box">我是歌谣</view></view> </template><script>export default {data() {return {}},methods: {}} </script><style> .box{width: 375rpx;height: 375rpx;backgrou…

目前市场上的电脑一体机从计算机种类,一体机电脑与普通电脑的区别

现在&#xff0c;市场上开始流行一体机电脑了&#xff0c;很多网友可能会对一体机电脑感兴趣。下面&#xff0c;本文针对市场上的一体机电脑优劣作一个简要说明。一、一体机电脑的好处由于一体机电脑&#xff0c;所有的设备都封装在同一个容器内&#xff0c;就连显示屏都和电脑…

C#之调用存储过程

C#调用存储过程 以下内容可能有错漏之处&#xff0c;请大家多多指教。 C#后台代码如下&#xff1a; //调用存储过程的方法public static void Startupworkflow(string firstnodename, string secondnodename, string firstact) { SqlConnection conn new Sq…

css3修改透明png颜色

实现思路&#xff1a; 图片作为背景&#xff0c;drop-shadow阴影形成新的元素&#xff0c;再用left把整体偏移回去&#xff0c;最后用overflow:hidden把原图片隐藏 注&#xff1a;当放在其他绝对定位的元素上面时注意z-index的处理。 .iconXXX{overflow:hidden;display: inl…

计算机速录比赛主题,弘扬汉字,全国输入法表演赛电脑输入法比赛组速录表演组的报名...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼“弘扬汉字&#xff0c;全国输入法表演赛”组委会、评委、电脑输入法比赛组、速录表演组的报名现在已陆续报名&#xff0c;没有报名的赶快报名&#xff0c;有方案展示的也可以参加&#xff0c;没有填写详细信息的请及时填写&#x…

前端学习(2357):uni的基本数据绑定

<template><view><view>{{msg}}</view><view>{{你好世界}}</view></view> </template><script>export default {data() {return {msg:hello}},methods: {}} </script><style></style>运行结果

GitChat专栏:Spring Cloud 与 Consul 的整合使用

Spring Cloud 热度日益提升&#xff0c;注册中心、配置中心的选型是一个必然面对的问题。 Eureka 2.0 开源工作宣告停止&#xff0c;Zookeeper 略显笨重&#xff0c;Consul 是某种业务场景下相对较好的选择。Consul 部署简单&#xff0c;兼具注册中心和配置中心&#xff0c;Go …

地图收敛心得170405

寻路算法大总结! 交换机生成树采用的是完全不同的D-V(distance vector)距离矢量算法,并不是很可靠. 并不是任意两点之间的最短路径,因为任意两点之间取最短路径可能有环路:总权更大 交换机STP不一定是最小生成树!!!举例论证 因为它只是所有交换机到根桥最短 贪心算法的味道 kru…

win7下搭建小程序服务器,技术开发人员告诉你微信小程序怎么做

现在每个玩微信公众号的博主基本上都在玩微信小程序&#xff0c;现在的小程序特别火。虽然小程序特别火但也不是任何人想立马做就能做的&#xff0c;也是需要会开发及了解微信小程序的相关规则才能做的。今天邀请技术开发人员告诉你微信小程序怎么做&#xff0c;想入门的小伙伴…

前端学习(2358):v-bind和v-for

geyao.vue <template><view><view>{{msg}}</view><view>{{你好世界}}</view><image v-bind:src"imgUrl" mode""></image><view v-for"(item,index) in arr">序号:{{index}}名字:{{item.…

Java 操作 HDFS

HDFS 作为开源界比较成熟的分布式文件存储系统&#xff0c;适用于海量文件存储&#xff0c;本文介绍了如何使用 Java 操作 HDFS&#xff0c;采用 Maven 管理包。 pom.xml <dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client…

iOS---------- MBProgressHUD (1.0.0)的变动

1.改变菊花的颜色 // hud.color [UIColor blackColor];--------------> hud.bezelView.color [UIColor blackColor]; 2.改变菊花的坐标 // hud.yOffset-150; --------------> hud.offset CGPointMake(0, -150); 待续。。。 转载于:https:/…

怎么把数据文件上传云服务器,怎么把数据上传导云服务器

怎么把数据上传导云服务器 内容精选换一换安装传输工具在本地主机和Windows云服务器上分别安装数据传输工具&#xff0c;将文件上传到云服务器。例如QQ.exe。在本地主机和Windows云服务器上分别安装数据传输工具&#xff0c;将文件上传到云服务器。例如QQ.exe。本地磁盘映射(推…

长路漫漫,唯剑作伴--loadView、viewDidLoad及viewDidUnload的关系

一、loadView 什么时候被调用&#xff1f; 每次访问UIViewController的view(比如controller.view、self.view)而且view为nil&#xff0c;loadView方法就会被调用。 有什么作用&#xff1f; loadView方法是用来负责创建UIViewController的view 默认实现是怎样的&#xff1f; 它会…

前端学习(2359):如何注册事件

<template><view><view>{{msg}}</view><view>{{你好世界}}</view><image v-bind:src"imgUrl" mode""></image><view v-for"(item,index) in arr">序号:{{index}}名字:{{item.name}},年龄…