# 从浅入深 学习 SpringCloud 微服务架构(十七)--Spring Cloud config(2)

从浅入深 学习 SpringCloud 微服务架构(十七)–Spring Cloud config(2)

一、springcloudConfig 入门案例:搭建 config 服务端

1、登录 码云:https://gitee.com/

1)点击右上角 【+】 再点击【新建仓库】

仓库名称:config-repostory
公开
勾选【设置模板(添加 Readme,lssue,Pull Request 模板文件)】

2)点击 【创建】。

2、把 生产者 子工程 product_service (子模块)中的 配置文件 application.yml 文件,复制两份,分别命名为:并修改端口号为:9001, 9002, 然后上传至 gitee 仓库中。

1)文件命名规则:

  • {application}–{profile}.yml
  • {application}–{profile}.properties
  • application 为应用名称,profile 指开发环境(用于区分开发环境,测试环境,生产环境等)。

2)application-dev.yml

##  spring_cloud_demo\product_service\src\main\resources\application-dev.ymlserver:port: 9001  # 启动端口 命令行注入。
spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: true
eureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。
name: djh-dev  # 开发环境

3)application-pro.yml

##  spring_cloud_demo\product_service\src\main\resources\application-pro.ymlserver:port: 9002  # 启动端口 命令行注入。
spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: true
eureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。
name: djh-pro  # 生产环境

4)码云:https://gitee.com/ 的 config-repostory 仓库中,点击【文件】,再点击【上传】
把 application-dev.yml ,和 application-pro.yml 传上去。

提交信息:2024-5-11-1

点击【提交】。

3、Spring Cloud Config:入门案例–搭建 config 服务端–在父工程 spring_cloud_demo 下,创建子工程 config_service(子模块)

3.1 创建 子工程 config_service(子模块)
	--> 右键 spring_cloud_demo 父工程--> Modules --> Maven --> Groupld : ( djh.it )Artifactld : ( config_service )Version : 1.0-SNAPSHOT--> Next --> Module name: ( config_service )Content root : ( spring_cloud_demo\config_service)Module file location: ( spring_cloud_demo\config_service )--> Finish
3.2 在 子工程 config_service (子模块)中的 pom.xml 中导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>config_service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies>
</project>
<!-- spring_cloud_demo\config_service\pom.xml -->
3.3 在 子工程 config_service (子模块)中,创建启动类 ConfigServerApplication.java
/***   spring_cloud_demo\config_service\src\main\java\djh\it\config\ConfigServerApplication.java**   2024-5-11 启动类 ConfigServerApplication.java*/package djh.it.config;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
}
3.4 在 子工程 config_service (子模块)中,创建配置文件 application.yml
##  spring_cloud_demo\config_service\src\main\resources\application.ymlserver:port: 10000  # 启动端口 命令行注入。
spring:application:name: config-server  #spring应用名, # 注意 FeignClient 不支持名字带下划线cloud:config:server:git:uri: https://gitee.com/djh-xian/config-repostory.git

4、运行启动类 ConfigServerApplication.java,进行测试

浏览器地址栏输入:localhost:10000/product-dev.yml 会显示配置文件内容。

在这里插入图片描述

二、springcloudConfig 入门案例:客户端改造,动态获取配置信息

1、在 product_service 子工程(子模块)的 pom.xml 中,引入依赖坐标。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product_service</artifactId><dependencies><!-- springcloudgateway 的内部是通过 netty + webflux 实现。webflux 实现和 springmvc 存在冲突,需要注销掉父工程中的 web 依赖,在各子模块中导入 web 依赖。--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--            <version>5.1.32</version>--><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 引入 EurekaClient 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 引入 sleuth 链路追踪 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId></dependency><!-- 引入 zipkin 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency><!-- 引入 rabbit 相关 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-sleuth-zipkin</artifactId></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId></dependency><!-- 引入 动态获取配置信息 相关依赖坐标  --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency></dependencies>
</project>
<!-- spring_cloud_demo\product_service\pom.xml -->

2、在 product_service 子工程(子模块)中,删除 application.yml 配置文件,创建 bootstrap.yml 配置文件,让它从 gitee 中动态拉取 application.yml 配置文件。

##  spring_cloud_demo\product_service\src\main\resources\bootstrap.ymlspring:cloud:config:name: application  # 应用名称,需要对应 gitee 中配置文件名称的前半部分profile: dev   # 开发环境     label: master  # gitee 中的分支uri: http://localhost:10000  # config-server 的请求地址。

