笔记-- Spring Cloud(七):配置中心(消息总线)_set ‘spring

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注运维)
img

正文

port: 12000

eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/

management:
endpoints:
web:
exposure:
include: bus-refresh


##### 启动类


加`@EnableConfigServer`注解



@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
}

}


#### 客户端


##### POM 配置



org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-bus org.springframework.cloud spring-cloud-stream-binder-rabbit org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web

##### 配置文件


还是分为两个,分别如下  
 application.yml



spring:
application:
name: config-client-git
cloud:
bus:
trace:
enabled: true
enabled: true
server:
port: 13000

management:
endpoints:
web:
exposure:
include: refresh


bootstrap.yml



spring:
cloud:
config:

uri: http://localhost:12000 # 配置中心的具体地址,即 config-server

  name: config-client # 对应 {application} 部分
  profile: dev # 对应 {profile} 部分
  label: master # 对应 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存储,则该参数无用
  discovery:
    enabled: true
    service-id: config-server

eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/


##### Controller



@RestController
@RefreshScope
public class HelloController {

@Value("${hello.yujian}")
private String name;

@GetMapping("/info")
public String hello() {
	return name;
}

}


`@RefreshScope`必须加,否则客户端会受到服务端的更新消息,但是更新不了,因为不知道更新哪里的。


至于启动主类,用默认生成的不用改,就不贴了。


##### 测试


分别启动 eureka、config-server 和两个 config-client



打包

./mvnw clean package -Dmaven.test.skip=true

运行两个 client

java -jar target/spring-cloud-config-client-0.0.1-SNAPSHOT.jar --server.port=13000
java -jar target/spring-cloud-config-client-0.0.1-SNAPSHOT.jar --server.port=13001


启动后,RabbitMQ 中会自动创建一个 topic 类型的 Exchange 和两个以`springCloudBus.anonymous.`开头的匿名 Queue


