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

Spring Cloud 教程第十四章 spring-cloud-starter-config使用教程

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

不知不觉已经写到第十四章了,Spring cloud 的使用很简单,但是要深入就需要花费更多的精力。本文我们通过一个案例来搞定 spring-cloud-starter-config,也就是配置中心的使用。

配置中心其实理解起来很简单,通过下面的一张图,我相信你就会明白吧。

Spring cloud 配置中心

别看这张图丑,但是能大致说明整个配置中心的架构交互方式。下面我们开始具体的代码吧!

还是老规矩,我们先创建一个名为 xttblog-config-client 的工程。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-starter-config</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-web</artifactId>
    </dependency>

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

然后是 bootstrap.yml 文件的内容:

server:
  port: 8876

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

spring:
  application:
    name: xttblog
  cloud:
    config:
      label: master
      profile: dev
      # 添加zuul 的注意,这里配zuul的地址进行转发
      uri: http://localhost:8877/

这里需要说一下的是为什么我们写的是 bootstrap.yml 而不是 application.yml。这是因为 spring boot 的加载属性文件有优先级。bootstrap.yml 的优先级高,需要在加载属性之前去 spring cloud config server 上取配置文件,那 spring cloud config 相关配置就是需要最先加载的,而 bootstrap.yml 的加载是先于 application.yml 的,所以 config client 要配置 config 的相关配置就只能写到 bootstrap.yml 里了。

接着就是启动类了,代码如下:

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

下面我们再来写一个 Controller 类,来验证我们配置文件获取的内容对不对。代码如下:

@RestController
public class XttblogController {

    @Value("${site.url}")
    private String url;
    @Value("${site.active}")
    private String active;

    @RequestMapping("/test")
    public String test(){
        return "test:test config client, url: " + url + ",active: " + active;
    }
}

到这里所有的代码都写完了,我们启动程序。在浏览器里输入 http://localhost:8876/test 就能看到我们成功的获取到了 xttblog-dev.yml 配置文件中的属性内容。

到这里看似 Spring Cloud 的内容差不多写完了,但是你仔细想想,我们是不是还缺点什么。后面我们继续学习!

业余草公众号

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

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