3、启动 product_service(9001), eureka_service, config_server 三个子项目的启动类,进行测试。

浏览器地址栏输入:localhost:9001/product/1 会显示从 MySQL 摘取的数据内容。
浏览器地址栏输入:localhost:9001/product/test 会显示:djh-dev

4、更改 product_service 子工程(子模块)的 bootstrap.yml 配置文件,使用 pro 生产环境。

##  spring_cloud_demo\product_service\src\main\resources\bootstrap.ymlspring:cloud:config:name: application  # 应用名称,需要对应 gitee 中配置文件名称的前半部分# profile: dev   # 开发环境profile: pro   # 生产环境label: master  # gitee 中的分支uri: http://localhost:10000  # config-server 的请求地址。

5、再次启动 product_service(9002), eureka_service, config_server 三个子项目的启动类,重新进行测试。

浏览器地址栏输入:localhost:9002/product/1 会显示从 MySQL 摘取的数据内容。
浏览器地址栏输入:localhost:9002/product/test 会显示:djh-pro

在这里插入图片描述
在这里插入图片描述

三、springcloudConfig 入门案例:手动刷新数据

1、在 product_service 子工程(子模块)的 pom.xml 中,引入依赖坐标。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product_service</artifactId><dependencies><!-- springcloudgateway 的内部是通过 netty + webflux 实现。webflux 实现和 springmvc 存在冲突,需要注销掉父工程中的 web 依赖,在各子模块中导入 web 依赖。--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--            <version>5.1.32</version>--><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 引入 EurekaClient 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 引入 sleuth 链路追踪 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId></dependency><!-- 引入 zipkin 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency><!-- 引入 rabbit 相关 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-sleuth-zipkin</artifactId></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId></dependency><!-- 引入 动态获取配置信息 相关依赖坐标  --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- 引入 手动刷新数据 相关依赖坐标  --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies>
</project>
<!-- spring_cloud_demo\product_service\pom.xml -->

2、修改 product_service 子工程(子模块)的 Controller 类 ProductController.java 添加动态刷新数据。

/***  spring_cloud_demo\product_service\src\main\java\djh\it\product\controller\ProductController.java**  2024-5-11 商品的 controller 类 ProductController.java*/
package djh.it.product.controller;import djh.it.product.domain.Product;
import djh.it.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/product")
@RefreshScope  //开启动态刷新
public class ProductController {@Autowiredprivate ProductService productService;@Value("${name}")private String name;@RequestMapping(value = "/{id}", method = RequestMethod.GET)public Product findById(@PathVariable Long id){return productService.findById(id);}@RequestMapping(value = "/test")public String test (){return name;}}

3、修改 product_service 子工程(子模块)的 bootstrap.yml 配置文件,添加 动态刷新的请求路径端点配置。

##  spring_cloud_demo\product_service\src\main\resources\bootstrap.ymlspring:cloud:config:name: application  # 应用名称,需要对应 gitee 中配置文件名称的前半部分profile: dev   # 开发环境# profile: pro   # 生产环境label: master  # gitee 中的分支uri: http://localhost:10000  # config-server 的请求地址。
# 开启动态刷新的请求路径端点
management:endpoints:web:exposure:include: refresh

4、再次启动 product_service(9001), eureka_service, config_server 三个子项目的启动类,重新进行测试。

1)浏览器地址栏输入:localhost:9001/product/1 会显示从 MySQL 摘取的数据内容。
浏览器地址栏输入:localhost:9001/product/test 会显示:djh-dev

在这里插入图片描述

2)登录 码云:https://gitee.com/ 的 config-repostory 仓库中,点击application.yml 文件,再点击【编辑】,修改内容: name: djh-dev 开发环境,不注释汉字了。

提交信息:2024-5-11-1

点击【提交】。
在这里插入图片描述

3)浏览器地址栏输入:localhost:9001/product/1 会显示从 MySQL 摘取的数据内容。
浏览器地址栏输入:localhost:9001/product/test 会显示:djh-dev 没有更新。

4)打开 Postman 软件,POST 请求地址:http://localhost:9001/actuator/refresh 刷新缓存后。

在这里插入图片描述

5)浏览器地址栏输入:localhost:9001/product/1 会显示从 MySQL 摘取的数据内容。
浏览器地址栏输入:localhost:9001/product/test 不用重新启动 product_service 启动类,
会动态刷新数据显示:djh-dev-5-12。

