Java基础、中级、高级、架构面试资料

Spring Cloud 教程第十三章 spring-cloud-config-server使用教程

JAVA herman 2499浏览 0评论
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

Spring Cloud 是一个全家桶,提供的各种组件或者说是插件很齐全。你能想到的和不能想不到的都有。前面我已经陆陆续续写了 12 章的教程,但是对于整个 Spring Cloud 来说才是冰山一角,所以我们需要继续 Spring CLoud 的系列教程。本文是第十三章,对 spring-cloud-config-server 的简单使用教程。

前面的几个章节,我们陆陆续续写了很多的服务,每个服务都有单独的配置。但是随着章节的深入,服务越来越多。我们随着而来面临的是服务的管理问题。在实际的生产环境中,Spring Cloud 的微服务实例多达上千个,每个服务都有一套配置。它们很分散,如果我想查看某一个服务的具体配置,还需要登录到对应的服务器去看配置文件,或者查看具体版本的配置文件中的代码,对每个运维来说就显得非常的麻烦,我们能不能有一个集中式的配置中心,来管理这些服务的各个配置文件呢?

答案当然是有。我们能想到的,Spring Cloud 都能想到。本文要讲的 spring-cloud-config-server 就是对各个服务的配置文件的一个集中式管理组件。

关于 spring-cloud-config-server 我就不在详细的介绍,网上有很多的文章来介绍它。我们还是让重点放在如何使用上吧。下面我一起来一步一步搞定它。

为了方便演示 spring-cloud-config-server 的效果,本文采用 git 的版本控制管理方式来存放配置文件。

首先,我们创建一个 xttblog-cloud-config 的仓库或文件夹。在它下面创建 3 个配置文件,分别代表开发环境,测试环境,生产环境。其中 xttblog-dev.yml 文件的配置内容如下:

site:
  url: www.xttblog.com
  name: 业余草
  active: dev

xttblog-uat.yml 文件的配置内容如下:

site:
  url: www.xttblog.com
  name: 业余草
  active: uat

xttblog-pat.yml 文件的配置内容如下:

site:
  url: www.xttblog.com
  name: 业余草
  active: pat

不方便创建的,也可以使用我的公共库中的示例配置 xttblog-cloud-config

做好上面的工作后,我们再创建一个 xttblog-config-server 工程。它的 pom.xml 配置文件内容如下:

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

作为配置中心的 server 端,它同样的需要注册到 eureka,具体的注册中心参考我之前的文章,或者  xttblog-eureka-server 工程。

对应的 application.yml 配置文件内容如下:

server:
  port: 8877

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

spring:
  application:
    name: xttblog-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xmt1139057136/xttblog-cloud.git
#          username:
#          password:
          search-paths: xttblog-cloud-config

这个配置文件,我们是要指定 git 的配置仓库在哪里。除了 git 外,Spring cloud 同样的支持 SVN,后面如果有时间的话,我们再来说 SVN 的使用。

启动类也很简单,只是多了一个 @EnableConfigServer 注解而已。

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

做完所有的开发工作后,我们启动当前这个工程,然后在浏览器里输入 http://localhost:8877/xttblog/dev 就能访问到 xttblog-dev.yml 的内容。改变最后一个参数,我们就能访问其他其他的内容。

是不是很惊奇,Spring Cloud 就是这么简单。关于如何使用配置中心这个服务,其他服务如何通过配置中心获取到配置内容,我们下一章再见!

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » Spring Cloud 教程第十三章 spring-cloud-config-server使用教程