网站建设白云,医院做网站,美妆网站怎么做,新加坡域名注册商1.概述 在本教程中#xff0c;我们将回顾Spring Cloud Config Server的基础知识。 我们将设置一个Config Server #xff0c;然后构建一个客户端应用程序 #xff0c;该客户端应用程序在启动时会消耗配置 #xff0c;然后刷新配置而不重新启动。 我们正在构建的应用程序与《… 1.概述 在本教程中我们将回顾Spring Cloud Config Server的基础知识。 我们将设置一个Config Server 然后构建一个客户端应用程序 该客户端应用程序在启动时会消耗配置 然后刷新配置而不重新启动。 我们正在构建的应用程序与《 集中式配置入门指南 》中讨论的“ Hello World”应用程序相同但是在本文中我们将更深入地介绍Spring Cloud Config Server的概念。 本教程的完整源代码在Github上 。 2.什么是Spring Cloud Config Server 正如文档简要指出的那样“ Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。” 服务器存储后端的默认实现使用git 因此它轻松支持带标签的配置环境版本并且许多用于管理内容的工具都可以使用它。 Spring Cloud Config 非常适合Spring应用程序因为它的客户端和服务器概念都精确地映射到Spring Environment和PropertySource抽象。 但是Spring Cloud Config可以与以任何语言运行的任何应用程序一起使用。 3.创建一个多模块项目 我们正在创建的应用程序将具有两个模块一个模块用于配置服务另一个模块用于配置客户端。 因此我们需要创建一个父pom 。 3.1父母 在我们的IDE中让我们创建一个新项目。 我正在使用Spring Tool Suite但这只是个人喜好。 在我们的pom.xml中 我们指定两个模块 ?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 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.michaelcgood/groupIdartifactIdcom.michaelcgood/artifactIdversion0.0.1/versionpackagingpom/packagingnamemichaelcgood-spring-cloud-config-server/namedescriptionIntro to Spring Cloud Config Server/descriptionmodulesmodulemcg-configuration-client/modulemodulemcg-configuration-service/module/modules/project3.2配置服务 在我们的IDE中让我们为配置服务创建一个新的Maven模块并将其插入到pom中 ?xml version1.0?
project xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancemodelVersion4.0.0/modelVersiongroupIdcom.michaelcgood/groupIdartifactIdmcg-configuration-service/artifactIdversion0.0.1/versionpackagingjar/packagingnamemcg-configuration-service/nameparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.5.9.RELEASE/versionrelativePath / !-- lookup parent from repository --/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-config-server/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionEdgware.RELEASE/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build
/project3.3配置客户端 现在我们只需要为我们的配置客户端创建一个模块。 因此让我们制作另一个Maven模块并将其插入到pom中 ?xml version1.0?
project xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancemodelVersion4.0.0/modelVersiongroupIdcom.michaelcgood/groupIdartifactIdmcg-configuration-client/artifactIdversion0.0.1/versionpackagingjar/packagingparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.5.9.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-config/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionEdgware.RELEASE/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build
/project 我们的项目结构现在看起来像这样 4.配置服务器 现在我们将创建一个Config Servic e以充当客户端和git存储库之间的中介。 4.1启用配置服务器 我们使用Spring Cloud的EnableConfigServer创建可以与之通信的配置服务器。 因此这只是一个普通的Spring Boot应用程序其中添加了一个注释以启用Config Server 。 EnableConfigServer
SpringBootApplication
public class ConfigServiceApplication {public static void main(String[] args) {SpringApplication.run(ConfigServiceApplication.class, args);}
}4.2 application.properties 为确保Config Service和客户端的端口之间没有冲突我们为Config Service指定了另一个端口 server.port8888spring.cloud.config.server.git.uri${HOME}/Desktop/mcg-config 第二行spring.cloud.config.server.git.uri $ {HOME} / Desktop / mcg-config指向git存储库我们将在其下创建它。 4.3 Git 在* nix系统上我们可以在命令行上执行所有操作。 我们在桌面上创建一个文件夹 mkdir mcg-config 我们使用vim创建一个名为a-bootiful-client.properties的文件 vim a-bootiful-client.properties 我们添加了消息“ Hello World”但这可能是我们想要的。 写w之后我们退出qvim。 现在让我们创建一个新的仓库 git init 现在我们添加包含消息的文件 git add a-bootiful-client.properties 让我们提交 git commit5.配置客户端 现在让我们创建一个新的Spring Boot应用程序该应用程序使用Config Server加载其自己的配置并刷新其配置以按需反映对Config Server的更改而无需重新启动JVM。 Spring将看到配置属性文件就像从application.properties application.yml或任何其他PropertySource加载的任何属性文件一样。 5.1反映变化 客户端可以使用标准的Spring方法访问Config Server中的任何值例如ConfigurationProperties或Value“ $ {…}}” 。 考虑到这一点我们创建一个REST控制器该控制器返回已解析的message属性的值 SpringBootApplication
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}
}RefreshScope
RestController
class MessageRestController {Value(${message:Hello default})private String message;RequestMapping(/message)String getMessage() {return this.message;}
} 默认配置仅允许在客户端启动时读取值而不是再次读取。 因此使用RefreshScope我们强制Bean刷新其配置这意味着它将从Config Server中获取更新的值然后触发刷新事件。 5.2 bootstrap.properties 在引导阶段必须先读取配置Config Client的属性然后才能从Config Server中读取应用程序的其余配置。 我们指定客户端的spring.application.name以及配置服务器spring.cloud.config.uri的位置 spring.application.namea-bootiful-client
spring.cloud.config.urihttp://localhost:8888
management.security.enabledfalse 注意 我们通过设置management.security.enabled false禁用了安全性从而使我们的测试和修补变得容易。 6.演示 首先我们需要将目录更改为我们的配置服务并启动它 mcg-configuration-service mike$ mvn spring-boot:run 然后为我们的客户做同样的事情 mcg-configuration-client mike$ mvn spring-boot:run 当添加a-bootiful-client.properties时我们可以在终端中看到配置服务 INFO 5921 --- [nio-8888-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/dk/48l9cm2x3vnfl5ymh6dtxpwc0000gn/T/config-repo-7195892194658362240/a-bootiful-client.properties 让我们打开浏览器并访问http// localhost8080 / message 。 我们看到“ Hello World”。 现在让我们再次在a-bootiful-client.properties中更改消息这次输入“嗨 :-)”。 保存并提交后我们访问http// localhost8888 / a-bootiful-client / default确认更改。 现在我们调用Spring Boot Actuator引用端点来刷新客户端 curl -X POST http://localhost:8080/refresh 我们访问http// localhost8080 / message并看到我们的消息“嗨 -“ 被陈列。 有关Spring Boot Actuator的更多信息请参阅教程Building Spring Boot RESTful Service Spring Boot Actuator 。 7.结论 我们刚刚在Spring中完成了服务的集中配置。 我们通过站起来一个Spring Cloud Config Server并创建一个客户端来在启动时使用配置然后刷新配置而不重新启动来实现此目的。 我们没有接触过的Spring Cloud Config Server可以完成许多其他事情例如 让Config Server向Spring Cloud NetflixEureka Service Discovery或Spring Cloud Consul的Discovery Service注册 提供YAML或属性格式的配置 服务纯文本配置文件 将配置服务器嵌入到应用程序中 完整的源代码可以在Github上找到。 翻译自: https://www.javacodegeeks.com/2017/12/intro-spring-cloud-config-server.html