微服务-springcloud-config

一、config-server

1、配置文件:application.properties
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/zzzqqq123/springcloudtest.git
spring.cloud.config.server.git.username=zhangq@webyun.cn
spring.cloud.config.server.git.password=zhangqiang@123
2、在启动类中添加注解
@EnableConfigServer

二、config-client

1、application.properties
spring.application.name=company-info-config-client
server.port=8889
2、bootstrap.properties
spring.cloud.config.uri=http://localhost:8888/
#git中的文件名称为application-dev.properties
spring.cloud.config.name=application
spring.cloud.config.profile=dev
spring.cloud.config.label=master

注意git仓库中的文件名称为:application-dev.properties

三、刷新

刷新的是客户端:http://localhost:8889/actuator/refresh(post请求)
前提是需要开启刷新端点

management.endpoints.web.exposure.include=*
#health,info,env
management.endpoint.health.show-details=always

4、实现自动刷新的功能[简单实现]

@SpringBootApplication
@EnableScheduling
public class CompanyInfoConfigClientApplication {

    //定时更新数据
    private final ContextRefresher contextRefresher;
    CompanyInfoConfigClientApplication(ContextRefresher contextRefresher){
        this.contextRefresher=contextRefresher;
    }
    @Scheduled(fixedRate = 10*1000,initialDelay = 10*1000)
    public void refreshAuto(){
        Set<String> refresh = contextRefresher.refresh();
        Iterator<String> iterableSet=refresh.iterator();
        System.out.println("------------------》");
        while(iterableSet.hasNext()){
            System.out.println(iterableSet.next());
        }

    }

    public static void main(String[] args) {
        SpringApplication.run(CompanyInfoConfigClientApplication.class, args);
    }

}
5获取配置文件中的数据
package com.company.info.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope//刷新
@RequestMapping("user")
public class UserController {
    @Value("${user.name}")
    private String name;
    @GetMapping("name")
    public String testValue(){
        return name;
    }
}

6注意:

由于config一般采用集群配置,所以我们可以采用,单个依次重启服务的方式,实现配置文件生效

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值