SpringCloud 分布式配置中心搭建

服务端配置

配置中心单独作为一个服务

依赖配置

<dependencies>
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-config-server</artifactId>
	</dependency>
	<!--如果需要注册到Eureka-->
	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
<build>
	<resources>
	    <resource>
	        <directory>src/main/java</directory>
	        <includes>
	            <include>**/*.properties</include>
	            <include>**/*.xml</include>
	        </includes>
	        <filtering>false</filtering>
	    </resource>
	    <resource>
	        <directory>src/main/resources</directory>
	        <includes>
	            <include>**/*.properties</include>
	            <include>**/*.xml</include>
	            <include>**/*.txt</include>
	            <!--让秘钥可见-->
	            <include>**/*.keystore</include>
	        </includes>
	        <filtering>false</filtering>
	    </resource>
	</resources>
</build>

启动类配置

@EnableConfigServer

配置文件配置

bootstrap.properties

server.port=8085

eureka.client.serviceUrl.defaultZone=http://admin:admin@localhost:8763/eureka/
spring.application.name=config-server
#远程代码仓库
spring.cloud.config.server.git.uri=https://github.com/Asperger12345/micro-config-server
#项目名
spring.cloud.config.server.git.search-paths=/**
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
#本地缓存目录
spring.cloud.config.server.git.basedir=C:/work/config/tmp
#强制从GitHub配置中心中拉取配置信息,不走缓存
spring.cloud.config.server.git.force-pull=true

#加密配置
encrypt.key-store.location=config-server.keystore
encrypt.key-store.alias=config-server
#秘钥库口令
encrypt.key-store.password=
#秘钥口令
encrypt.key-store.secret=

加密

在 JDK 安装文件夹下找到 keytool 工具,生成秘钥文件
在这里插入图片描述

keytool -genkeypair -alias config-server -keyalg RSA -keystore config-server.keystore -validity 365

其中,-alias 与配置文件中的 spring.application.name 对应

如果 JDK 在 C 盘,需要以管理员方式运行 cmd
在这里插入图片描述

cd C:\Program Files\Java\jdk1.8.0_221\jre\bin

在这里插入图片描述
在这里插入图片描述
自己记住口令
后面还有一堆需要自己设置的东西,最后输入y,回车确定
在这里插入图片描述
放到 resources 目录下
在这里插入图片描述
利用 Postman 软件加密/解密
加密:http://localhost:8085/encrypt
解密:http://localhost:8085/decrypt
在这里插入图片描述
在中央仓库的配置文件里,写密码的时候,写上面的密文,且前面加上 {cipher}

客户端配置

依赖配置

<dependencies>
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-config</artifactId>
	    <version>LATEST</version>
	</dependency>
	<!--如果需要重试功能-->
	<dependency>
	    <groupId>org.springframework.retry</groupId>
	    <artifactId>spring-retry</artifactId>
	</dependency>
	<!--手动刷新配置文件-->
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
	<!--消息总线-->
	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
</dependencies>

配置文件配置

bootstrap.properties

#环境
spring.cloud.config.profile=dev
#分支
spring.cloud.config.label=master
#spring.cloud.config.uri=http://localhost:8085/
#开启服务发现功能,不用uri的方式
spring.cloud.config.discovery.enabled=true
#服务发现的服务名称
spring.cloud.config.discovery.service-id=config-server

#快速失败与重试
#如果连接不上获取配置有问题,快速响应失败
spring.cloud.config.fail-fast=true
#默认重试的间隔时间,默认1000ms
spring.cloud.config.retry.multiplier=1000
#下一间隔时间的乘数,默认是1.1
#spring.cloud.config.retry.initial-interval=1.1
#最大间隔时间,最大2000ms
spring.cloud.config.retry.max-interval=2000
#最大重试次数,默认6次
spring.cloud.config.retry.max-attempts=6

#消息总线,每一个端点都要配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
#刷新配置url  http://localhost:8085/actuator/bus-refresh
spring.cloud.bus.refresh.enabled=true
spring.cloud.bus.trace.enabled=true

刷新配置文件

EurekaClient 里的 bean 通过 @Value 注入的配置。但 bean 不刷新的话,新配置就无法生效。而且EurekaServer 从 GitHub 上拉取的配置文件也不会自动同步到 EurekaClient 。如何解决呢?

在有 @Value 进行注入值的类上,打上注解

@RefreshScope

请求
http://localhost:8085/actuator/refresh
就能刷新读取配置文件

如果有集群,就要按不同 port 请求多次 http://localhost:port/actuator/refresh

可以使用消息总线解决上面请求多次的问题,在配置文件里可以看到相关配置。
post 请求 http://localhost:port/actuator/bus-refresh

如果一次也不想请求呢?

可以把这个 URL 配置到 GitHub 上的这个项目的 Settings 中,每当这个项目中有更改都会调用这个 URL:
在这里插入图片描述
这里的 URL 需要是公网 IP,或者使用内网穿透技术。

Spring Cloud是一种基于Spring框架的开源分布式架构解决方案,它提供了一系列工具和组件,用于简化构建分布式系统的过程。 搭建Spring Cloud分布式架构主要包括以下几个步骤: 1. 环境准备:首先要准备好需要的开发环境,包括Java开发环境、Eclipse或者IntelliJ IDEA等开发工具,以及Maven项目管理工具。 2. 创建项目:通过Maven创建一个Spring Boot项目作为基础项目。可以使用Spring Initializr或者手动创建一个基本的Spring Boot项目。 3. 添加依赖:在创建的Spring Boot项目中,添加Spring Cloud的相关依赖,如spring-cloud-starter-netflix-eureka-server、spring-cloud-starter-config等,这些依赖将提供各种分布式系统所需的功能。 4. 配置服务注册中心:通过在配置文件中配置服务注册中心,可以使用Eureka或Consul等作为服务注册中心。服务注册中心用于服务的发现与注册,确保每个服务的可用性。 5. 编写业务代码:在项目中编写对应的微服务业务代码,如提供用户服务、订单服务等。每个微服务都是独立的应用程序,可以通过服务间的调用来实现不同微服务之间的协作。 6. 配置服务间的通信:通过使用Feign或Ribbon等组件,可以方便地实现服务间的通信。Feign提供了声明式的HTTP客户端,而Ribbon可以实现客户端负载均衡等功能。 7. 配置服务网关:使用Zuul等组件配置服务网关,可以实现对外部的请求进行路由和负载均衡。服务网关可以提供统一的API入口,并且可以进行安全认证等操作。 8. 配置分布式配置中心:通过使用Spring Cloud Config,可以将所有的配置文件放在统一的配置中心,实现动态的配置管理。 9. 配置服务容错保护:使用Hystrix等组件,可以实现对服务的容错保护。Hystrix可以控制对依赖服务的访问,防止级联故障并提供故障恢复机制。 10. 部署和运行:最后,将编写好的项目打包成JAR包,并进行部署和运行,可以使用Docker等技术进行容器化部署,提高项目的可扩展性和可维护性。 通过以上步骤,就可以搭建起一个基于Spring Cloud的分布式架构。Spring Cloud提供了丰富的组件和功能,可以帮助开发人员快速构建和部署分布式系统,并提高系统的可用性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值