在这里插入图片描述

四、springcloudConfig 高可用-上

1、在 config_service 子工程(子模块)的 pom.xml 文件中,导入 springcloudConfig 高可用相关依赖坐标。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>config_service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!-- springcloudConfig 高可用 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-bus</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-stream-binder-rabbit</artifactId></dependency><!-- 引入 EurekaClient 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 引入 动态获取配置信息 相关依赖坐标  --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- 引入 手动刷新数据 相关依赖坐标  --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies>
</project>
<!-- spring_cloud_demo\config_service\pom.xml -->

2、在 config_service 子工程(子模块)的 application.yml 文件中,添加 eureka 配置。

##  spring_cloud_demo\config_service\src\main\resources\application.ymlserver:port: 10001  # 启动端口 命令行注入。
spring:application:name: config-server  #spring应用名, # 注意 FeignClient 不支持名字带下划线cloud:config:server:git:uri: https://gitee.com/djh-xian/config-repostory.git
eureka: # 配置 eureka_serverclient:service-url: # 配置暴露给 EurekaClient 的请求地址defaultZone: http://127.0.0.1:9000/eureka/  # 配置高可用时,须配置为另一个 EurekaServerApplication 的端口号,如:8000instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}

3、在 idea 的 RunDashboard 面板,复制一个 ConfigServerApplication 模拟高可用, 更名为 ConfigServerApplication(2),application.yml 启动端口改为 10001。

在这里插入图片描述

4、启动 product_service(9001), eureka_service, config_server(ConfigServerApplication,ConfigServerApplication(2))四个启动类,进行测试。

1)浏览器地址栏输入:localhost:9000 登录到 eureka 注册中心,发现 2 个微服务,其中 config-server 有两个端口(10000,10001)。

2)浏览器地址栏输入:localhost:10000/product/1
浏览器地址栏输入:localhost:10001/product/1 都能获取到数据。

在这里插入图片描述

五、springcloudConfig 高可用-下

1、在 product_service 子工程(子模块)的 bootstrap.yml 文件中,添加 Eureka 服务发现。

##  spring_cloud_demo\product_service\src\main\resources\bootstrap.ymlspring:cloud:config:name: application  # 应用名称,需要对应 gitee 中配置文件名称的前半部分profile: dev   # 开发环境# profile: pro   # 生产环境label: master  # gitee 中的分支# uri: http://localhost:10000  # config-server 的请求地址。discovery:   # 通过注册中心获取 config-server 配置enabled: trueservice-id: config-server  #服务名# 开启动态刷新的请求路径端点
management:endpoints:web:exposure:include: refresh
eureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。

2、启动 product_service(9001), eureka_service, config_server(ConfigServerApplication,ConfigServerApplication(2))四个启动类,进行测试。

1)浏览器地址栏输入:localhost:9001/prodcut/1
浏览器地址栏输入:localhost:9001/prodcut/test 都可以正常访问。

2)说明:两个 config-server 都注册到 Eureka, 微服务 Product_service 可以通过 Eureka 注册中心,获取信息,这样,即使 有一个 config-server 挂掉,也不会影响获取数据。

在这里插入图片描述

六、springcloudConfig:结合消息总线 bus 动态修改配置文件信息

1、消息总线 bus

在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,它广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线。SpringCloud 中也有对应的解决方案,SpringCloud Bus 将分布式的节点用轻量的消息代理连接起来可以很容易搭建消息总线,配合 SpringCloud config 实现微服务应用配置信息的动态更新

2、根据下图我们可以看出利用 Spring Cloud Bus 做配置更新的步骤:

  • 提交代码触发 post 请求给 bus/refresh。
  • server 端接收到请求并发送给 Spring Cloud Bus。
  • Spring Cloud bus 接到消息并通知给其它客户端。
  • 其它客户端接收到通知,请求 Server 端获取最新配置。
  • 全部客户端均获取到最新的配置。

3、在 config_service 子工程(子模块)的 pom.xml 文件中,导入 消息总线 依赖坐标。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>config_service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!-- springcloudConfig 高可用 --><!-- 引入 消息总线的 依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-bus</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-stream-binder-rabbit</artifactId></dependency><!-- 引入 EurekaClient 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 引入 动态获取配置信息 相关依赖坐标  --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- 引入 手动刷新数据 相关依赖坐标  --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies>
</project>
<!-- spring_cloud_demo\config_service\pom.xml -->

