SpringCloud的学习记录(4)

本篇基于上一篇写的, 在git上更改配置后, eureka-client如何更新.

我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp;

这就是说我们需要装rabbitMq;

一定要先安装erlang, 成功后再安装rabbitmq. 切记

1. 先去下载erlang, 我的电脑是64位的,下载的是OTP 21.3 Windows 64-bit Binary File.

 image

2. 点击rabbitmq下载. 至于怎么使用 rabbitmq, 搜索引擎下.

安装成功后, 启动rabbitmq, 如下

imageimage

或则cmd到安装你的rebbitmq目录, 到sbin的目录, 然后输入rabbitmq-plugins enable rabbitmq_management

看到如下的打印,可以说明rabbitmq运行成功了.

image

然后浏览器打开http://localhost:15672, 用户名和密码都是guest, 界面如下

image

说明启动成功. 下面开始改造config-server和eureka-client项目.

分别在config-server和eureka-client项目里的pom.xml添加如下配置

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

然后在config-server的application.yml文件内容如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:          
          uri: https://github.com/xxx 你的git仓库地址
          searchPaths: repos
          username: 你的git用户名
          password: 你的git密码
      label: master
  rabbitmq:
    host: 127.0.0.1
    port: 5672  // 注意这里的端口是5672,而我们刚才浏览器打开的http://localhost:15672端口是15672
    username: guest
    password: guest
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: '*'

然后在eureka-client的application.yml文件内容如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: eureka-client
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: '*'

image

用Run DashBoard启动如下项目:

1. 先启动eureka-server项目

2. 再启动config-server项目, Event log里能看到类似如下内容:

o.s.a.r.c.CachingConnectionFactory: Attempting to connect to: [127.0.0.1:5672]
o.s.a.r.c.CachingConnectionFactory: Created new connection: rabbitConnectionFactory#40195a:0/SimpleConnection@5d73bd [delegate=amqp://guest@127.0.0.1:5672/, localPort= 4586]

3. 最后启动eureka-client项目, Event log里能看到类似如下内容(git仓库地址配置好):

c.c.c.ConfigServicePropertySourceLocator: Fetching config from server at : http://localhost:8765/
c.c.c.ConfigServicePropertySourceLocator: Located environment: name=eureka-client, profiles=[dev], label=master, version=addadb9ed2392ae7f6dddc63a5be60361e2ad8d6, state=null
b.c.PropertySourceBootstrapConfiguration: Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, 
MapPropertySource {name='https://github.com/xxx 你的git仓库地址/repos/eureka-client-dev.properties'}]}

启动完成上面三个项目后, 在浏览器打开eureka-client里的api: http://localhost:8762/testone/config

image

这里从git仓库取出来的message内容是hello spring io –111111, foo的内容是foo version 111111.

现在我们改变git仓里里的message内容.

如何安装curl,请戳这里.

打开cmd, 输入 curl -X POST http://localhost:8765/actuator/bus-refresh

image

或则用post man发送:

image

在config-server和eureka-client这两个项目的Event log里看到类似如下的内容:

o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed []

再看rabbitmq页面内容变化, 刷新页面http://localhost:15672, 显示如下:

image

最后页面刷新http://localhost:8762/testone/config, 就可以看到改的内容了.

我把message改成hello spring io -111111 add message(多了add message内容).

image

原文地址:https://www.cnblogs.com/xumBlog/p/10622390.html