springcloud学习之路(五):远程配置服务

在某些场景中,某个页面中仅有一些字段会经常变化,如果每次都找到网页对应的服务,把它关掉,改变字段,在重新部署,势必挺影响服务体验的,对运维人员也不太友善。能不能把这个字段作为一个变量,每次我改变这个变量的值,网页显示的内容就会随之自动变化?当然是可以的,这边是远程配置服务的作用。

对于远程配置服务搭建,同样三步走方法。
1、pom.xml文件配置

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version></version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

前两个依赖包之前已经介绍过多次了,在这不再赘述,第三个依赖包为定义服务的依赖包。

2、application.yml文件配置
在resources文件夹下新建application.yml文件,并填充内容如下:

spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: https://github.com/itOcean/spring-cloud/

server:
    port: 8009 #定义服务的端口

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/

第一部分声明该服务的注册名和远程配置仓库的地址,即某个配置文件存放的位置,可以打开https://github.com/itOcean/spring-cloud/,可以看到一个config.yml配置文件,里面的内容为itOcean:to be No.1;第二部分定义服务的端口;第三部分的作用是将该服务注册到注册中心。

3、后端代码
在java文件夹下,新建包com.springcloud.config,在该包下新建一个类ConfigServiceBootstrap作为启动类,填充内容成如下:

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

至此,远程配置服务的定义就完成了,显然远程配置服务是服务于消费服务的,为了让消费服务能够使用远程配置服务,则需要改造消费服务。

首先在消费服务user-center的resources文件夹下,新建另外一个配置文件bootstrap.yml,填充内容如下:

spring:
  cloud:
    config:
#      uri: http://localhost:8002/        #当无法通过服务名调取服务,可以通过地址来
      discovery:
        service-id: config-service
        enabled: true
      label: master  #git分支
      name: config
#      profile: dev     文件名如果是config-dev,则需要加profile标签,与name同一级

第二个需要改造的地方便是消费服务的rest包下的资源提供接口,将其改造成如下:

@RestController
public class ClientRest {
    @Value("${itOcean:James}")
    private String language;

    @RequestMapping("/test")
    public String test() throws InterruptedException {
        return "test2 "+language;
    }
}

@Value(“${itOcean:James}”)中的itOcean即为配置文件中的那个itOcean,冒号后面内容表示,当无法从远端加载itOcean字段中的内容时,就把James赋给itOcean。

启动远程配置服务,重启两个消费服务,在浏览器中输入http://localhost:8001/api/b/test,返回的内容为test1 to be No.1与test2 to be No.1的轮流切换。如果是test1 James与test2 James的轮流切换则说明远程配置服务没有正常工作。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值