Spring Cloud (1)——config server使用SVN作为远程例子的运行与配置

本文介绍了如何使用Spring Cloud Config Server与SVN集成。首先,通过下载官方的Eclipse STS和Config Server样本项目进行配置。然后,修改配置文件启用SVN,并在启动后验证配置。客户端应用程序通过指定名字和Config Server的URI来获取配置。成功运行后,客户端和服务器端显示相同的消息,表明配置已成功同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

官方链接与文档 写道

 

https://spring.io/guides/gs/centralized-configuration/

 

 

Spring cloud config Server基于Spring boot构建,

 

  1. 建议下载使用官方的Eclipse ( sts-3.7.2.RELEASE)
  2. 下载config server sample 的源码,并导入到eclipse工程中,gs-centralized-configuration-complete\configuration-service
  3. 修改配置文件
  4. 启动程序

配置文件 maven文件:

<?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>

	<groupId>com.example</groupId>
	<artifactId>configuration-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.2.8.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.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>
		<dependency>
			<groupId>org.tmatesoft.svnkit</groupId>
		<artifactId>svnkit</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-parent</artifactId>
				<version>Angel.SR4</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>

 application.properties:

server.port=8888
spring.application.name=configServer
#spring.cloud.config.server.git.uri=${HOME}/Desktop/config
spring.profiles.active=subversion
spring.cloud.config.server.svn.uri=svn://IP:port/project/config
spring.cloud.config.server.svn.username=lvdccyb
spring.cloud.config.enabled=true
spring.cloud.config.server.svn.password=lvdccyb@126.com
spring.main.show-banner=false

 

其中这3项表面使用svn,并需要注意,远程url可用。

 

 

spring.profiles.active=subversion
spring.cloud.config.server.svn.uri=svn://IP:port/project/config
spring.cloud.config.server.svn.username=lvdccyb

 

 

 

运行结果(部分内容去掉):

 

 

http://localhost:8888/env

 

{
    "profiles": [
        "subversion"
    ],
    "bootstrap": {},
    "servletContextInitParams": {},

    "configServerClient": {
        "spring.cloud.config.enabled": "false"
    },
    "applicationConfig: [classpath:/application.properties]": {
        "spring.cloud.config.server.svn.password": "******",
        "spring.cloud.config.server.svn.uri": "svn://X.Y.Z.U/project/config",
        "server.port": "8888",
        "spring.profiles.active": "subversion",
        "spring.main.show-banner": "false",
        "spring.cloud.config.enabled": "true",
        "spring.cloud.config.server.svn.username": "lvdccyb",
        "spring.application.name": "configServer"
    },
    "defaultProperties": {
        "spring.application.name": "bootstrap"
    }
}

http://localhost:8888/configuration-client/default

注意上面地址:对应的svn地址一般为:
svn://IP:PORT/{$配置的uri}/trunk/configuration-client.properties
而且这个地址,需要仔细阅读spring 官方文档对application,profile,label的解释,以及路径的寻找优先级,上述地址缺省配置时,可以写成多种多样:
http://localhost:8888/configuration-client/default/trunk
http://localhost:8888/configuration-client/trunk/trunk
http://localhost:8888/configuration-client/trunk
甚至如下:
http://localhost:8888/configuration-client/abc
这里需要多一级trunk目录,而不是直接使用配置文件。

{
    "name": "configuration-client",
    "profiles": [
        "default"
    ],
    "label": "trunk",
    "propertySources": [
        {
            "name": "svn://IP:PORT/{$配置的uri}/trunk/configuration-client.properties",
            "source": {
                "IamApplicationName": "true",
                "message": "Hello World,I am spring client"
            }
        }
    ]
}

—————————————————————————————————————————————

Config 客户端:

application.properties文件
#这个需要与配置文件对应起来
spring.application.name=configuration-client
spring.cloud.config.uri=http://localhost:8888
启动程序后,访问:

http://localhost:8080/message

可以看到:

Hello World,I am spring client

 

这个与前面SERVER端显示的内容一样,说明配置已经生效了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值