在优锐课的java核心笔记中,与讨论了关于更改微服务的属性可能会导致一个复杂的问题。 在本文中,我们将看到Spring Cloud Config Server和微服务如何相处。
1.在一个中央存储库中管理分布式系统的所有模块的配置。
2.动态刷新配置,无需在更改配置后重新启动每个服务。
Spring Cloud配置服务的分步指南,除了构建一个客户端,该客户端在启动时使用中央配置,然后在不重新启动的情况下刷新其配置。
1-创建配置服务项目
2- 添加依赖关系
<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>复制代码
<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-创建配置库
·在桌面上创建一个名为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
server.port=8888spring.cloud.config.server.git.uri=https://github.com/husseinterek/spring-cloud-repo.git复制代码
PS:如果我们没有定义自定义端口,Spring Cloud配置服务器会自动在localhost:8888上运行
5- Application.java
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-启动配置服务器
http:// localhost:8888 / <客户端名称> / default
该URL应该以JSON格式返回在请求的配置文件下定义的配置属性。
Spring Cloud Configuration Server上的完整代码
7-创建Spring Cloud Client
eclipse并创建一个新的Maven项目,并将其命名为SpringCloudConfigClientA。
8- 8-添加客户端依赖项
<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-boot-starter-actuator”依赖项提供了实用程序方法,这些方法可检查配置服务器的运行状况并在更改后刷新配置。
9- bootstrap.properties
spring.application.name=client-Aspring.cloud.config.uri=http://localhost:8888复制代码
URL是localhost:8888,因此,如果我们从此处删除它,我们仍然可以连接到配置服务。
10- application.properties
server.port:9095
复制代码
11-创建REST控制器
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;}}复制代码
12- 刷新配置
hello.message=Hello from Client A updated
复制代码
http:// localhost:9095 / actuator / refresh
http:// localhost:9095 / message时,将得到以下结果:
Hello from Client A updated
复制代码
Spring Cloud Configuration Client上的完整代码
文章写道这里,如有不足之处,欢迎补充评论。
抽丝剥茧,细说架构那些事 --优锐课