SpringCloud6-Config分布式配置中心

谁借莪1个温暖的怀抱¢ 2022-04-03 05:45 405阅读 0赞

springcloud版本: 【Finchley 版】







结构图

分布式配置中心
在这里插入图片描述
集中管理配置文件-提供了集中化的外部配置支持,配置服务器为各个微服务的环境提供了一个中心化的外部配置
动态配置
springcloud Config分为客户端和服务端(分布式配置中心,是一个独立的微服务应用)
推荐使用git管理版本信息







管理版本信息
  1. 新建github仓库
    在这里插入图片描述
    仓库地址: git@github.com:ai1045152332/springcloud-config.git

Mac下git 相关问题:
https://www.cnblogs.com/leisurezxy/p/8203435.html
https://baijiahao.baidu.com/s?id=1610561731776096216&wfr=spider&for=pc

  1. 克隆到本地
    git clone ssh地址
  2. 上传
    watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_16_color_FFFFFF_t_70 2
    内容:

    spring:
    profiles:

    1. active:
    2. - dev

    spring:
    profiles: dev
    application:

    1. name: eureka-client-user-dev

    spring:
    profiles: test
    application:

    1. name: eureka-client-user-test

    保存为UTF-8

上传的命令

  1. git status
  2. git add .
  3. git commit -m ""
  4. git push origin master

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_16_color_FFFFFF_t_70 3








服务端配置
  1. <dependencies>
  2. <dependency>
  3. <groupId>com.zjy.cloud</groupId>
  4. <artifactId>springcloud-common</artifactId>
  5. <version>1.0.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-web</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.cloud</groupId>
  17. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-config-server</artifactId>
  22. </dependency>
  23. </dependencies>

yml

  1. server:
  2. port: 3344
  3. spring:
  4. application:
  5. name: eureka-client-config-server
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://github.com/ai1045152332/springcloud-config.git
  11. username: ai1045152332
  12. password: ********
  13. # 缓存位置
  14. basedir: /Users/zhaojianyu/IdeaProjects/springcloud-parent/eureka-client-config/src/main/resources/file

启动类

  1. package com.zjy.config;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. @EnableConfigServer
  6. @SpringBootApplication
  7. public class ConfigServerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(ConfigServerApplication.class, args);
  10. }
  11. }

访问方式
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_16_color_FFFFFF_t_70 4

  1. http://localhost:3344/application-dev.yml
    http://localhost:3344/application-test.yml
    http://localhost:3344/application-xxxxx.yml在这里插入图片描述
  2. 第二种访问方式 http://localhost:3344/application/dev/master在这里插入图片描述
  3. 第三种 http://localhost:3344/master/application-dev.yml







客户端配置
  1. 客户端配置上传到github
    https://github.com/ai1045152332/springcloud-config/blob/master/cloud-config-client.yml
  2. 创建config-client项目

2.1 pom文件

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-client</artifactId>
  4. </dependency>

2.2 bootstrap.yml系统级资源配置项,优先级高

  1. spring:
  2. cloud:
  3. config:
  4. name: cloud-config-client # github上配置文件名
  5. profile: dev # 本次访问的配置项
  6. label: master
  7. uri: http://localhost:3344 # 连接server

application.yml (是用户级)

  1. spring:
  2. application:
  3. name: cloud-config-client
  1. 接口

    /* 描述: 配置客户端rest访问配置 @author zhaojianyu @create 2018-12-21 8:49 PM */
    @RestController
    public class ConfigClientRest {

    1. @Value("${spring.application.name}")
    2. private String applicationName;
    3. @Value("${eureka.client.service-url.defaultZone}")
    4. private String eurekaServer;
    5. @Value("${server.port}")
    6. private String port;
    7. @RequestMapping("/config")
    8. public String getConfig(){
    9. //通过3344 server获取配置信息
    10. return "application:"+applicationName+"<br/>"+
    11. "eurekaServer:"+eurekaServer+"<br/>"+
    12. "port:"+port+"<br/>";
    13. }

    }

启动类无特殊改动

  1. 两种访问
    在这里插入图片描述
    在这里插入图片描述
    并且已经注册到eureka server中
    在这里插入图片描述

给server配置密码

https://blog.csdn.net/chengqiuming/article/details/80888355

发表评论

表情:
评论列表 (有 0 条评论,405人围观)

还没有评论,来说两句吧...

相关阅读