首页 > 其他 > 详细

08 使用config完成自动配置

时间:2020-03-27 18:31:11      阅读:78      评论:0      收藏:0      [点我收藏+]

SpringCloudConfig就是通常意义上的配置中心,把应用原本放在本地文件的配置抽取出来放在中心服务器,从而能够提供更好的管理、发布能力。

1、环境约束

  • win10 64为操作系统
  • idea2018.1.5
  • maven-3.0.5
  • jdk-8u162-windows-x64

2、前提约束

3、操作步骤

3.1 创建config server

  • 创建一个springcloud项目,包括springweb,config server模块,包括以下关键依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

注意:springboot的版本是2.1.8.RELEASE,springcloud的版本是Greenwich.SR2

  • 修改application.yml
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/langli/springcloudconfig.git  #项目路径
          search-paths: springcloudconfig #项目名称
          default-label:  master #代码分支,可以不写
          username: langli2049@163.com #gitee账号
          password: 12345678 #gitee密码
server:
  port: 8001
  • 在主启动类上除@SpringBootApplication外加入以下注解:
@EnableConfigServer
  • 测试
    (1)push一个文件到gitee的springcloudconfig下,改文件名称为application-dev.yml,内容如下:
data:
  env: config-eureka-dev
  user:
    password: 1986
    username: xiaoli

(2)启动config server项目,使用浏览器访问http://localhost:8001/application-dev.yml ,便可以看到如下结果:
技术分享图片

3.2 创建config client

  • 创建一个springcloud项目,包括springweb,config client,actuator模块,包括以下关键依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

注意:springboot的版本是2.1.8.RELEASE,springcloud的版本是Greenwich.SR2

  • 在resources文件夹下创建bootstrap.yml,该yml文件先于application.yml加载。
spring:
  profiles:
    active: dev
---
spring:
  profiles: prod
  application:
    name: application
  cloud:
     config:
       uri: http://localhost:8001
       label: master
       profile: prod
---
spring:
  profiles: dev
  application:
    name: application
  cloud:
     config:
       uri: http://localhost:8001
       label: master
       profile: dev
  • 修改application.yml,内容如下:
server:
  port: 8002
management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"

data:
  env: NaN
  user:
    username: NaN
    password: NaN
  • 在主启动类同级目录下,创建GitAutoRefreshConfig.java:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "data")
//采用这种方式能做到即刻拿到最新配置数据
public class GitAutoRefreshConfig {

    private String env;

    private UserInfo user;

    public GitAutoRefreshConfig(String env, UserInfo user) {
        this.env = env;
        this.user = user;
    }

    public GitAutoRefreshConfig() {
    }

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }

    public UserInfo getUser() {
        return user;
    }

    public void setUser(UserInfo user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "GitAutoRefreshConfig{" +
                "env=‘" + env + ‘\‘‘ +
                ", user=" + user +
                ‘}‘;
    }
}

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 + ‘\‘‘ +
                ‘}‘;
    }
}
  • 在主启动类同级目录下加入GitConfig.java:

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

@Component
//这种方式不能即刻拿到最新数据,只能重启
public class GitConfig {

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

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

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

    public GitConfig() {
    }

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }

    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 "GitConfig{" +
                "env=‘" + env + ‘\‘‘ +
                ", username=‘" + username + ‘\‘‘ +
                ", password=‘" + password + ‘\‘‘ +
                ‘}‘;
    }
}

  • 在主启动类同级目录下加入GitController.java:

import net.wanho.cloud.configclient.config.GitAutoRefreshConfig;
import net.wanho.cloud.configclient.config.GitConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@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;
    }
}

3.3 测试

(1)依次重启config server,config client
(2)浏览器中访问http://localhost:8002/show,看到如下结果:
技术分享图片
(3)浏览器中访问http://localhost:8002/autoShow,看到如下结果:
技术分享图片
(4)修改application-dev.xml中的username为zhangli,并推送到gitee,再次刷新以上两个url,没有效果
(5)以POST方式访问http://localhost:8002/actuator/refresh
(6)访问http://localhost:8002/autoShow能看到zhangli,访问http://localhost:8002/show只能看到之前的xiaoli
注意:post方式的刷新只对GitAutoRefreshConfig 起作用;不能每次push代码都是手动刷新,此时应该在gitee中配置webhook钩子。
以上就是springcloud config的使用。

08 使用config完成自动配置

原文:https://www.cnblogs.com/alichengxuyuan/p/12581320.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!