目录认识Spring Cloud ConfigConfig Server读取配置文件步骤11创建config-server项目2在config-server中开启Config Server功能3在config-server配置文件进行相关配置4在config-server中创建配置文件步骤2搭建Config Client1创建config-client项目导入依赖2在config-client配置文件进行相关配置3在config-client中添加方法步骤3测试运行从Git仓库读取配置文件步骤1创建Git远程仓库并创建配置文件1百度搜索“码云”打开Git官网并注册登录2登录成功后创建仓库3创建仓库成功后在仓库中新建yml文件输入配置内容提交,然后点击加号新建文件夹步骤2修改Config Server的配置文件步骤3测试运行bootstrap.yml与application.yml的区别搭建高可用Config Server步骤1创建Eureka Server步骤3改造Config Client项目步骤4搭建Config Server集群步骤5测试运行认识Spring Cloud Config概述Spring Cloud Config为Spring应用提供了便捷的配置管理解决方案并且可以与其他编程语言编写的应用程序协同工作。Spring Cloud Config通过配置服务器即Config Server和配置客户端即ConfigClient为分布式系统提供外部配置支持。Config Server作为一个独立的微服务负责从Git等存储库加载配置并向需要它的微服务公开这些配置信息。该方案支持环境隔离功能能够实现多环境的代码共享以及针对特定环境的配置管理需求。所有环境配置数据由中央服务器统一维护并通过Git进行版本控制。为了保护敏感信息的安全它提供了加密与解密机制并可通过EnableConfigServer注解轻松集成到Spring Boot应用中。此外这种集中管理方式确保了配置信息的一致性和安全性同时简化了跨环境部署的复杂性。Config Client - Config Server -git RepoConfig Server读取配置文件从本地仓库读取配置文件步骤1.搭建config-server2.搭建config-client3.测试运行步骤1创建父工程config-1在其中搭建Config Server1创建config-server项目导入依赖使用Spring Initializr方式创建一个名称为config-server的Spring Boot项目将Artifact命名为config-server添加Config Server、Test依赖。其中Config Server依赖如下dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-config-server/artifactId version2.0.0.RELEASE/version /dependency2在config-server中开启Config Server功能在程序的启动类ConfigServerApplication添加EnableConfigServer注解开启Config Server功能。3在config-server配置文件进行相关配置在项目的配置文件application.yml中进行相关配置包括指定服务名、端口号、本地读取配置以及读取路径。4在config-server中创建配置文件在项目的resource目录下建一个 bushuo 文件夹用于存放本地配置文件。在 bushuo目录下新建一个config-client-dev.yml文件用作后续将要创建的config-client工程的dev开发环境的配置文件。在config-client-dev.yml配置文件中配置端口号和自定义变量。步骤2搭建Config Client1创建config-client项目导入依赖使用Spring Initializr方式创建一个名称为config-client的Spring Boot项目将Artifact命名为config-client添加Config、Web和Test的起步依赖。其中Config依赖如下dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-config/artifactId version2.0.0.RELEASE/version /dependency2在config-client配置文件进行相关配置在resources文件下新建bootstrap.yml文件。3在config-client中添加方法为了更直观的看到配置文件config-client-dev.yml被读取创建controller包下的ConfigController类添加一个hi()方法进行测试。步骤3测试运行先启动config-server项目再启动config-client项目观察config-client控制台日志。使用浏览器访问config-client的请求方法http://localhost:8002/hi。观察输出结果。从Git仓库读取配置文件步骤1.创建Git远程仓库并创建配置文件2.修改Config Server的配置文件3.测试运行步骤1创建Git远程仓库并创建配置文件在远程Git仓库创建一个名称为hello的公开仓库然后在公开库中创建一个和config-client-dev.yml相同的文件。1百度搜索“码云”打开Git官网并注册登录gitee2登录成功后创建仓库新建仓库内容如下3创建仓库成功后在仓库中新建yml文件输入配置内容提交,然后点击加号新建文件夹往下翻点击提交步骤2修改Config Server的配置文件点击“克隆”按钮复制uri地址修改username与password的值为登录Git的用户名和密码。修改Config Server的配置文件application.yml。配置服务名、端口号、远程Git仓库的地址、文件夹地址、用户名、密码、分支名等。步骤3测试运行重启config-server和config-client观察config-client控制台日志。使用浏览器访问http://localhost:8002/hi。bootstrap.yml与application.yml的区别加载顺序若application.yml 和bootstrap.yml 在同一目录下bootstrap.yml 先加载 application.yml后加载配置区别bootstrap.yml 和 application.yml 都可以用来配置参数。bootstrap.yml 用来程序引导时执行应用于更加早期配置信息读取。可以理解成系统级别的一些参数配置这些参数一般是不会变动的。一旦bootstrap.yml 被加载则内容不会被覆盖。application.yml 可以用来定义应用级别的 应用程序特有配置信息可以用来配置后续各个模块中需使用的公共参数等。属性覆盖问题application.yml 的内容标签与 bootstrap 的标签一致application 也不会覆盖 bootstrap而 application.yml 里面的内容可以动态替换注意git的配置文件与项目name一致思考为新的config客户端config-client-another在Git中添加配置文件运行测试。1创建config-client-another项目导入依赖其中Config依赖如下dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-config/artifactId version2.0.0.RELEASE/version /dependency2在resources 新建bootstrap.yml文件spring: application: name: config-client-another cloud: config: uri: http://localhost:8001 fail-fast: true profiles: active: dev(3)添加方法创建controller包configcontroller添加方法hello1进行测试package com.bushuo.configclientanother.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; RestController public class configcontroller { Value( ${hello1} ) String hello1; RequestMapping(value /hello1) public String hello1(){ return hello1; } }4在git中在hello下创建config-client-another-dev.yml5运行测试搭建高可用Config Server服务实例很多时所有的服务实例需要同时从配置中心Config Server读取配置文件这时可以考虑将配置中心Config Server做成一个集群化的微服务从而达到高可用。将Config Server和Config Client注册在Eureka Server.步骤1.创建Eureka Server2.改造Config Server项目3.改造Config Client项目4.搭建Config Server集群5.测试运行步骤1创建Eureka Server步骤1创建项目引入依赖1使用Spring Initializr方式创建一个名称为eureka-server的Spring Boot项目将Artifact命名为eureka-server在pom.xml文件中添加Eureka Server依赖。方便测试效果新建一个eureka-server项目作为Config Server的注册中心将eureka-server端口号设置为7001。?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.0.6.RELEASE/version relativePath/ !-- lookup parent from repository -- /parent groupIdcom.bushuo/groupId artifactIdeureka-server/artifactId version0.0.1-SNAPSHOT/version nameeureka-server/name descriptionDemo project for Spring Boot/description url/ licenses license/ /licenses developers developer/ /developers scm connection/ developerConnection/ tag/ url/ /scm properties java.version1.8/java.version /properties dependencies dependency groupIdorg.springframework.cloud/groupId artifactId spring-cloud-starter-netflix-eureka-server /artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency /dependencies dependencyManagement dependencies dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-dependencies/artifactId versionFinchley.SR2/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin /plugins /build /project与springboot一致 2.0.6.RELEASE 的springcloud的依赖 Finchley.SR2 —2添加Eureka的相关配置在全局配置文件application.yml中添加Eureka的相关配置信息。3在项目启动类添加EnableEurekaServer注解在项目启动类EurekaServerApplication上添加EnableEurekaServer注解开启Eureka Server功能。4测试运行 http://localhost:7001步骤2改造Config Server项目1Config Server作为服务器需要在工程中的pom.xml配置文件中加入Eureka Client依赖。parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.0.6.RELEASE/version relativePath/ !-- lookup parent from repository -- /parent properties java.version1.8/java.version /properties dependencies dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-netflix-eureka-client/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency /dependencies dependencyManagement dependencies dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-dependencies/artifactId versionFinchley.SR2/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin /plugins /build /project2在项目的启动类ConfigServerApplication添加EnableEurekaClient和 EnableConfigServer注解开启Eureka Server和Config Server功能。3修改配置文件application.yml文件为Config Server指定服务注册的地址等信息。步骤3改造Config Client项目1在pom文件中加入Eureka Client起步依赖。在项目启动类上添加EnableEurekaClient注解启动Eureka Client功能。在配置文件bootstrap.yml加入指定服务注册地址等相关配置配置如下步骤4搭建Config Server集群搭建高可用的Config Server服务只需要将Config Server多实例部署使用Spring Initializr方式创建一个名称为config-server2的Config Server项目设置端口号为8003服务名也为config-server其他配置信息和搭建过程与config-server项目一致。步骤5测试运行使用浏览器访问http://localhost:7001。访问config-client的请求方法http://localhost:8002/hi。观察输入结果。停掉config-server服务再次访问请求观察还能否正常访问