wordpress网站访问慢,网站建设35类,做市场调查的网站,东莞市建设工程交易中心网关于Spring Cloud系列我们其实讲解了很多#xff0c;但是这里我们介绍一下Spring Cloud Config#xff0c;它是一个解决分布式系统的配置管理方案#xff0c;他包含了Client 和 Server 两个部分#xff0c;server提供配置文件的存储#xff0c;以接口的方式将配置文件内容…关于Spring Cloud系列我们其实讲解了很多但是这里我们介绍一下Spring Cloud Config它是一个解决分布式系统的配置管理方案他包含了Client 和 Server 两个部分server提供配置文件的存储以接口的方式将配置文件内容提供出去Client通过接口获取相关数据并依据数据初始化自己的应用Spring Cloud 使用git或者svn存放配置文件默认情况下使用git。
我们第一步在github上创建一个文件夹Springcloud-config用来存放配置文件我们可以创建三配置文件分别如下
//开发环境
springcloud-config-dev.properties
//测试环境
springcloud-config-test.properties
//生产环境
springcloud-config-pro.properties之后我们为每个配置文件都写一个springcloud.miaow,属性值分别是你好miaow-dev/test/pro。
springcloud:miaow: hello,miaow-devspringcloud:miaow: hello,miaow-testspringcloud:miaow: hello,miaow-pro这个是我们正常的开发逻辑但是为了不一样我采用我自己的方式进行如下图所示我在gitee上进行 server端
pom.xml文件
?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.xsdparentartifactIdspringCloud/artifactIdgroupIdcom.miaow/groupIdversion0.0.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdspring-cloud-config-git/artifactIdnamespring-cloud-config-git/name!-- FIXME change it to the projects website --urlhttp://www.example.com/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.11/versionscopetest/scope/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-config-server/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies
/project配置相关文件Application.yml
server:port: 3421
spring:application:name: spring-cloud-config-servercloud:config:server:git:uri: https://github.com/ # git仓库的地址search-paths: /**/springcloud-config-server # git仓库地址下的相对地址可以配置多个用,分割。username: #Git仓库用户名password: #Git仓库密码启动类
EnableConfigServer
SpringBootApplication
public class GitApplication
{public static void main( String[] args ){SpringApplication.run(GitApplication.class);}
}客户端Client
?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.xsdparentartifactIdspringCloud/artifactIdgroupIdcom.miaow/groupIdversion0.0.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdspring-cloud-config-git/artifactIdnamespring-cloud-config-git/name!-- FIXME change it to the projects website --urlhttp://www.example.com/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.11/versionscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-config/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies
/project配置文件Application.yml
注意这个是我们作为客户端的配置文件可以进行多个添加我们根据Spring Boot解析配置文件的先后顺序分别创建两个文件 application.yml和bootstrap.properties。
application
server:port: 4322
spring:application:name: spring-cloud-config-clientbootstrap
spring.cloud.config.namespringcloud-config # 如果以我的方式springcloud-zuul-dev。
spring.cloud.config.profiledev
spring.cloud.config.urihttp://localhost:8080/
spring.cloud.config.labelmasterspring.application.name对应{application}部分
spring.cloud.config.profile对应{profile}部分
spring.cloud.config.label对应git的分支。如果配置中心使用的是本地存储则该参数无用
spring.cloud.config.uri配置中心的具体地址
spring.cloud.config.discovery.service-id指定配置中心的service-id便于扩展为高可用配置集群。启动类
SpringBootApplication
public class ClientApplication {public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);}
}创建一个控制层
RestController
public class HelloController {Value(${springcloud.miaow})private String hello;RequestMapping(/hello)public String from() {return this.hello;}
}之后我们启动项目并访问http://localhost:4322/hello
如果返回hello,miaow-dev,说明已经正确的从server端获取到了参数。到此一个完整的服务端提供配置服务客户端获取配置参数的例子就完成了。
将配置文件上传到GitHub仓库中按照以下格式命名
{application}-{profile}.properties结束
使用GitHub作为配置中心的好处如下
集中管理配置文件将所有应用程序的配置文件存储在一个地方方便管理和维护。动态刷新配置当配置文件发生变化时Spring Cloud应用程序可以自动获取最新的配置无需重启应用程序。安全性GitHub提供了访问控制和身份验证功能可以保证配置文件的安全性。可扩展性可以使用Git的分支和标签功能为不同的环境和版本管理不同的配置文件。可视化管理可以使用GitHub的Web界面方便地查看和编辑配置文件。