Spring Cloud(五):Spring Cloud Config分布式配置中心

  Spring Cloud Config就跟它名字的含义一样,是一个单独的配置中心的服务,所有的服务所需要的配置文件都可以从Cloud Config配置中心获取。Spring Cloud Config的服务为Config Server,Config Server 既可以从本地仓库当中读取配置文件,也可以从远程仓库Git当中读取。

  (一):从远程仓库Git当中读取。

在远程仓库Git当中创建配置项目,本案例使用码云,具体目录根据需要来定,本案例配置项目地址为:

https://github.com/PiCaQiuLory/spring-cloud-config

项目基于之前的项目体系,创建cloud-config服务模块(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">
    <parent>
        <artifactId>cloud-demo</artifactId>
        <groupId>spring-cloud-demo</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config</artifactId>

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

application.yml

server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/PiCaQiuLory/spring-cloud-config.git
          search-paths: respo
          username:
          password:

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class CloudConfigServerApplication {

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

修改原项目的eureka-feign模块用作客户端测试:

application.yml修改为:

注意:spring.application.name的值必须和远程仓库当中配置文件的名称对应

server:
  port: 8091
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: client-feign
  cloud:
    config:
      label: master
      profile: dev
      uri: http://localhost:8888

feign:
  hystrix:
    enabled: true

添加测试方法,在HelloController当中增加如下:

import com.eureka.feign.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @Value("${stringMsg}")
    private String message;

    @GetMapping("/feign")
    public String hello(String name){
        return helloService.hello(name);
    }


    @GetMapping("/config/message")
    public String getMessage(){
        return message;
    }

}

这里我注入了远程git仓库当中client-feign-dev.yml的stringMsg属性,该配文件如下:

启动服务,访问服务可以看见stringMsg已经成功注入

(二)使用本地仓库:

在cloud-config模块的resources目录下创建shared文件夹。将上面的client-feign-dev.yml配置文件复制到该目录下,修改配置文件application.yml为:

server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
  profiles:
    active: native
      #server:
      #  git:
      #    uri: https://github.com/PiCaQiuLory/spring-cloud-config.git
      #    search-paths: respo
      #    username:
      #    password:

启动服务:

访问上面相同的路径,可以看见网页打印出同样的信息。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值