SpringCloud(Finchley.RC2)基于springboot 2.0.2搭建配置中心(二)

9 篇文章 0 订阅
5 篇文章 0 订阅

1、接着上一章的源码,咱们接着搭建微服务的架构。配置中心(config-server)的搭建

  1. 老规矩首先在项目中新建一个名字为config-server的module, 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>com.yqf</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.yqf</groupId>
        <artifactId>eureka-demos</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>



        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>

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

    </dependencies>

</project>

2.在ConfigServerApplication中开启配置,即添加@EnableConfigServer的配置

3.在resources目录下面新建一个名字为share的文件夹,该文件用于集中管理配置文件,文件目录如下图

这里写图片描述

4、config-server的配置文件(application.yml)配置如下

spring:
  cloud:
    config:
      server:
        native:
          search-loc
          ations: classpath:/shared    --设置配置文件存放路径
  profiles:
     active: native                    --本地存放配置文件
  application:
    name: config-server

# port
server:
  port: 8769

management:
  security:
    enabled: false

5、在eureka-client即(需要从config-server中获取配置的module)添加config的依赖

6、修改eureka-client配置文件如下图

spring:
  application:
    name: eureka-client
  cloud:
    config:
      uri: http://localhost:8769     --config-server的地址
      fail-fast: true
  profiles:
    active: dev                     --启动测试的配置文件

7、在config-server的share文件夹中新增一个名字为eureka-client-dev.yml的配置文件,这里文件命名规则是${spring.application.name}-${dev/prod} 前面是应用的名称后面跟上dev或prod即指明是获取测试的配置文件还是开发的配置文件

8、分别启动config-server 、eureka-server 、eureka-client, 当看到eureka-client的控制台输出如下日志时,即代表获取配置文件成功

2018-06-12 09:04:20.969  INFO [transfer,,,] 7632 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8769
2018-06-12 09:04:21.505  INFO [transfer,,,] 7632 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=transfer, profiles=[dev], label=null, version=null, state=null
2018-06-12 09:04:21.505  INFO [transfer,,,] 7632 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='classpath:/shared/transfer-dev.yml'}, MapPropertySource {name='classpath:/shared/application.yml'}]}
2018-06-12 09:04:21.594 INFO  com.jly.transfer.TransferApplication Line:663 - The following profiles are active: dev

至此spring的配置中心即搭建完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT管道工

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

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

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

打赏作者

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

抵扣说明:

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

余额充值