Spring Could——ConfigSever搭建

ConfigServer配置管理器本质上讲github上存储的配置文件,下载下来传递给其他客户端作为启动时的配置文件,对于大型的项目来说,具有很大的优势,对各类配置文件方便管理。下面主要分两步:

配置服务器搭建

依赖文件

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

添加启动类注解

package com.example.config_server;

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;

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {

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

}

配置文件

server:
 port: 8088
spring:
  application:
    name: configServer
  cloud:
    config:
      server:
        git:
        #          仓库地址
          uri: https://github.com/GISHanBo/springConfig.git
#          search-paths: my-sample-config git下的文件夹路径
#          username: 私有项目用户名
#          password:私有项目密码
#   启动程序后的测试地址 http://127.0.0.1:8088/master/application-dev.properties

这样服务器端就搭建完成了,运行服务器端程序,浏览器访问http://127.0.0.1:8088/master/application-dev.properties,mater为分支名,后面的为git里对应的配置文件名称,浏览器里就可以看到放在git里的配置文件信息

客户端使用

添加依赖

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

配置文件一定是bootstrap.properties,不能是application.propertie。否则配置文件后面会有冲突


#配置服务器地址
spring.cloud.config.uri=http://localhost:8088/
#文件名-之前内容
spring.cloud.config.name=application\
#文件名-之后点之前内容
spring.cloud.config.profile=consumer
#分支名称
spring.cloud.config.label=master

Contoller自己根据自己的需求编写就可以了,启动这个客户端,用配置github文件的接口和controller路径测试请求是不是能正常运行,最终测试正常,而且github中的配置文件修改端口后,重新运行,在新的接口正常运行

 

我的github项目配置如下图

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GIS开发者

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

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

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

打赏作者

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

抵扣说明:

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

余额充值