# SpringCloud 分布式配置中心 (Spring Cloud Config)


在微服务开发中使用Spring cloud Config作为分布式配置中心,从而可以方便的管理各个模块的配置文件

主要思路:Spring Cloud Config加载所有模块的配置信息,各模块启动的时候去配置中心读取各自的配置文件,最后各模块注册到Eureka 上实现微服务。

父Pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.7.RELEASE</version>
</parent>
<modules>
    <module>os_configserve</module>
    <module>os_eurekaserve</module>
    <module>order</module>
</modules>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注册中心(EureKa配置)

Maven配置:基本上的错误为版本号错误,版本错误见前面博客

  • pomx.ml
<parent>
    <artifactId>OnlineShop</artifactId>
    <groupId>com.li</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
  • 启动类
@SpringBootApplication
@EnableEurekaServer
public class OsEurekaserveApplication {
    public static void main(String[] args) {
        SpringApplication.run(OsEurekaserveApplication.class, args);
    }

}
  • application.yml
#    端口号
server:
  port: 8001
spring:
  application:
    name: eureka-provider
eureka:
  instance:
    hostname: localhost # Eureka访问地址
  client:
    register-with-eureka: false # 禁止自己注册自己
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Spring Cloud Config 配置中心
  • pom.xml
<parent>
    <artifactId>OnlineShop</artifactId>
    <groupId>com.li</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>os_configserve</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>
  • 配置中心启动类
@SpringBootApplication
@EnableConfigServer
public class OsConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OsConfigServerApplication.class,args);
    }
}
  • 配置中心配置
    application.yml
server:
  port: 8002
spring:
  application:
    name: config-provider
  profiles:
    active: native # 配置文件的获取方式本地读取
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared  # 本地配置文件的存放位置

配置读取文件的路径,在resources文件夹下建/shared文件夹,并且创建order-dev.yml配置文件,和后面的服务互相对应。order-dev.yml的内容如下

server:
  port: 8010 # order-dev的端口号

测试读取配置中心配置 模块
  • pom.xml
<parent>
    <artifactId>OnlineShop</artifactId>
    <groupId>com.li</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>
  • 启动类
@SpringBootApplication
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }
}
  • 测试类
@RestController
@RequestMapping("/order")
public class OrderController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index(){
        return "order的端口是"+this.port;
    }
}
  • bootstrap.yml配置文件
spring:
  application:
    name: order #之前在配置文件中的文件名字order-dev.yml
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8002 # 配置configserver的端口号
      fail-fast: true

依次启动注册中心、本地文件系统、服务

在这里插入图片描述
浏览器访问 localhost:8001 如下可以看到注册的服务
在这里插入图片描述
访问测试地址如下
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

全栈程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值