微服务实战 集中配置组件Config规避敏感信息泄露_config 敏感信息

新建config-server自身的配置文件application.yml

server:
  port: 8005
spring:
  application:
    name: config-server
  profiles:
    active: native #使用本地文件
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/repo #本地配置仓库地址
# git:
# uri: https://gitee.com/xxxx/xxxxx.git
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

这里我们以使用本地配置仓库地址为例,spring.profiles.active设置为native,配置仓库路径为repo文件夹,所以我们在resources文件下创建repo文件夹,并创建新的一个configclient-dev.yml的文件,内容如下:

server:
  port: 8007

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
feign:
  hystrix:
    enabled: true
logging:
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'

3、新建启动类

/\*\*
 \* @Author:公众号:程序员965
 \* @create 2022-07-05
 \*\*/
@EnableConfigServer //开启配置服务
@EnableEurekaClient
@SpringBootApplication
public class ConfitServerApplication {

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

}

注意增加@EnableConfigServer注解,表示这是个配置中心服务端。

4、创建配置中心客户端

服务端开发完成后,我们再新建一个客户端config-client项目,引入如下依赖:

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

与服务端不同的是,客户端的配置文件我们创建bootstrap.yml文件

spring:
  cloud:
    config:
      name: configclient
      profile: dev
      label: master
      discovery:
        enabled: true
        service‐id: config-server

eureka:
  client:
    service‐url:
      defaultZone: http://localhost:8001/eureka/

注意spring.cloud.config.name与服务端中的文件名称对应,spring.cloud.config.profile与文件名-后面的环境代码对应,配置文件的命名规则是 {application}/{profile}[/{label}]

当 Config Client 去访问 Config Server 时,spring.cloud.config.namespring.cloud.config.profile 以及 、spring.cloud.config.label 的值分别对应上面三个占位符,如果配置了spring.cloud.config.name,那么就取spring.cloud.config.name,如果没有配置就取 spring.application.name,通过灵活使用 {application}{profile}{label} 三个占位符,就可以来动态地控制 client 从 server 所访问的仓库!

然后编写客户端启动类:

/\*\*
 \* @Author:公众号:程序员965
 \* @create 2022-07-05
 \*\*/
@EnableDiscoveryClient


![img](https://img-blog.csdnimg.cn/img_convert/edab2cc182c7da8378e9b274728b5f3f.png)
![img](https://img-blog.csdnimg.cn/img_convert/26021d5e698551a136a5b800e3f0ea50.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值