Spring Cloud Config Server —程序员门

73 篇文章 0 订阅
36 篇文章 0 订阅

通过优锐课核心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=8888
spring.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
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

6-启动配置服务器

以可执行jar形式启动服务器,并通过运行以下url来确保其充当配置服务器并公开客户端的配置文件:
http:// localhost:8888 / <客户端名称> / default
该URL应该以JSON格式返回在请求的配置文件下定义的配置属性。
测试客户端的配置后,我们得到以下结果:
在这里插入图片描述
在这里插入图片描述
Spring Cloud Configuration Server上的完整代码

7-创建Spring Cloud Client

现在,我们将创建一个使用配置服务器公开的中央配置的客户端。
再次进入eclipse并创建一个新的Maven项目,并将其命名为SpringCloudConfigClientA。

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-A
spring.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
@RestController
public 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上的完整代码

喜欢这篇文章的可以点个赞,欢迎大家留言评论,记得关注我,每天持续更新技术干货、职场趣事、海量面试资料等等
如果你对java技术很感兴趣也可以加入我的java学习群 V–(ddmsiqi)来交流学习,里面都是同行,验证【CSDN2】有资源共享。
不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值