手机网站什么意思,站群wordpress,宁乡电商网站建设收费,用ih5做微网站Config
概述
分布式系统当前面临的配置问题
微服务意味着要将单体应用中的业务拆分成一个个子服务#xff0c;每个服务的粒度相对较小#xff0c;因此系统中会出现大量的服务。
比如#xff1a;有n个微服务连接同一套数据库#xff0c;当连接数据库需要发生变动时…Config
概述
分布式系统当前面临的配置问题
微服务意味着要将单体应用中的业务拆分成一个个子服务每个服务的粒度相对较小因此系统中会出现大量的服务。
比如有n个微服务连接同一套数据库当连接数据库需要发生变动时需要改n多次 比如每一个微服务都有devprd环境 …
每一个微服务自己带着一个application.yml上百个配置文件的管理…/(T o T)/~~
由于每个服务都需要必要的配置信息才能运行所以一套集中式的、动态的配置管理设施是必不可少的SpringCloud提供了ConfigServer来解决这个问题
当前主流使用的配置中心有三种
configbusalibaba nacos携程 阿波罗
Config是什么 一句话统一共用的配置放在配置中心各自独立的配置各自保存
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config分为服务端和客户端两部分。
服务端也称为分布式配置中心它是一个独立的微服务应用用来连接配置服务器并为客户端提供获取配置信息加密/解密信息等访问接口
客户端也就是我们的微服务模块则是通过指定的配置中心来管理应用资源以及与业务相关的配置内容并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息这样就有助于对环境配置进行版本管理并且可以通过git客户端工具来方便的管理和访问配置内容
Config能干嘛
集中管理配置文件不同环境不同配置动态化的配置更新分环境部署比如dev/test/prod/beta/release运行期间动态调整配置不再需要在每个服务部署的机器上编写配置文件服务会向配置中心统一拉取配置自己的信息当配置发生变动时服务不需要重启即可感知到配置的变化并应用新的配置将配置信息以REST接口的形式暴露
config服务端配置与测试 git参考文档 https://blog.csdn.net/qq_44722674/article/details/117200397 https://lolitasian.blog.csdn.net/article/details/79085301 在GitHub上新建一个仓库springcloud-config 获得刚才新建的git地址gitgithub.com:mazhuorui/springcloud-config.git 在本地硬盘目录新建git仓库 比如我新建目录D:\app\appFile\gitRepository在该目录下执行以下命令 注意上面的操作不是必须的只要github上有就可以克隆到本地只是为了方便修改 新建Module模块cloud-config-center-3344
它即为Cloud的配置中心模块cloudConfig Center
pom文件 artifactIdcloud-config-center-3344/artifactIddependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-config-server/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependency!-- 引入自己定义的api通用包可以使用Payment支付Entity --groupIdcom.mzr.springcloud/groupIdartifactIdcloud-api-commons/artifactIdversion1.0-SNAPSHOT/version/dependency/dependenciesyml文件
server:port: 3344spring:application:name: cloud-config-center #注册进Eureka服务器的微服务名cloud:config:server:git:uri: gitgithub.com:mazhuorui/springcloud_config.git #GitHub上面的git仓库名字####搜索目录search-paths:- springcloud_config####读取分支label: master#服务注册到eureka地址
eureka:client:service-url:defaultZone: http://localhost:7001/eureka主启动类
SpringBootApplication
EnableConfigServer //加上该注解
EnableEurekaClient
public class ConfigCenterMain3344
{public static void main(String[] args) {SpringApplication.run(ConfigCenterMain3344.class, args);}
}windows下修改hosts文件增加映射127.0.0.1 config-3344.com
启动微服务3344先启动7001 测试通过Config微服务是否可以从GitHub上获取配置内容 http://config-3344com:3344/master/config-dev.yml 配置读取规则
/{label}-{nalrne}-{profiles}.yml
label:分支(branch)name :服务名profiles:环境(dev/test/prod)
建议使用第一种 不加分支名{label}默认是master分支 前两种返回的时yml内容第三种返回的是json串 config客户端配置与测试
新建cloud-config-client-3355模块
pom文件
artifactIdcloud-config-client-3355/artifactIddependenciesdependency!--这里与服务端不同,没有写server--groupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-config/artifactId///dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency/dependenciesapplicaiton. yml是用户级的资源配置项 bootstrap. yml是系统级的优先级更加高 Spring Cloud会创建一个“Bootstrap Context”作为Spring应用的Application Context的父上下文。 初始化的时候BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。 Bootstrap属性有高优先级默认情况下它们不会被本地配置覆盖。 Bootstrap context和Application Context有着不同的约定所以新增了一个bootstrap.yml文件保证Bootstrap Context和Application Context配置的分离。 一般会有bootstrap.yml、application.yml两个文件bootstrap.yml从外部获取共有的配置application.yml负责微服务独有的配置两个配置文件才能组合成一个完整的配置环境 本次只使用一个bootstrap.yml文件
server:port: 3355spring:application:name: config-clientcloud:#Config客户端配置config:label: master #分支名称name: config #配置文件名称profile: dev #读取后缀名称 上述3个综合master分支上config-dev.yml的配置文件被读取读取路径http://config-3344.com:3344/master/config-dev.ymluri: http://localhost:3344 #配置中心地址k#服务注册到eureka地址
eureka:client:service-url:defaultZone: http://localhost:7001/eureka主启动类
SpringBootApplication
EnableEurekaClient
public class ConfigClientMain3355{public static void main(String[] args) {SpringApplication.run(ConfigClientMain3355.class, args);}
}controller层
RestController
public class ConfigClientController {//将配置文件中config.info内容注入到configInfo变量中Value(${config.info}) private String configInfo;GetMapping(/configInfo)public String getConfigInfo(){return configInfo;}
}启动测试
可以发现3344和3355访问的内容是一致的
成功实现了客户端3355通过访问SpringCloud Config3344获取到GitHub上的配置信息 动态刷新
问题如果我们修改了GitHub上配置文件内容3344、3355还能获取到新的内容吗
Linux运维修改GitHub上的配置文件内容做调整比如修改config-dev.yml提交刷新3344发现ConfigServer服务端配置中心立刻响应得到最新值了刷新3355发现ConfigClient客户端没有任何响应拿到的还是旧值客户端3355没有变化除非自己重启或者重新加载才能拿到最新值难到每次运维修改配置文件客户端都需要重启噩梦
解决动态刷新问题
修改3355模块
POM引入actuator dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency修改YML暴露监控端口
server:port: 3355spring:application:name: config-clientcloud:#Config客户端配置config:label: master #分支名称name: config #配置文件名称profile: dev #读取后缀名称 上述3个综合master分支上config-dev.yml的配置文件被读取读取路径http://config-3344.com:3344/master/config-dev.ymluri: http://localhost:3344 #配置中心地址k#服务注册到eureka地址
eureka:client:service-url:defaultZone: http://localhost:7001/eureka#暴露监控端点
management:endpoints:web:exposure:include: *RefreshScope业务类Controller修改
RestController
RefreshScope//自动刷新注解
public class ConfigClientController
{Value(${config.info})private String configInfo;GetMapping(/configInfo)public String getConfigInfo(){return configInfo;}
}此时我们客户端的配置已经完成了我们再去刷新3355访问3355发现拿到的依然时旧值
最后一步操作向客户端发送post请求刷新3355相当于告诉3355GitHub内容已更新
curl -X POST “http://localhost:3355/actuator/refresh” 两个必须1.必须是 POST 请求2.请求地址http://localhost:3355/actuator/refresh 假如有多个微服务客户端3355/3366/3377…
每个微服务都要执行一次post请求手动刷新?
可否广播一次通知处处生效我们想大范围的自动刷新并且还能够定点通知、精确通知比如100台
中我要剔除2台通知其他98台求方法~ 好哥们儿消息总线Bus不请自来
点击即学