4、在 config_service 子工程(子模块)的 application.yml 文件中,添加 消息总线 配置。

##  spring_cloud_demo\config_service\src\main\resources\application.ymlserver:port: 10000  # 启动端口 命令行注入。
spring:application:name: config-server  #spring应用名, # 注意 FeignClient 不支持名字带下划线cloud:config:server:git:uri: https://gitee.com/djh-xian/config-repostory.git# 配置消息总线rabbitmq:host: 127.0.0.1port: 5672username: guestpassword: guest
management:endpoints:web:exposure:include: bus-refresh  # 暴露的端点
eureka: # 配置 eureka_serverclient:service-url: # 配置暴露给 EurekaClient 的请求地址defaultZone: http://127.0.0.1:9000/eureka/  # 配置高可用时,须配置为另一个 EurekaServerApplication 的端口号,如:8000instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}

5、在 product_service 子工程(子模块)的 pom.xml 中,引入 消息总线 依赖坐标。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product_service</artifactId><dependencies><!-- springcloudgateway 的内部是通过 netty + webflux 实现。webflux 实现和 springmvc 存在冲突,需要注销掉父工程中的 web 依赖,在各子模块中导入 web 依赖。--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--            <version>5.1.32</version>--><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 引入 EurekaClient 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 引入 sleuth 链路追踪 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId></dependency><!-- 引入 zipkin 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency><!-- 引入 rabbit 相关 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-sleuth-zipkin</artifactId></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId></dependency><!-- 引入 动态获取配置信息 相关依赖坐标  --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- 引入 手动刷新数据 相关依赖坐标  --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 引入 消息总线的 依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-bus</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-stream-binder-rabbit</artifactId></dependency></dependencies>
</project>
<!-- spring_cloud_demo\product_service\pom.xml -->

6、在 product_service 子工程(子模块)的 bootstrap.yml 文件中,删除掉 动态刷新 配置。

##  spring_cloud_demo\product_service\src\main\resources\bootstrap.ymlspring:cloud:config:name: application  # 应用名称,需要对应 gitee 中配置文件名称的前半部分profile: dev   # 开发环境# profile: pro   # 生产环境label: master  # gitee 中的分支# uri: http://localhost:10000  # config-server 的请求地址。discovery:   # 通过注册中心获取 config-server 配置enabled: trueservice-id: config-server  #服务名
eureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。

7、修改 gitee.com 服务器上的配置文件 application-pro.yml,添加 消息总线 配置。

##  spring_cloud_demo\product_service\src\main\resources\application.ymlserver:port: 9001  # 启动端口 命令行注入。
spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: true# 配置消息总线rabbitmq:host: 127.0.0.1port: 5672username: guestpassword: guest  
eureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。
name: djh-dev-5-12  # 开发环境

8、启动 product_service, eureka_service, config_server(ConfigServerApplication,ConfigServerApplication(2))四个启动类,进行测试。

1)浏览器地址栏输入:localhost:10000/application-dev.yml 正常访问到 gitee 服务器上的配置文件。

2)浏览器地址栏输入:http://localhost:9001/product/1
浏览器地址栏输入:localhost:9001/prodcut/test 都可以正常访问 。

3)登录 码云:https://gitee.com/ 的 config-repostory 仓库中,点击 application-dev.yml 文件,再点击【编辑】,修改内容: name: djh-dev 开发环境,不注释汉字了。

提交信息:2024-5-11-1

点击【提交】。
在这里插入图片描述

3)
浏览器地址栏输入:localhost:9001/product/1 会显示从 MySQL 摘取的数据内容。
浏览器地址栏输入:localhost:9001/product/test 会显示:djh-dev-5-12 没有更新。

4)打开 Postman 软件,POST 请求地址:http://localhost:10000/actuator/bus-refresh 向消息总线刷新数据。

5)
浏览器地址栏输入:localhost:9001/product/1 会显示从 MySQL 摘取的数据内容。
浏览器地址栏输入:localhost:9001/product/test 不用重新启动 product_service 启动类,
会更新数据显示:djh-dev-5-12 开发环境。

在这里插入图片描述

上一节关联链接请点击:
# 从浅入深 学习 SpringCloud 微服务架构(十七)–Spring Cloud config(1)

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

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

相关文章

算法学习笔记(一)-快速幂

#问题的引入-对于幂次方的求解我们怎么可以最大限度的降低时间复杂度呢 #对于一个基本的幂次运算&#xff0c;c代码如下示例 long long int myPower(int base,int power) {long long int result 1 ;for (int i 1 ; i < power ; i){result * base ;}return result ; } #…

