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

 

官方链接与文档 写道

 

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文件:


	4.0.0

	com.example
	configuration-service
	0.0.1-SNAPSHOT
	jar

	
		org.springframework.boot
		spring-boot-starter-parent
		1.2.8.RELEASE
		 
	

	
		UTF-8
		1.8
	

	
		
			org.springframework.cloud
			spring-cloud-config-server
		


		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.tmatesoft.svnkit
		svnkit
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-starter-parent
				Angel.SR4
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

 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
[email protected]
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端显示的内容一样,说明配置已经生效了。

你可能感兴趣的:(Spring)