Spring Cloud Config本地以及远端模式实践

Spring Cloud Config本地以及远端模式实践

前言

本篇主要整理了spring cloud config的使用,包含本地模式以及远端模式。

项目工程

包含一个eurekaServer注册中心,一个eurekaClient,一个Spring Cloud Config配置中心。
在这里插入图片描述

Spring Cloud Config配置

首先是pom文件,需要添加spring cloud config的依赖支持,同时也将spring cloud config作为eureka Client注册到eureka上面,也要添加eureka相关依赖。

具体pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>configserver</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.10.RELEASE</version>
    </parent>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

第二步,需要在启动类上面添加一个@EnableConfigServer注解

/**
 * spring cloud config启动类
 *
 * @author yuanzhihao
 * @since 2021/11/9
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {

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

第三步,添加相关配置项

这边分为两种模式,一种是本地模式,就是配置文件存放在本地目录,一种是远端模式,是配置文件存放在服务器上面。

  • 本地模式

    本地模式需要设置spring.profiles.active属性为native,同时设置spring cloud config指定存放配置文件的目录。

    spring:
      application:
        name: config-server
      profiles:
        active: native # 本地模式
      cloud:
        config:
          server:
            native:
              search-locations: classpath:/cloud-repo # 指定目录方式
    server:
      port: 8888
    

    我这边在项目的resources目录下新建了一个cloud-repo目录,用于存储读取的配置文件信息。


    config.properties配置文件:

    name=yuanzhihao
    

    启动项目,可以通过浏览器输入http://{ip}:{port}/{application}-{profile}.properties去访问到我们配置的文件信息。
    在这里插入图片描述
    application对应的就是配置文件的名称,profile对应的是配置文件的版本信息,我们这边开发的时候会根据profile后缀来区分不同的开发环境的配置信息,如果配置文件不区分版本的话,通过验证,profile随便填什么都是访问的自己。

  • 远端模式

    远端模式支持通过配置代码仓库的地址以及账号和密码来读取存放在代码服务器上面的配置文件。
    配置文件如下:

    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: http://localhost:5001/root/cloud-repo.git # 指定代码仓地址
              username: root # 用户名
              password: 12345678 # 密码
              default-label: main # 仓库分支
              search-paths: cloud-repo/* # 配置文件存放目录
    

    我本地开发调测使用的是gitlab代码仓,是直接使用docker容器拉起的,配置也很简单,大家可以去找一下相关的安装指导。
    我新建了一个cloud-repo仓库,在仓库下面有一个cloud-repo目录存放配置文件的信息:
    在这里插入图片描述

    启动服务器,同样通过http://{ip}:{port}/{application}-{profile}.properties可以访问到配置文件信息
    在这里插入图片描述

    到此,配置中心就配置完成了,下面就是微服务如何去获取到配置中心里面配置文件的信息。

客户端读取Spring Cloud Config配置

读取spring cloud config配置比较简单,只需要在配置文件中添加spring cloud config的地址以及需要读取的配置文件信息即可

pom文件中添加spring-cloud-config-client依赖

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

配置信息配置如下:

spring:
  application:
    name: eureka-client1
  cloud:
    config:
      name: config # 指定读取配置文件的名称
      uri: http://localhost:8888 # 指定config server的地址
      profile: default # 指定配置文件版本 默认是default

我们在client1添加一个获取配置文件中参数内容的方法

/**
 * @author yuanzhihao
 * @since 2021/11/26
 */
@RestController
@Slf4j
public class HelloController {
    @Value("${name}")
    private String name;
    
    @GetMapping("/hello")
    public String hello() {
        return "Hello " + name;
    }
}

启动client1,通过浏览器访问该接口,可以获取到name的值,读取成功!
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-laJjtbbP-1637942033416)(/Users/yuanzhihao/Library/Application Support/typora-user-images/image-20211126235142795.png)]

项目源码参考

源码地址:https://github.com/yzh19961031/SpringCloudDemo

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值