网络实验新境界,PNETLab模拟器部署指南

在网络工程领域&#xff0c;拥有一个可靠的网络实验平台至关重要。PNETLab模拟器是一款功能强大的网络仿真工具&#xff0c;它支持包括华为、华三、锐捷、思科在内的多种设备&#xff0c;并且以开源免费的形式提供&#xff0c;这使得它在业界备受青睐。 软件介绍 PNETLab&am…

K8s:二进制安装k8s(单台master)

一、安装k8s 1、拓扑图 2、系统初始化配置 #所有节点执行 systemctl stop firewalld systemctl disable firewalld iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X #永久关闭firewalld并清空iptables所有表规则 setenf…

标准输入输出流(中北大学-程序设计基础(2))

目录 题目 源码 结果示例 题目 输入三角形的三边a,b,c&#xff0c;计算三角形的面积。形成三角形的条件是ab>c,bc>a,ac>b&#xff0c;编写程序&#xff0c;输入a,b,c&#xff0c;检查a,b,c是否满足以上条件&#xff0c;如不满足&#xff0c;由cerr输出有关出错信息…

Spring框架核心:揭秘Java厨房的智能烹饪艺术

前情回顾&#xff1a;Spring框架深度解析&#xff1a;打造你的Java应用梦工厂 六. 实现控制反转 6.1 描述如何在Spring中实现IoC 在Spring Town的厨房里&#xff0c;实现控制反转就像是将食材的采购和准备过程外包给了一个智能系统。这个系统知道每种食材的特性&#xff0c;也…

Java String转JSONObject时保持字段顺序不变

Java String转JSONObject时保持字段顺序不变 问题背景解决方案 问题背景 在业务接口开发过程中&#xff0c;有一个新增接口&#xff0c;需要支持批量新增数据&#xff0c;这时入参就需要用到 json 格式数据&#xff0c;且包含 list 集合&#xff0c;比如这样的数据格式&#x…

Linux-磁盘管理类实训

一、Linux分区和磁盘操作命令 &#xff08;1&#xff09;将系统内所有的分区&#xff08;文件系统&#xff09;列出来&#xff09; &#xff08;2&#xff09;将系统中所有特殊文件格式及名称都列出来 &#xff08;3&#xff09;将/bin下面的可以用的磁盘容量以易读的容量格式…

做抖店如何提高与达人合作的几率?有效筛选+有效推品

我是王路飞。 总是有很多新手商家&#xff0c;找我吐槽&#xff0c;抖音上的达人特别不好找&#xff0c;好不容易加上了&#xff0c;要么是发消息不回复&#xff0c;要么是寄样后就没下文了。 虽然一直都说找达人带货玩法比较简单&#xff0c;但也离不开电商的基本逻辑&#…

【数据可视化01】matplotlib实例介绍1

目录 一、引言二、实例介绍1.柱状图1)简单柱状图2)堆叠柱状图 2.线条形式3.折线图&#xff08;多子图&#xff09;4.散点图5.水平和垂直线条6.饼状图1&#xff09;饼状图2&#xff09;“条形饼”图 一、引言 matplotlib是一个用于绘制数据可视化的Python库。它可以创建各种静态…

制造业如何挖掘数据价值,附数据分析处理软件推荐

制造业如何挖掘和利用数据价值&#xff1f; 在信息化、智能化高速发展的今天&#xff0c;制造业正迎来一场由数据驱动的深刻变革。数据&#xff0c;作为这场变革的核心驱动力&#xff0c;正被制造业企业深度挖掘和利用&#xff0c;以实现更高效、更智能的生产模式。 制造业在利…

252 基于MATLAB的自适应差分阈值法检测心电信号的QRS波

基于MATLAB的自适应差分阈值法检测心电信号的QRS波&#xff0c;QRS波群反映左、右心室除极电位和时间的变化&#xff0c;第一个向下的波为Q波&#xff0c;向上的波为R波&#xff0c;接着向下的波是S波。通过GUI进行数据处理&#xff0c;展示心率和QRS。程序已调通&#xff0c;可…

44.WEB渗透测试-信息收集-域名、指纹收集(6)

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 内容参考于&#xff1a; 易锦网校会员专享课 上一个内容&#xff1a; web指纹&#xff1a; 每一个网站&#xff0c;前端开发语言&#xff0c;后端语言&#…