SpringCloud系列十四:Config-基本使用

一、配置文件托管到GitLab
1、新建文件夹springcloud-config
2、在文件夹下新建配置文件application.yml

spring:
  profiles:
    active: 
    - dev
---
spring:
  profiles: dev   #开发环境
  application:
    name: springcloud-config-atguigu-dev
---
spring:
  profiles: test   #测试环境
  application:
    name: springcloud-config-atguigu-test
#必须保存为UTF-8格式

3、在文件夹下新建配置文件springcloud-config-client.yml

spring:
  profiles:
    active: 
    - dev
---
server:
  port: 8201
spring:
  profiles: dev  
  application:
    name: springcloud-config-client
eureka:
  client:
    service-url: 
      defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
  port: 8202
spring:
  profiles: test  
  application:
    name: springcloud-config-client
eureka:
  client:
    service-url: 
      defaultZone: http://eureka-test.com:7001/eureka/
#必须保存为UTF-8格式

4、推送到GitLab上
在这里插入图片描述
二、配置中心服务端
1、新建配置中心服务端microservicecloud-config-3344工程
2、引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <!-- 热部署插件 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <!-- eureka服务注册中心-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!-- hystrix服务熔断 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <!-- hystrix-dashboard相关: actuator监控 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- springCloud config配置中心 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>

3、yml配置

server:
  port: 3344

spring:
  application:
    name: springcloud-config
  cloud:
    config:
      server:
        git:
          uri: http://192.168.106.130:8090/root/springcloud-config.git    #GitLab上面的git仓库地址
          username: root
          password: lizhiqiang1217

4、在主启动类上添加@EnableConfigServer注解

/**
 *访问配置信息的URL与配置文件(本地或者git里的配置文件,不是项目里的配置文件的内容)的映射关系如下:
 *   application就是文件名的前半部分或yml中配置的应用名,profile就是开发版本 label就是git分支
 *   /{application}/{profile}[/{label}]
 *   /{application}-{profile}.yml
 *   /{label}/{application}-{profile}.yml
 *   /{application}-{profile}.properties
 *   /{label}/{application}-{profile}.properties
 * 上面的url会映射{application}-{profile}.properties对应的配置文件,其中{label}对应Git上不同的分支,默认为master。
 * 我们可以尝试构造不同的url来访问不同的配置内容,比如,要访问master分支,config-client应用的dev环境,就可以访问这个url:
 *   http://config3344.com:3344/springcloud-config/dev/master
 *   http://config3344.com:3344/application-dev.yml
 *   http://config3344.com:3344/master/application-dev.yml
 *   http://config3344.com:3344/application-dev.properties
 *   http://config3344.com:3344/develop/application-dev.properties
 */
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfig3344_App {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfig3344_App.class, args);
    }
}

5、找到C:\Windows\System32\drivers\etc路径下的hosts文件。添加域名映射

127.0.0.1  config3344.com

6、启动服务。
http://config3344.com:3344/application-dev.yml
在这里插入图片描述
http://config3344.com:3344/application-test.yml
在这里插入图片描述
三、配置中心客户端
1、新建配置中心客户端microservicecloud-config-client-3355工程
2、引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <!-- 热部署插件 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <!-- eureka服务注册中心-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <!-- config服务配置中心 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!-- hystrix服务熔断 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <!-- hystrix-dashboard相关: actuator监控 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- springCloud config配置中心 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config</artifactId>
    </dependency>
</dependencies>

3、application.yml配置

spring:
  application:
    name: springcloud-config-client

4、bootstrap.yml配置。主要负责从从外部源加载配置属性并解析配置。

#application.yml是用户级的资源配置项
#bootstrap.yml是系统级的资源配置项,优先级更高
spring:
  cloud:
    config:
      name: springcloud-config-client    #需要从GitLab上读取的资源名称,注意没有yml后缀名
      profile: dev    #本次访问的配置项
      label: master
      uri: http://config3344.com:3344    #该微服务启动后先去找服务配置中心3344,通过SpringCloudConfig获取GitLab地址

5、找到C:\Windows\System32\drivers\etc路径下的hosts文件。添加域名映射

127.0.0.1  client-config.com

6、创建类ConfigClientRest用来验证能否从GitLab上获取到配置信息

@RestController
public class ConfigClientRest {

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

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

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

    @RequestMapping("/config")
    public String getConfig() {
        String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
        System.out.println("从服务配置中心3344中获取的信息: " + str);
        return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
    }
}

7、启动配置中心服务端,再启动配置中心客户端。
http://client-config.com:8201/config
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值