转载

Spring Cloud 配置服务器 -程序员入口

在优锐课的java核心笔记中,与讨论了关于更改微服务的属性可能会导致一个复杂的问题。 在本文中,我们将看到Spring Cloud Config Server和微服务如何相处。

在分布式系统中管理微服务的配置是一项繁琐且耗时的任务,尤其是当我们谈论的是由大量微服务组成的大型系统时。

每次需要更改微服务的配置时,都将转到相应的项目,更改其配置,然后重新启动应用程序以使更改生效。

通过引入Spring Cloud config实用程序项目解决了这一挑战,该项目为分布式系统中的外部化配置提供支持,并提供以下功能:

1.在一个中央存储库中管理分布式系统的所有模块的配置。

2.动态刷新配置,无需在更改配置后重新启动每个服务。

在本教程中,我们提供了一个设置

Spring Cloud配置服务的分步指南,除了构建一个客户端,该客户端在启动时使用中央配置,然后在不重新启动的情况下刷新其配置。

1-创建配置服务项目

打开Eclipse,然后创建一个新的Maven项目并将其命名为SpringCloudConfigServer。

2- 添加依赖关系

添加Spring Cloud Config服务器依赖项:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency>复制代码

然后添加以下依赖项管理属性:

<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>复制代码

仅此而已,只需添加Spring boot starter父级,你就可以开始了。

以下是完整的pom.xml供参考:

<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.programmer.gate</groupId><artifactId>SpringCloudConfigServer</artifactId><packaging>jar</packaging><version>0.0.1-SNAPSHOT</version><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project
复制代码

3-创建配置库

下一步是创建存储所有配置文件的存储库,你可以选择任何存储库系统,例如GIT,SVN等。在本教程中,我们使用GitHub。

·在桌面上创建一个名为spring-cloud-repo的新文件夹。

·分别创建2个属性文件,分别命名为client-A.properties和client-B.properties。

·这两个属性文件都仅具有以下简单属性:

client-A.properties:hello.message =来自客户端A的Hello

·client-B.properties:hello.message =来自客户端B的Hello

·每个属性文件都对应一个微服务模块,应命名为<微服务名称> .properties。

·在客户端项目中使用“ spring.application.name”定义微服务的名称。

·我们还可以添加一个名为“ application.properties”的通用文件,以存储所有客户端的通用配置。

·提交并推送到GIT文件夹。

4- 配置 application.properties

在src / main / resources下创建application.properties并添加以下配置:

server.port=8888spring.cloud.config.server.git.uri=https://github.com/husseinterek/spring-cloud-repo.git复制代码

在这里我们为配置服务器定义了一个自定义端口,并且还定义了存储配置文件的存储库的路径,该存储库的路径是从我们先前创建的GIT项目中复制的。

PS:如果我们没有定义自定义端口,Spring Cloud配置服务器会自动在localhost:8888上运行

5- Application.java

创建启动器类并使用@EnableConfigServer对其进行注释,这是将我们的项目标记为配置服务器的注释。

package com.programmer.gate;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}复制代码

6-启动配置服务器

以可执行jar形式启动服务器,并通过运行以下url来确保其充当配置服务器并公开客户端的配置文件:

http:// localhost:8888 / <客户端名称> / default

该URL应该以JSON格式返回在请求的配置文件下定义的配置属性。

测试客户端的配置后,我们得到以下结果:

Spring Cloud 配置服务器 -程序员入口

Spring Cloud 配置服务器 -程序员入口

Spring Cloud Configuration Server上的完整代码

7-创建Spring Cloud Client

现在,我们将创建一个使用配置服务器公开的中央配置的客户端。再次进入

eclipse并创建一个新的Maven项目,并将其命名为SpringCloudConfigClientA。

8- 8-添加客户端依赖项

转到topom.xml并添加以下依赖项:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>复制代码

在这里,我们将Spring Cloud配置库导入到我们的项目中,并将其定义为Web应用程序。

“ spring-boot-starter-actuator”依赖项提供了实用程序方法,这些方法可检查配置服务器的运行状况并在更改后刷新配置。

9- bootstrap.properties

在src / main / resources下创建bootstrap.properties。

spring.application.name=client-Aspring.cloud.config.uri=http://localhost:8888复制代码

该文件在客户端应用程序启动时运行,它为客户端应用程序定义了唯一名称,除了配置服务器的路径。

在“ spring.application.name”下定义的名称应类似于存储在存储库下的配置文件的名称。

附言:配置服务器的默认

URL是localhost:8888,因此,如果我们从此处删除它,我们仍然可以连接到配置服务。

10- application.properties

在src / main / resources下创建application.properties。

server.port:9095
复制代码

在此文件中,我们定义了每次更改后都需要重新启动的配置,这里我们只为应用程序定义了一个自定义端口。

11-创建REST控制器

在com.programmer.gate下创建Application.java:

package com.programmer.gate;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RefreshScope@RestControllerpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Value("${hello.message}")private String message;@RequestMapping("/message")String getMessage() {return this.message;}}复制代码

我们将我们的类定义为一个控制器,该控制器公开一个简单的方法/ message,该方法打印出hello.message属性的值,以确保我们的客户端已成功从配置服务读取。

12- 刷新配置

我们返回到存储库并在client-A.properties下将“ hello.message”属性的值修改为:

hello.message=Hello from Client A updated
复制代码

为了将此更改反映给Client A应用程序,你应该运行以下POST URL:

http:// localhost:9095 / actuator / refresh

现在,当你运行

http:// localhost:9095 / message时,将得到以下结果:

Hello from Client A updated
复制代码

Spring Cloud Configuration Client上的完整代码

文章写道这里,如有不足之处,欢迎补充评论。

抽丝剥茧,细说架构那些事 --优锐课

原文  https://juejin.im/post/5dfc7123f265da339f7cfc57
正文到此结束
Loading...