Spring Cloud Config集成SVN实践

相信一些公司是用SVN做版本管理的,那如何使用Spring Cloud Config集成svn来做配置中心呢?

1、首先搭建一个config-server:

pom 依赖:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.tmatesoft.svnkit</groupId>
			<artifactId>svnkit</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>

</dependencies>

bootstrap.properties:

server.port=8304
spring.application.name=test-config
eureka.client.serviceUrl.defaultZone=http://192.168.12.55:8301/eureka/

#eureka.instance.ip-address=192.168.12.21

eureka.instance.prefer-ip-address=true

#svn 配置
spring.cloud.config.server.svn.uri=https://192.168.12.50/svn/test/trunk/demo
spring.cloud.config.server.svn.username=admin
spring.cloud.config.server.svn.password=123456
spring.cloud.config.server.svn.default-label=trunk

#specific svn
spring.profiles.active=subversion
#spring.profiles.application=test-config-server

management.security.enabled=false
#spring cloud bus
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=192.168.12.20
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=123456

启动类:

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServiceApplication {

	public static void main(String[] args) {

		SpringApplication.run(ConfigServiceApplication.class, args);

	}
}

注意在 svn:https://192.168.12.50/svn/test/trunk/demo 这个目录下新建一个config-dev.properties文件:

test.name=zhangsan

 

再新建一个 config-client项目:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
        <!- 必须要的哈,用于refresh-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>

</dependencies>

application.properties:

server.port=8305
spring.application.name=test-config-client
management.security.enabled=false

# 指定此实例的ip
#eureka.instance.ip-address=192.168.12.50
# 注册时使用ip而不是主机名
#eureka.instance.prefer-ip-address=true


#spring cloud bus
spring.rabbitmq.host=192.168.12.20
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=123455

bootstrap.properties:

#对于svn的文件名
spring.cloud.config.name=config
#config-dev.properties 对于dev
spring.cloud.config.profile=dev
spring.cloud.config.label=trunk
spring.cloud.config.discovery.enabled=true
#指定config server serviceId
spring.cloud.config.discovery.serviceId=test-config
spring.cloud.bus.trace.enabled=true

#注册中心
eureka.client.serviceUrl.defaultZone=http://192.168.12.55:8301/eureka/

编写服务类:

/**
 * 使用@RefreshScope该注解,会在接到SpringCloud配置中心配置刷新的时候自动将新的配置更新到该类对应的字段中
 */
@RestController
@RefreshScope
public class HelloController {

    @Value("${test.name}")
    private String testName;

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
	   return "hello " + name + ", Config test.name:" + testName;
    }

}


@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

}


启动项目:

访问:http://localhost:8305/hello?name=tim 就会正常显示:

hello tim, Config test.name: zhangsan

当修改svn文件提交后,以post请求访问:http://localhost:8305/refresh  就会获取到最新配置信息了。

 

 

转载于:https://my.oschina.net/woter/blog/1843687

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值