springcloud config 实现配置中心

springcloud config 实现配置中心,第1张

springcloud config 实现配置中心

文章目录
  • 实现简单的配置中心
    • 实现配置中心服务端
      • 一、新建springboot项目,引入依赖
      • 二、相关配置
      • 三、测试
    • 实现配置中心客户端
      • 一、引入maven依赖
      • 二、初始化配置文件
      • 三、启动类
      • 四、相关的配置类
      • 五、controller
      • 六、测试
  • 实现自动刷新
    • 在github中配置webhook

实现简单的配置中心

在github中创建springcloud-config仓库
新建config-dev.yml,config-prod.yml,config-test.ym三个配置文件。
配置文件的内容大致如下:

config:
  info: master branch,springcloud-config/config-dev.yml version=7
实现配置中心服务端 一、新建springboot项目,引入依赖

pom.xml



    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-center-3344
    
        




        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        






        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

二、相关配置
  1. application.yml
server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/github用户名/springcloud-config.git
          ####搜索目录
          search-paths:
            - springcloud-config
          force-pull: true
          username: github用户名
          password: 密码
          default-label: master
          skip-ssl-validation: true
      ####读取分支
      label: master
#  rabbitmq:
#    host: ip
#    port: 5672
#    username: guest
#    password: guest
#    virtual-host: /

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

  1. 在启动类上增加相关注解@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344
{
    public static void main(String[] args) {
            SpringApplication.run(ConfigCenterMain3344.class, args);
    }
}
三、测试

启动服务3344
访问http://config-3344.com:3344/master/config-prod.yml
http://config-3344.com:3344/master/config-dev.yml
如果访问以上地址可以正常返回数据,则说明配置中心服务端正常。

实现配置中心客户端 一、引入maven依赖

    org.springframework.boot
    spring-boot-starter-web



    org.springframework.cloud
    spring-cloud-starter-config



    org.springframework.boot
    spring-boot-starter-actuator

二、初始化配置文件
  1. bootstrap.yml
spring:
  profiles:
    active: dev

---
spring:
  profiles: prod
  application:
    name: config-single-client
  cloud:
     config:
       uri: http://localhost:3301
       label: master
       profile: prod


---
spring:
  profiles: dev
  application:
    name: config-single-client
  cloud:
     config:
       uri: http://localhost:3301
       label: master
       profile: dev

配置了两个版本的配置,并通过spring.profiles.active设置当前使用的版本,例如本例使用的dev版本。

  1. application.yml
server:
  port: 3355
management:   # 自动刷新配置actuator
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"
data:          # 当无法读取配置中心的配置时,使用此配置,以免项目无法启动 
  env: NaN
  user:
    username: NaN
    password: NaN
三、启动类
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class, args);
    }
}
四、相关的配置类

要读取配置中心的内容,需要增加相关的配置类,springcloud config 读取配置中心的方式和读取本地配置文件中的配置是一样的。

  1. 可以使用@Value的方式
@Data
@Component
public class GitConfig {

    @Value("${data.env}")
    private String env;

    @Value("${data.user.username}")
    private String username;

    @Value("${data.user.password}")
    private String password;

}
  1. 可以使用@ConfigurationProperties的方式
@Component
@Data
@ConfigurationProperties(prefix = "data")
public class GitAutoRefreshConfig {

    public static class UserInfo {
        private String username;

        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "UserInfo{" +
                    "username='" + username + ''' +
                    ", password='" + password + ''' +
                    '}';
        }
    }
    private String env;
    private UserInfo user;
}
五、controller
@RestController
public class GitController {

    @Autowired
    private GitConfig gitConfig;

    @Autowired
    private GitAutoRefreshConfig gitAutoRefreshConfig;

    @GetMapping(value = "show")
    public Object show(){
        return gitConfig;
    }

    @GetMapping(value = "autoShow")
    public Object autoShow(){
        return gitAutoRefreshConfig;
    }
}

可以用来测试项目。

六、测试

启动项目,访问restful接口
http://localhost:3355/show

结果:

{
  "env": "localhost-dev-edit",
  "username": "fengzheng-dev",
  "password": "password-dev"
}

访问http://localhost:3355/autoShow
结果:

{
  "env": "localhost-dev-edit",
  "user": {
      "username": "fengzheng-dev",
      "password": "password-dev"
  	}
}
实现自动刷新

前言:

springcloud config 在项目启动时加载配置内容这一机制,导致它存在一个缺陷——修改配置内容后,不会自动刷新。当服务已经启动的时候,去修改github上的配置文件内容,这时,再次刷新页面,却还是旧的配置内容,新内容不会主动刷新过来。
springcloud提供了一个刷新机制,但是需要我们主动触发。即@RefreshScope注解并结合actuator,注意要引入spring-boot-starter-actuator包。

  1. 在config client 客户端配置中增加一个actuator配置
management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"
  1. 在需要读取配置的类上增加@RefreshScope注解
@RestController
@RefreshScope
public class GitController {

    @Autowired
    private GitConfig gitConfig;

    @Autowired
    private GitAutoRefreshConfig gitAutoRefreshConfig;

    @GetMapping(value = "show")
    public Object show(){
        return gitConfig;
    }

    @GetMapping(value = "autoShow")
    public Object autoShow(){
        return gitAutoRefreshConfig;
    }
}
  1. 重启client客户端,修改github上的配置内容,并提交修改,再次刷新页面,没有反应。
    接着,发送post请求到http://localhost:3355/actuator/refresh(用来触发加载新配置)

返回内容如下:

[
	"config.client.version",
	"data.env"
]

之后,再次访问restful接口,http:localhost:3355/autoShow,这个接口获取的数据已经是最新的了,说明refresh机制起作用了。
而http:localhost:3355/show获取的还是旧数据,这与@Value注解的实现有关。建议在以后的项目中不要使用这种方式加载配置。

在github中配置webhook

https://www.cnblogs.com/fengzheng/p/11242128.html

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5691826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存