网站用户界面设计,用dw如何做网站,h5教程入门,中国万网怎么自己做网站前些天发现了一个巨牛的人工智能学习网站#xff0c;通俗易懂#xff0c;风趣幽默#xff0c;忍不住分享一下给大家。点击跳转到教程。
一、 微服务系统中有多个服务应用#xff0c;也会有多个配置文件。此时也可用 springcloud bus 来实现对配置文件的管理。
PS#xf…前些天发现了一个巨牛的人工智能学习网站通俗易懂风趣幽默忍不住分享一下给大家。点击跳转到教程。
一、 微服务系统中有多个服务应用也会有多个配置文件。此时也可用 springcloud bus 来实现对配置文件的管理。
PSrabbitmq的安装见文章Docker 方式安装 RabbitMQribbitmq linux 部署
二、springcloud bus 的使用
1. 修改 config-client 工程。修改 pom、启动类、配置。
1.1 在 pom 中加入依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bus-amqp/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency
几经测试父级 pom 使用 2.1.6 始终不成功。故换为 2.0.3 版本。运行成功。完整 pom 如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.config/groupIdartifactIdconfig-client/artifactIdversion0.0.1-SNAPSHOT/versionnameconfig-client/namedescription配置文件管理 client/descriptionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.0.3.RELEASE/versionrelativePath//parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.versionspring-cloud.versionFinchley.RELEASE/spring-cloud.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--消息总线--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bus-amqp/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!--注册服务到 注册中心作为 Eureka 客户端--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-config/artifactId/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project1.2 启动类中加上 注解RefreshScope 。刷新配置文件 。
完整启动类
package com.config.configclient;import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
SpringBootApplication// 刷新配置
RefreshScope
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}Value(${version})String version;RequestMapping(value /getVersion)public String getVersion() {return version;}/*** 增加此方法用以解决报错Could not resolve placeholder version in value ${version}* return*/Beanpublic static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {PropertySourcesPlaceholderConfigurer c new PropertySourcesPlaceholderConfigurer();c.setIgnoreUnresolvablePlaceholders(true);return c;}}1.3 配置文件调整为2份 application.properties 、bootstrap.properties 。分别配置不同内容。
bootstrap.properties
# 项目名
spring.application.nameconfig-client# 端口
server.port4445# 指定远程仓库分支
spring.cloud.config.labelmaster# 读取文件dev开发环境、test测试、pro生产
spring.cloud.config.profiledev# 配置文件管理中心 config-server 地址
# 配置中心-集群模式时不使用 ip方式
# spring.cloud.config.urihttp://localhost:3333/# 配置中心-集群模式 start -----------------# 注册中心 - 端口 1234、工程名 eureka 见 eureka 工程中配置。
eureka.client.serviceUrl.defaultZone http://localhost:1234/eureka/# 从配置中心读取文件
spring.cloud.config.discovery.enabledtrue# 配置文件管理中心服务名 config-server
spring.cloud.config.discovery.serviceIdconfig-server# 配置中心-集群模式 end ----------------- application.properties
# rabbitmq 心跳检查
eureka.client.healthcheck.enabledtrue# rabbitmq 服务所在ip
spring.rabbitmq.hostergouzi.fun# rabbitmq 端口
spring.rabbitmq.port5672# rabbitmq 账号
spring.rabbitmq.usernamejiangyu# rabbitmq 密码
spring.rabbitmq.passwordloveU# 消息总线相关
spring.cloud.bus.enabledtrue
spring.cloud.bus.trace.enabledtrue
management.endpoints.web.exposure.includebus-refresh
2. 启动 eureka 、config-server。
3. 分不同端口号启动 2 个 config-client 端口号为 4444、4445 eureka 中注册服务为 4. 确认配置文件内容为 5. 分别请求 4444 和 4445 6. 以往配置文件更新后需要重启工程才会读取到最新的配置内容。
使用消息总线 则只需要向任意一个 config-client 发送一个更新配置信息的 post 类型提示请求便可。
请求 URL 为http://localhost:4444/actuator/bus-refresh 返回状态码 204 表示请求成功服务端无内容返回。
此时更新配置文件流程是
1前提不同 config-server、config-client 向 eureka 注册。
2远程配置文件更新。
3接口向任意一个 config-client 发起请求http://localhost:4444/actuator/bus-refresh 表示更新配置。
4 config-client 收到请求并发起消息到消息总线。
5消息总线向其它应用服务传递最新配置信息内容整个系统配置文件更新完成。
-------------------------------------------------------------
下一篇springCloud - 第10篇 - 服务间调用追踪 zipkin 的使用
源码见
https://gitee.com/FJ_WoMenDeShiJie/springcloud-base
https://gitee.com/FJ_WoMenDeShiJie/springcloud-config-server
https://gitee.com/FJ_WoMenDeShiJie/springcloud-config-client
-------------------------------------------------------------
PS这个系列不定时更新只是个人的学习分享
内容全程参考书目
《Spring Cloud 与 Docker 微服务架构空实战 》、
《Spring Cloud 微服务实战》、
《深入理解 Spring Cloud 与微服务构建》、
及大神博客https://blog.csdn.net/forezp/article/details/70148833 ----------------------------------------------------------------