SpringCloud配置中心--git版

倾然丶 夕夏残阳落幕 提交于 2019-11-26 14:14:46

今天,日月教大家如何配置git版的spring-cloud-config配置中心,这里只讲server端,因为其他的都和之前发的svn版的一样。话不多说直接上代码。

1、先在码云上创建一个仓库

并提交一个配置文件,里面配置hello: git-config-test
在这里插入图片描述
当然,有条件的同学,也可以自己搭建gitlab服务器。

2、创建spring-cloud-config-server项目

pom依赖

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

配置文件application.yml

server:
  port: 8001
    
spring:
  application:
    name: spring-cloud-config-server
  profiles:
    active: master
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/riyuewuyuan/spring-cloud-config.git
          default-label: ${spring.profiles.active}   #解决监控down
          
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/   ## 注册中心eurka地址
      
      
management:
  security:
     enabled: false

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

3、测试

先启动注册中心eureka,在启动spring-cloud-config-server
浏览器分别访问:
http://localhost:8080
http://localhost:8001/application-dev.yml
在这里插入图片描述

服务注册成功

在这里插入图片描述
可以看到,成功从git中获取配置数据,至此,git版配置中心搭建完成,其他的都可以参照之前发的svn版的。
ps:如果该文章有帮助到您,就点个赞吧!您的支持与肯定是我持续更新最大的动力。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!