![在这里插入图片描述](https://img-blog.csdnimg.cn/20201024160443856.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTM0NTQx,size_16,color_FFFFFF,t_70#pic_center)


我们访问 http://localhost:13000/info 和 http://localhost:13001/info 返回内容的都是`dev`。  
 将 Git 中的配置信息由`dev`改为`dev bus`,并执行



curl -X POST http://localhost:12000/actuator/bus-refresh/


再次访问 http://localhost:13000/info 和 http://localhost:13001/info 这时返回内容就是修改之后的`dev bus`了,说明成功了。


服务端在刷新接口产生的的日志:



2020-08-23 01:16:46.554 INFO 14816 — [io-12000-exec-5] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
2020-08-23 01:16:46.570 INFO 14816 — [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2020-08-23 01:16:46.570 INFO 14816 — [io-12000-exec-5] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory.publisher#71c9ee6b:0/SimpleConnection@3eff8314 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 52828]
2020-08-23 01:16:46.570 INFO 14816 — [io-12000-exec-5] o.s.amqp.rabbit.core.RabbitAdmin : Auto-declaring a non-durable, auto-delete, or exclusive Queue (springCloudBus.anonymous.p0gJRZZuQ62jvBUNVVyEjA) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.
2020-08-23 01:16:46.589 INFO 14816 — [io-12000-exec-5] o.s.cloud.bus.event.RefreshListener : Received remote refresh request.
2020-08-23 01:16:47.117 INFO 14816 — [io-12000-exec-5] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set ‘spring.devtools.add-properties’ to ‘false’ to disable
2020-08-23 01:16:47.652 INFO 14816 — [io-12000-exec-5] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set ‘spring.devtools.add-properties’ to ‘false’ to disable
2020-08-23 01:16:47.661 INFO 14816 — [io-12000-exec-5] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2020-08-23 01:16:47.670 INFO 14816 — [io-12000-exec-5] o.s.boot.SpringApplication : Started application in 1.071 seconds (JVM running for 4816.901)
2020-08-23 01:16:47.917 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient …
2020-08-23 01:16:48.538 INFO 14816 — [io-12000-exec-7] .c.s.e.MultipleJGitEnvironmentRepository : Fetched for remote master and found 1 updates
2020-08-23 01:16:50.069 INFO 14816 — [io-12000-exec-7] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set ‘spring.devtools.add-properties’ to ‘false’ to disable
2020-08-23 01:16:50.085 INFO 14816 — [io-12000-exec-7] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-151267203105365156/4.springcloud/code/spring-cloud-2020/config-repo/config-client-dev.yml
2020-08-23 01:16:50.919 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Unregistering …
2020-08-23 01:16:50.924 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIG-SERVER/192.168.1.7:config-server:12000 - deregister status: 200
2020-08-23 01:16:50.936 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient
2020-08-23 01:16:51.786 INFO 14816 — [io-12000-exec-6] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set ‘spring.devtools.add-properties’ to ‘false’ to disable
2020-08-23 01:16:51.801 INFO 14816 — [io-12000-exec-6] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-151267203105365156/4.springcloud/code/spring-cloud-2020/config-repo/config-client-dev.yml
2020-08-23 01:16:51.806 INFO 14816 — [io-12000-exec-5] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2020-08-23 01:16:51.808 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2020-08-23 01:16:51.811 INFO 14816 — [io-12000-exec-5] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2020-08-23 01:16:51.811 INFO 14816 — [io-12000-exec-5] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2020-08-23 01:16:51.811 INFO 14816 — [io-12000-exec-5] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2020-08-23 01:16:51.811 INFO 14816 — [io-12000-exec-5] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2020-08-23 01:16:51.904 INFO 14816 — [io-12000-exec-5] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2020-08-23 01:16:51.905 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2020-08-23 01:16:51.905 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2020-08-23 01:16:51.905 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2020-08-23 01:16:51.905 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Application is null : false
2020-08-23 01:16:51.905 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2020-08-23 01:16:51.905 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2020-08-23 01:16:51.905 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2020-08-23 01:16:51.909 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : The response status is 200
2020-08-23 01:16:51.909 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2020-08-23 01:16:51.910 INFO 14816 — [io-12000-exec-5] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2020-08-23 01:16:51.911 INFO 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1598116611911 with initial instances count: 3
2020-08-23 01:16:51.915 INFO 14816 — [io-12000-exec-5] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application CONFIG-SERVER with eureka with status DOWN
2020-08-23 01:16:51.915 WARN 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1598116611915, current=DOWN, previous=STARTING]
2020-08-23 01:16:51.915 INFO 14816 — [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIG-SERVER/192.168.1.7:config-server:12000: registering service…
2020-08-23 01:16:51.915 INFO 14816 — [io-12000-exec-5] o.s.c.n.e.s.EurekaServiceRegistry : Registering application CONFIG-SERVER with eureka with status UP
2020-08-23 01:16:51.915 WARN 14816 — [io-12000-exec-5] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1598116611915, current=UP, previous=DOWN]
2020-08-23 01:16:51.916 INFO 14816 — [io-12000-exec-5] o.s.cloud.bus.event.RefreshListener : Keys refreshed []
2020-08-23 01:16:51.926 INFO 14816 — [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIG-SERVER/192.168.1.7:config-server:12000 - registration status: 204


客户端在刷新接口产生的的日志:



2020-08-23 01:16:46.620 INFO 15480 — [cuilRxfRVNFkg-1] o.s.cloud.bus.event.RefreshListener : Received remote refresh request.
2020-08-23 01:16:47.122 INFO 15480 — [cuilRxfRVNFkg-1] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set ‘spring.devtools.add-properties’ to ‘false’ to disable
2020-08-23 01:16:47.681 INFO 15480 — [cuilRxfRVNFkg-1] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set ‘spring.devtools.add-properties’ to ‘false’ to disable
2020-08-23 01:16:47.693 INFO 15480 — [cuilRxfRVNFkg-1] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://192.168.1.7:12000/
2020-08-23 01:16:50.087 INFO 15480 — [cuilRxfRVNFkg-1] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=08ec27dd91c0c98cbcb46d32a4a9adbb02c33756, state=null
2020-08-23 01:16:50.087 INFO 15480 — [cuilRxfRVNFkg-1] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name=‘bootstrapProperties-configClient’}, BootstrapPropertySource {name=‘bootstrapProperties-https://gitee.com/monkeyshow/yujian2020/4.springcloud/code/spring-cloud-2020/config-repo/config-client-dev.yml’}]

最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

资料预览

给大家整理的视频资料:

给大家整理的电子书资料:

如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注运维)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注运维)
[外链图片转存中…(img-cSwaf8NK-1713630325990)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 26
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值