《微服务系列:SpringCloudConfig配置中心》

说在前头:本人为大二在读学生,书写文章的目的是为了对自己掌握的知识和技术进行一定的记录,同时乐于与大家一起分享,因本人资历尚浅,能力有限,文章难免存在一些错漏之处,还请阅读此文章的大牛们见谅与斧正。若在阅读时有任何的问题,也可通过评论提出,本人将根据自身能力对问题进行一定的解答。 

前言

 SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

SpringCloud Config分为服务端和客户端两个部分。

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

接下来我们来构建一个服务配置中心。

1.创建eureka-server

①创建一个名为eureka-server的工程,作为配置中心

②依赖配置文件内容

<?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">
    <parent>
        <artifactId>Bosen</artifactId>
        <groupId>com.bosen.www</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
​
    <artifactId>eureka-server</artifactId>
​
    <dependencies>
        <!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
</project>

③启动类内容

package com.bosen.eureka.server;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
​
/**
 * <p>eureka服务端启动类</p>
 * @author Bosen 2021/5/20 13:47
 */
@SpringBootApplication// springboot启动配置
@EnableEurekaServer// 开启eureka服务发现功能
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
​

④配置文件内容

spring:
  application:
    name: eureka-server # 应用名
​
server:
  port: 8761 # 端口号
​
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false # 是否注册自身
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/   # 注册地址

⑤启动项目:启动成功后控制台日志输入如下

2.在gitee创建配置仓库

SpringCloudConfig的配置文件一般都放在github中统一托管,但国内访问github相对较慢,因此我们使用国内的gitee作为配置托管中心进行测试。

我们需要在gitee中创建一个保存项目配置文件的操作,当我们启动项目时,项目通过该仓库获取配置信息。

在gitee中注册完成后,创建一个仓库的流程如下:

3.创建config-server

在gitee创建好仓库后,我们接下来需要创建配置中心服务,用于管理各个服务的配置信息。

①创建config-server服务:过程如下图

②依赖配置文件内容

<?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">
    <parent>
        <artifactId>Bosen</artifactId>
        <groupId>com.bosen.www</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
​
    <artifactId>config-server</artifactId>
​
    <dependencies>
        <!-- config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
</project>

③创建启动类

package com.bosen.config.server;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
​
/**
 * <p>配置中心启动类</p>
 * @author Bosen 2021/5/21 13:10
 */
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

④启动类内容说明

  • EnableEurekaClient:声明为eureka客户端,需要向eureka注册中心注册,并依赖注册中心完成各个服务的配置信息装配。

  • EnableConfigServer:开启配置中心服务。

⑤设置配置文件application.yml

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/bosen-once/bosen # 存放配置文件的仓库
          default-label: master # 分支
          search-paths: config # 对应的文件夹
          #username:      # 如果仓库是公共的不需要输入用户名和密码,否则必须要输入
          #password:      # 密码(可以使用本机公钥)
​
server:
  port: 8888
​
eureka:
  client:
    serviceUrl:
      defautlZone: http://localhost:8761/eureka/

⑥启动服务:启动成功后如下

⑦访问eureka-server查看config-server是否已经成功注册

访问http://localhost:8761/ 如下

4.创建用于测试的eureka-client客户端

截止到上一步,我们已经成功准备好,服务注册发现中以及服务配置中心,接下来我们需要创建一个用于测试的eureka-client服务

①依赖配置

<?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">
    <parent>
        <artifactId>Bosen</artifactId>
        <groupId>com.bosen.www</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
​
    <artifactId>eureka-client</artifactId>
​
    <dependencies>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!-- config-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
    </dependencies>
</project>

②启动类

package com.bosen.eureka.client;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
​
@SpringBootApplication
@EnableEurekaClient
​
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

③编写一个控制器

package com.bosen.eureka.client.controller;
​
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
public class IndexController {
    @RequestMapping("/")
    public String index() {
        return "This is IndexController index();";
    }
}
​

④配置文件

注意:配置文件命名应当使用bootstrap,不建议使用application。原因:bootstrap的优先级大于application,需要从远端获取配置文件时,应该使用优先级大的bootstrap,以免发生不必要的错误)

spring:
  application:
    name: eureka-client # 当前服务应用名
  cloud:
    config:
      label: master # 仓库分支
    discovery:
      enabled: true # 开启 Config 服务发现与注册
      client:
        simple:
          local:
            service-id: config-server # 配置中心应用名

⑤在gitee中编写该服务的配置文件

server:
    port: 8001 # 端口号
​
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/   # 注册地址

⑥需要注意的地方

配置文件的配置规则是根据${spring.application.name}-${profiles}.yml的规则来匹配,即保存在gitee中的配置文件命名方式应该以应用名命名,如果配置了启动环境还应当加上环境参数,由于现在是测试阶段,我们没有添加环境参数,直接以服务名命名即可。

当我们的配置文件编写完毕后,启动我们的eureka-client服务,启动成功如下:

5.测试

服务注册发现中心,服务配置中心以及用于测试的客户端服务启动成功后,我们先访问服务注册发现中心http://localhost:8761/,可以看到我们eureka-client以成功注册

并且我们查看eureka-client的日志信息,可以很明确的看到,项目在启动时去访问了http://localhost:8888,这正是服务配置中心的地址,接着通过访问https://gitee.com/bosen-once/bosen/config/eureka-client.yml获取对应的配置文件


6.总结

​本篇我们简单介绍了SpringCloudConfig服务配置中心,并结合eureka实现了远程调用服务配置文件,相信聪明的你们已经学会了SpringCloudConfig使用方法。编程道路还很漫长,让我们一起加油吧。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云丶言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值