Spring Cloud Configuration Server是一个集中式应用程序,可管理所有与应用程序相关的配置属性.在本章中,您将详细了解如何创建Spring Cloud Configuration服务器.
创建Spring Cloud配置服务器
首先,下载Spring从Spring Initializer页面启动项目并选择Spring Cloud Config Server依赖项.观察下面给出的截图 :
现在,在构建配置文件中添加Spring Cloud Config服务器依赖项,如下所述 :
Maven用户可以将以下依赖项添加到pom.xml文件中.
org.springframework.cloud spring-cloud-config-server
Gradle用户可以在build.gradle文件中添加以下依赖项.
compile('org.springframework.cloud:spring-cloud-config-server')
现在,在主Spring中添加@EnableConfigServer注释启动应用程序类文件. @EnableConfigServer注释使您的Spring Boot应用程序充当配置服务器.
主要的Spring Boot应用程序类文件在下面和下面给出;
package com.it1352.configserver; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class ConfigserverApplication { public static void main(String[] args) { SpringApplication.run(ConfigserverApplication.class, args); }}
现在,将以下配置添加到属性文件中,并将application.properties文件替换为bootstrap.properties文件.观察下面给出的代码 :
server.port = 8888spring.cloud.config.server.native.searchLocations=file:///C:/configprop/SPRING_PROFILES_ACTIVE=native
Configuration Server在Tomcat端口8888上运行,应用程序配置属性从本机加载搜索位置.
现在,在 file:///C:/configprop/中,放置客户端应用程序 - application.properties文件.例如,您的客户端应用程序名称是 config-client ,然后将您的application.properties文件重命名为 config-client.properties ,并将属性文件放在路径上file:///C:/configprop/.
config-client属性文件的代码在下面给出 :
welcome.message = Welcome to Spring cloud config server
完整的构建配置文件在下面和下面给出;
Maven用户可以使用下面给出的 pom.xml :
4.0.0 com.IT屋 configserver 0.0.1-SNAPSHOT jar configserver Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 Edgware.RELEASE org.springframework.cloud spring-cloud-config-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
Gradle用户可以使用下面给出的build.gradle文件 :
import buildscript { ext { springBootVersion = '1.5.9.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'org.springframework.boot'group = 'com.it1352'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8repositories { mavenCentral()}ext { springCloudVersion = 'Edgware.RELEASE'}dependencies { compile('org.springframework.cloud:spring-cloud-config-server') testCompile('org.springframework.boot:spring-boot-starter-test')}dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" }}
现在,创建一个可执行的JAR文件,并使用以下Maven或Gradle命令运行Spring Boot应用程序 :
对于Maven,使用下面给出的命令 :
mvn clean install
在"BUILD SUCCESS"之后,您可以在目标目录下找到JAR文件.
对于Gradle,使用下面给出的命令 :
gradle clean build
在"BUILD SUCCESSFUL"之后,您可以在build/libs目录下找到JAR文件.
使用运行JAR文件以下命令 :
java -jar
现在,应用程序已在Tomcat端口8888上启动,如下所示 :
现在点击URL http://localhost:8888/config-client/default/master 在您的Web浏览器上,您可以看到您的config-client应用程序配置属性,如下所示.