SpringCloud配置中心--svn(二)

上章我们讲完server端,现在我们来client端

client 端

主要展示如何在业务项目中去获取server端的配置信息

1、添加依赖

<dependencies>

<dependency>

	<groupId>org.springframework.cloud</groupId>

	<artifactId>spring-cloud-starter-config</artifactId>

</dependency>

<dependency>

	<groupId>org.springframework.boot</groupId>

	<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

	<groupId>org.springframework.boot</groupId>

	<artifactId>spring-boot-starter-test</artifactId>

	<scope>test</scope>

</dependency>

引入spring-boot-starter-web包方便web测试

2、配置文件

需要配置两个配置文件,application.propertiesbootstrap.properties

application.properties如下:

spring.application.name=spring-cloud-config-client  //项目名称

server.port=8002

bootstrap.properties如下:

spring.cloud.config.name=server-config   

spring.cloud.config.profile=dev

spring.cloud.config.uri=http://localhost:8001/    //server端地址

spring.cloud.config.label=trunk  

特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。

3、启动类

启动类只需要@SpringBootApplication注解就可以

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class ConfigClientApplication {

	public static void main(String[] args) {

		SpringApplication.run(ConfigClientApplication.class, args);

	}

}

4、web测试

使用@Value注解来获取server端参数的值

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

class HelloController {

    @Value("${hello}")

    private String hello;

    @RequestMapping("/hello")

    public String from() {

        return this.hello;

    }

}

启动项目后访问:http://localhost:8002/hello
在这里插入图片描述

说明已经正确的从server端获取到了参数。到此一个完整的服务端提供配置服务,客户端获取配置参数的例子就完成了。

我们在进行一些小实验,手动修改server-config-dev.properties中配置信息为:hello=springCloud!!! update 提交到svn
在这里插入图片描述
再次在浏览器访问http://localhost:8002/hello
返回:springCloud!!! 说明获取的信息还是旧的参数,这是为什么呢?因为springboot项目只有在启动的时候才会获取配置文件的值,修改svn信息后,client端并没有在次去获取,所以导致这个问题。如何去解决这个问题呢?留到下一章我们在介绍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值