Spring Cloud配置服务

这是使用Spring Cloud构建分布式应用程序的第一步。 在本文中将演示/指导您创建服务以管理其他服务的配置。

本文中讨论的内容包括:

  • 什么是配置服务器? 为什么在分布式应用程序中需要服务器?
  • 说明集中配置管理和版本化配置管理。
  • 设置存储库以存储配置信息。
  • 构建并运行Spring Cloud服务器。

1. 为什么需要配置服务器?

本小节中将讨论为什么需要一个服务来管理分布式应用程序中其他服务的配置。
下面是分布式应用程序的示意图 - “出租车管理应用程序”,包括三个子应用程序(乘客管理,司机管理和旅程管理),每个子应用程序都部署在服务器上。

每个服务(应用程序)都是由开发人员团队开发的项目。 在项目中,除了代码之外,它还包含配置,例如,连接到数据库的信息,有关数据源位置的信息等。如果您在项目代码中创建此信息的硬代码(在代码中写死数据库连接信息),这是一个很不好的做法。 因此,此信息通常放在单独的文件中,这些文件称为配置文件

完成应用功能开发后,项目将打包并部署到服务器上。 通常,配置文件将与代码一起打包并形成唯一(文件)产品。 因此,如果配置中有任何更改,则需要编译并重新打包项目并将其重新部署到服务器上。 这显然是分布式应用程序环境中的挑战。

使用配置服务器

解决上述问题的想法是需要服务(应用程序)来管理其他服务的配置。 它在服务器上独立运行。

上述做法带来以下好处:

在Config-Server上更改配置文件时,您肯定希望将此类更改通知给客户端。 Spring Cloud Bus提供了一种机制,用于通知客户“存在更改”并要求客户端更新新信息。

2. Config Server如何存储数据?

将所有配置文件放在配置服务器(Config Server)上时,您将询问配置服务器(Config Server)如何存储这些文件。

配置服务器(Config Server)有两种主要的存储配置文件的方法:

  • 将它们作为系统文件存储在服务器的硬盘驱动器上。
  • 使用GIT或SVN(Subversion)。

在本节中,将创建一个配置服务器(Config Server),它将配置文件存储在GitHub上。 创建了一个GitHub存储库:

3. 创建Spring Boot项目

在Eclipse上,创建一个Spring Boot项目:

输入:

  • Name: SpringCloudConfigServer
  • Group: com.zyiz
  • Artifact: SpringCloudConfigServer
  • Description: Spring Cloud Config Server
  • Package: com.zyiz.scconfigserver

接着选择创建的功能:

好的,项目已经创建完成:

文件:pom.xml -

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zyiz</groupId>
    <artifactId>SpringCloudConfigServer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringCloudConfigServer</name>
    <description>Spring Cloud Config Server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>
        <defaultGoal>compile</defaultGoal>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

在文件:SpringCloudConfigServerApplication.java 中添加注解:@EnableConfigServer ,完整代码如下所示 -

package com.zyiz.scconfigserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer // 此代码注解必须要加上
@SpringBootApplication
public class SpringCloudConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigServerApplication.class, args);
    }
}

4. 配置Config Server

此服务(应用程序)将在端口8888上部署并运行,并在GitHub上存储配置文件,因此需要在文件:application.properties 文件中进行一些配置。

文件:application.properties -

server.port=8888

spring.cloud.config.server.git.uri=https://github.com/zyizcom/spring-cloud-config-server.git

# For File System:
# spring.profiles.active=native
# spring.cloud.config.server.native.searchLocations=C:/Users/tran/Desktop/config

如果Git Server(或SVN Server)需要用户名/密码,则需要进行其他配置。

5. 配置Config Server

右键单击项目名称,选择:Run As -> Maven install ,然后打开命令行,进入到生成的Jar的target目录,执行以下命令 -

D:\worksp\springcloud\SpringCloudConfigServer\target> java -jar SpringCloudConfigServer-0.0.1-SNAPSHOT.jar

运行后,输出以下结果 -

测试Spring Cloud,打开浏览器访问URL:

也就是上面创建的Githup中的内容:


上一篇:Spring Cloud简介

下一篇:Spring Cloud配置客户端

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程