zl程序教程

您现在的位置是:首页 >  后端

当前栏目

springcloud config 分布式配置中心

2023-09-27 14:26:36 时间

1、下载安装: docker toolbox
windows下载:https://www.docker.com/products/docker-toolbox
下载后控制台执行命令:docker-machine create –engine-registry-mirror=”https://s0iielsh.mirror.aliyuncs.com” –engine-insecure-registry=”0.0.0.0/0” -d virtualbox default
这时可能提示没有boot2docker镜像,这样默认就会通过git下载,但是速度实在受不了,根本跑不动,但是可以直接通过安装的docker文件夹中找到
这里写图片描述
直接复制到C:\Users\Administrator.docker\machine\cache

2、 查看虚拟机IP等信息:docker-machine env default
注:如果想直接在windows命令窗口内使用docker命令,将上一条命令输出的内容,复制粘贴到控制台执行一次即可
3、 创建docker中的网络
docker network create dongnao_net

4、 运行gitlab
docker run -d –net=dongnao_net –publish 1443:443 –publish 18080:80 –name gitlab –restart always gitlab/gitlab-ce:latest
端口18080,通过你的虚拟机IP取访问就可以看到页面了

5、 运行rabbitmq
docker run -d –net=dongnao_net –name rabbitmq –publish 5671:5671 –publish 5672:5672 –publish 4369:4369 –publish 25672:25672 –publish 15671:15671 –publish 15672:15672 rabbitmq:management
连接的端口是 5672
web控制台是 15672
6、说明
Oracle VM VirtualBox是一个管理虚拟机的工具
Docker Quickstart Terminal是用于连接操作虚拟机的

由于虚拟机经常启动失败,我遇到失败的原因经常是vboxdrv服务没有安装或没有成功启动;
解决办法:开始后第一件事就尝试打开虚拟机,如果不成功就找到安装目录下的vboxdrv文件夹,
如C:\Program Files\Oracle\VirtualBox\drivers\vboxdrv,右击VBoxDrv.inf,选安装,然后重启电脑。
再在virturalBox里启动虚拟机,然后打开docker,运行命令 docker start rabbitmq来启动rabbitmq,服务器启动就正常了,这时也可以在浏览器上访问rabbitmq和gitlab了。


说明:以下代码注释说明得非常清楚,就不多做解释,有疑问的欢迎留言提问!
1、pom文件


 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" 

 modelVersion 4.0.0 /modelVersion 

 !-- spring boot 封装spring 

 starter封装、自动配置autoconfiguration

 parent 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-parent /artifactId 

 version Dalston.SR1 /version 

 relativePath / 

 /parent 

 groupId com.dongnaoedu.springcloud /groupId 

 artifactId lession-2-eureka /artifactId 

 version 0.0.1-SNAPSHOT /version 

 dependencyManagement 

 dependencies 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-dependencies /artifactId 

 version Dalston.SR1 /version 

 type pom /type 

 scope import /scope 

 /dependency 

 /dependencies 

 /dependencyManagement 

 dependencies 

 !-- spring-boot-starter-web web项目,集成容器tomcat -- 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-web /artifactId 

 /dependency 

 !-- spring-boot-starter-actuator 管理工具/web 查看堆栈,动态刷新配置 -- 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-actuator /artifactId 

 /dependency 

 !-- cloud eureka组件 注册中心 -- 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-eureka-server /artifactId 

 /dependency 

 /dependencies 

 build 

 plugins 

 plugin 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-maven-plugin /artifactId 

 /plugin 

 /plugins 

 /build 

 /project 

2、application.yml:


import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApp { public static void main(String[] args) { new SpringApplicationBuilder(EurekaApp.class).web(true).run(args); }
serviceUrl: defaultZone: http://eureka1:8761/eureka/,http://eureka2:8762/eureka/,http://eureka3:8763/eureka/ instance: preferIpAddress: true

2、bootstrap.yml:


native: # 用本地文件夹存储配置,仅作配置示例,没起作用。要想起作用,将上面的 active中git 改为 native searchLocations: file:D:\\cloud\\dongnao\\configrepo # git仓库 gitlab地址 git: # 记得在先gitlab上创建一个对应的project uri: http://192.168.99.100:18080/root/project1.git search-paths: / username: root password: 12345678 repos: # 不同环境不同的库,这里的话,只有当应用中的spring.profiles.active=staging的时候才生效 lession-2-sms-sys-staging: pattern: */staging # 记得在先gitlab上创建一个对应的project uri: http://192.168.99.100:18080/root/lession-2-config-repo-staging.git # 不同项目不同库 lession-2-sms-webmvc: pattern: # 这里是根据服务名称匹配的spring.application.name - lession-2-sms-webmvc/** - lession-2-sms-webmvc* # 这里面的是本地git仓库的,不知道配置本地git仓库的也可以像上面一样配置成远程git地址 uri: file:D:\cloud\dongnao\config-repo # 加解密 encrypt: enabled: true # svn环境 # spring.profiles.active=subversion # spring.cloud.config.server.svn.uri=http:#127.0.0.1:1234/sms-sys/development/trunk # spring.cloud.config.server.svn.username=xxx # spring.cloud.config.server.svn.password=xxx

3、启动类


public static void main(String[] args) { new SpringApplicationBuilder(ConfigServerApplication.class).web(true).run(args); }

4、pom文件


 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" 

 modelVersion 4.0.0 /modelVersion 

 !-- spring boot 封装spring 

 starter封装、自动配置autoconfiguration

 parent 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-parent /artifactId 

 version Dalston.SR1 /version 

 relativePath / 

 /parent 

 groupId com.dongnaoedu.springcloud /groupId 

 artifactId lession-2-config-server /artifactId 

 version 0.0.1-SNAPSHOT /version 

 dependencyManagement 

 dependencies 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-dependencies /artifactId 

 version Dalston.SR1 /version 

 type pom /type 

 scope import /scope 

 /dependency 

 /dependencies 

 /dependencyManagement 

 dependencies 

 !-- spring-boot-starter-web web项目,集成容器tomcat -- 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-web /artifactId 

 /dependency 

 !-- spring-boot-starter-actuator 管理工具/web 查看堆栈,动态刷新配置 -- 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-actuator /artifactId 

 /dependency 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-eureka /artifactId 

 /dependency 

 !-- spring-boot-starter-security -- 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-security /artifactId 

 /dependency 

 !-- cloud config组件 配置中心 -- 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-config-server /artifactId 

 /dependency 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-bus-amqp /artifactId 

 /dependency 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-config-monitor /artifactId 

 /dependency 

 /dependencies 

 build 

 plugins 

 plugin 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-maven-plugin /artifactId 

 /plugin 

 /plugins 

 /build 

 /project 

 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" 

 modelVersion 4.0.0 /modelVersion 

 !-- spring boot 封装spring 

 starter封装、自动配置autoconfiguration

 parent 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-parent /artifactId 

 version Dalston.SR1 /version 

 relativePath / 

 /parent 

 groupId com.tony /groupId 

 artifactId lession-2-sms-sys /artifactId 

 version 0.0.1-SNAPSHOT /version 

 dependencyManagement 

 dependencies 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-dependencies /artifactId 

 version Dalston.SR1 /version 

 type pom /type 

 scope import /scope 

 /dependency 

 /dependencies 

 /dependencyManagement 

 dependencies 

 !-- spring-boot-starter-web web项目,集成容器tomcat -- 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-web /artifactId 

 /dependency 

 !-- spring-boot-starter-actuator 管理工具/web 查看堆栈,动态刷新配置 -- 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-actuator /artifactId 

 /dependency 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-eureka /artifactId 

 /dependency 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-config /artifactId 

 /dependency 

 !-- spring bus 事件通知实现自动刷新 -- 

 dependency 

 groupId org.springframework.cloud /groupId 

 artifactId spring-cloud-starter-bus-amqp /artifactId 

 /dependency 

 dependency 

 groupId redis.clients /groupId 

 artifactId jedis /artifactId 

 version 2.9.0 /version 

 /dependency 

 /dependencies 

 build 

 plugins 

 plugin 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-maven-plugin /artifactId 

 /plugin 

 /plugins 

 /build 

 /project 

2、application.yml


# 这里写成eureka1,是因为我修改系统的hosts, 127.0.0.1 eureka1 defaultZone: http://eureka1:8761/eureka instance: preferIpAddress: true

3、bootst.yml


profile: ${spring.profiles.active} # git仓库中,可以使用label来做不同版本的配置管理,默认是master,可以用来做版本管理。比如“2.0” label: master

4、启动类


import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan("com.dongnaoedu") @EnableEurekaClient public class SmsServiceApplication { public static void main(String[] args) { new SpringApplicationBuilder(SmsServiceApplication.class).web(true).run(args); }

5、controller


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.core.env.Environment; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope // 此处很重要 +++ public class SmsController { @Value("${tony.configString}") private String configString; // 测试注入tony.configString配置 @RequestMapping("/test") public String sendSms() { return "configString的值:" + configString; @Autowired Environment env; @RequestMapping("/get/{configName}") public String test(@PathVariable String configName) { return configName + "的值为:" + env.getProperty("tony." + configName); }

6、redis


import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import redis.clients.jedis.JedisPool; @Configuration @RestController @RequestMapping("/redis") public class RedisController { @Autowired Environment env; @Bean @RefreshScope// 在refresh之后,这个实例会被刷新 public JedisPool jedisPool() { String host = env.getProperty("redis.host"); int port = env.getProperty("redis.port", Integer.class); return new JedisPool(host, port); @Autowired private JedisPool jedisPool; // 获取一个key的值 并打印出来 @RequestMapping("/get/{key}") public String sendSms(@PathVariable String key) { String value = jedisPool.getResource().get(key); return " redis中" + key + "的值为:" + value; }

当gitlab上的配置改变了,直接调用接口获取到的值会是改变之前的值,得先调用refresh(post接口)接口刷新后再调用目标接口,这样才能获取到最新的值。


profile激活不同环境,加载不同的配置
普通mvc程序通过 xml中 beans profile="dev" ... /beans 方式可以进行配置


2、在xml配置文件中配置 context:property-placeholder location="classpath:sms.properties"/ 就可以设置${}里取到的变量值就是sms.properties里配置的值。
3、spring cloud config 工作模式
配置项怎么配置? 通过配置文件进行配置并存储在git中
谁去读配置文件? configserver配置中心
应用系统中配置信息从哪来? 从configserver远程获取
3、全局刷新怎么实现的?
通过rabbitmq发送消息, 订阅的客户端(应用系统)收到消息后,判断是否需要刷新
具体实现后面spring cloud stream/bus会讲到

4、自动刷新是什么一个机制?
gitlab提交配置更新的时候,触发一个事件(项目-设置-集成-webhook)

发起http请求到config-server monitor组件
http://192.168.99.1:8888/monitor 这是配置中心地址

configserver解析请求,通过spring cloud bus消息总线发送通知给各服务(和/bus/refresh一样)
对应的webapi类:PropertyPathEndpoint#notifyByPath
5、安全机制:如何保证配置的保密性?
项目中的密码 不应该明文存储,应该是密文
configserver访问控制:用户名密码
文件内容加密:configserver中设置spring.cloud.config.server.encrypt.enabled=true
再配置一个秘钥:encrypt.key=12345678
配置文件中密文需要以{cipher}做为标识:如spring.rabbitmq.password= ‘{cipher}31010f99731d4bd8aa7e3ce76152b5686265e1160043aac7cf769c3c8e1bb7ef’

对应的web api:EncryptionController#encrypt 加密
EncryptionController#decrypt 解密


mall-swarm微服务电商项目发布大更新,打造Spring Cloud最佳实践 之前mall项目更新到了SpringBoot 2.3.0版本,微服务版本mall-swarm也已同步更新了。此次更新完善了项目的Spring Cloud技术栈,升级至Spring Cloud Hoxton版本并加入了Spring Cloud Alibaba、Oauth2和Knife4j,致力于打造Spring Cloud 最佳实践项目!
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载