栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud Config配置中心

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud Config配置中心

首先,先进行远程文件内容的获取。因为git容易连接超时,所以此处选用gitee,原理都是一样的。

步骤:

        

     1、在gitee上新建一个仓库,里面添加一个application.yml文件。

内容如下(随便写的,为了验证确实连接上了远程仓库,并且可以从远程仓库获取到文件内容):

spring:  
 profiles:
    active:
      - dev

---
spring:
  profiles: dev  #开发环境
  application:
    name: SpringCloudTestSGG-config-dev

---
spring:
  profiles: test  #测试环境
  application:
    name: SpringCloudTestSGG-config-test

        2、复制远程仓库的https或者ssh链接。ssh配置比较麻烦,所以我此处选用https链接。

        3、新建一个服务,命名为SpringCloudTestSGG-config-8099。

       4、 引入依赖:

        
        
            org.springframework.cloud
            spring-cloud-config-server
            1.4.7.RELEASE
        
        
        
            org.apache.httpcomponents
            httpclient
            4.5.6
        

要引入httpClient,否则会报错。

        5、application.yml配置

server:
  port: 3344

spring:
  application:
    name: SpringCloudTestSGG-config
  cloud:
    config:
      server:
        git:
          # 将gitee的仓库链接粘贴到这,注意要配置用户名和密码
          uri: https://gitee.com/hanweijie1999/cloud-test-sgg.git
          password: hanweijie1999
          username: hanweijie1014@163.com

        6、启动类代码:

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


    到此完成文件内容的获取。

验证:

 如果输入个不在其中的后缀:

配置规则:

 第二种访问方式已经在前面测试过,不再展现。

下面是第一种访问方式:

 

第三种访问方式:

 

bootstrap.yml

 

新建yml文件,名为: springcloudtest-config.yml。push到gitee远程仓库。

内容:

spring:
  profiles: 
    active:
      - dev

---
server:
  port: 8201

spring:
  profiles: dev
  application:
    name: springcloudtestSGG-config-client

eureka:
  client:
    server-url:
      defaultZone: http://127.0.0.1:8082/eureka/


---
server:
  port: 8202

spring:
  profiles: test
  application:
    name: springcloudtestSGG-config-client

eureka:
  client:
    server-url:
      defaultZone: http://127.0.0.1:8084/eureka/

新建一个名为SpringCloudTestSGG-config-client-3355的服务。

引入依赖:

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

创建bootstrap.yml文件:代码:

spring:
  cloud:
    config:
      #本服务启动后,会先通过此服务 http://127.0.0.1:3344 去 master分支 加载名为 springcloudtest-config 的文件
      name: springcloudtest-config  #文件名
      label: master   #所在分支
      uri: http://127.0.0.1:3344  #地址
      profile: test  #开发环境

启动类:

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

为了方便观察可以再建一个controller:

@RestController
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${eureka.client.server-url.defaultZone}")
    private String eurekaServers;

    @Value("${server.port}")
    private String port;


    @GetMapping(value = "/config")
    public String getConfig(){
        Logger cloudConfig = Logger.getLogger("CloudConfig");
        cloudConfig.info("applicationName>>: "+applicationName+"t"+"EurekaServer>>: "+eurekaServers+"t"+"port>>: "+port);
        String string = "applicationName>>: "+applicationName+"t"+"EurekaServer>>: "+eurekaServers+"t"+"port>>: "+port;
        return string;
    }
}

到此即可实现从远程仓库统一配置springcloud。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572035.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号