关于springboot:SpringCloud入门6Config

0次阅读

共计 2571 个字符,预计需要花费 7 分钟才能阅读完成。

前言

曾经 4 天没有更新了,怎么说呢,就很忙,很忙,很忙。次要还是毕业以及从一个城市到另外一个城市的事件,这段时间尽最大可能放弃 2,3 天一更,最晚不超过 4,5 天一更。

随着模块的增多,会呈现配置文件繁冗的通病,每次都要关上好多层目录能力找到配置文件,SpringCloud 中的 Config 组件就是为了解决这个问题,通过简略的配置就能实现配置文件的对立治理。

Config 服务端

引入 Config 服务端

创立 Config 空父模块,在上面建设一个 config-server 子模块,批改 子模块的 pom 文件

留神是子模块的 pom 文件,不像以前一样批改的是空父模块的 pom 文件

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>

配置文件

因为临时没必要注册进 Eureka 中,所以配置文件的编写还是比较简单的

server:
  port: 8101

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cutey_none/springcloud-study-config
          username:
          password:
      label: master

spring.cloud.config.server.git.uri:寄存文件的地址,到时候客户端就从这里获取配置文件,能够本地,也能够是 git

如果是公开仓库,那么usernamepassword 不必写

因为创立的springcloud-study-config 仓库设置的权限凋谢的,所以间接用我的也行,本人创立也行,就失常 github 或者 gitee 创立一个仓库就好。

仓库搁置的是各个微服务的配置文件

例子是治理 config-client 微服务(前面会创立)的配置文件,所以须要在仓库中创立一个config-client-dev.properties(-dev 示意是开发环境下的配置文件)

config-client-dev.properties 文件的内容如下,能够看作是 config-client 服务的某些配置

主启动类

减少@EnableConfigServer 注解以提供 config 服务反对

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

测试

SpringCloud Config 有本人 http 服务拜访资源的模式

  • /{application}/{profile}[/{label}] >> /config-client/dev
  • /{application}-{profile}.yml >> /config-client-dev.yml
  • /{label}/{application}-{profile}.yml >> /master/config-client-dev.yml
  • /{application}-{profile}.properties >> /config-client-dev.properties
  • /{label}/{application}-{profile}.properties >> /master/config-client-profile.properties

单个运行 ConfigServer8101 我的项目即可,用上述 5 种形式拜访资源,能够看到均能胜利从服务端拜访资源

config 客户端

引入 config 客户端

后面始终说的config-client 就是接下来要创立的模块,最终的我的项目构造目录如下

接着批改config-client9501 模块的 pom 文件,留神服务端和客户端引入的依赖是不一样的

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>

配置文件

配置文件留神命名是bootstrap.yml

因为客户端要设置服务端的 uri,所以应该 优先加载客户端配置文件

server:
  port: 9501
spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev
      label: master
      uri: http://localhost:8101

spring.cloud.config.uri:服务端的地址,去哪里取配置文件

主启动类和业务类

@SpringBootApplication
@RestController
public class ConfigClient9501 {public static void main(String[] args) {SpringApplication.run(ConfigClient9501.class, args);
    }

    @Value("${name}")
    String name;
    @GetMapping("/hi")
    public String hello() {return "hello," + name;}
}

在这里可能会遇到提醒找不到占位符 ${name},那必定是哪一方面没有依照步骤来了

测试

留神哈,下面客户端的配置文件中是没有 name 这个变量的

服务端的我的项目不必进行,再开启config-client9501 我的项目,开启的我的项目如下

上面拜访 localhost:9501/hi,失常的状况下能够看到如下

其实曾经抽蛮多的系统工夫来写了,然而有时候遇到了一些问题也还是须要尽可能弄懂再艰深地讲进去,心愿能帮到看这篇文章的小伙伴啦!!!

创作不易,如果对你有帮忙,欢送点赞,珍藏和分享啦!

上面是集体公众号,有趣味的能够关注一下,说不定就是你的宝藏公众号哦,根本 2,3 天 1 更技术文章!!!

正文完
 0