spring cloud config配置中心

1 spring cloud config 配置中心

eureka只做注册中心,config只做配置中心,nacos是把两部分合并到了一起
配置中心是用来集中管理各个模块的配置文件
在这里插入图片描述

1.1 把本地项目上传到gitee仓库

1.1.1 把所有的配置单独放到一个文件夹中

先在springcloud1主工程下新建一个名为config的文件夹,用于存放order-service、item-service、user-service的配置文件分别复制到这个config文件夹内,并重命名为order-service-dev.ymlitem-service-dev.ymluser-service-dev.yml
在这里插入图片描述

1.1.2 选择springcloud1工程目录

菜单 VCS => create git repository,创建git本地项目,即把springcloud1变成git项目
在这里插入图片描述

1.1.3 点击提交(对勾按钮)

在这里插入图片描述

1.1.4 勾选所有文件,填写信息,点击提交

在这里插入图片描述

1.1.5 点击Commit and Push,或是Ctrl + Shift + K快捷键

会弹出下面这个框,先在gitee上创建一个空的仓库,然后把空仓库的地址填写到这里
在这里插入图片描述

1.1.6 点击push

在这里插入图片描述

  • 如果遇到以下错误
hint: to the same ref. You may want to first integrate the remote changes
Done
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

则说明线上的gitee仓库里面的有东西,可以点“管理=>清空仓库"清一下再上传就可以了

1.1.6 配置不覆盖

config文件夹下的各个yml文件中添加如下配置,表示从配置中心下载的配置不覆盖本地的配置,比如我们对同一个项目设置了两个启动配置文件,分别设置了两个不同的端口时,如果不做如下的配置,则启动后下载了配置中心的配置后,本地设置的这个端口就被覆盖掉了

spring:
  cloud:
    config:
      override-none: true

1.2 搭建配置中心

1.2.1 新建spring-module:sp09-config

在这里插入图片描述

1.2.2 添加依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud1</artifactId>
        <groupId>cn.tedu</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>sp09-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sp09-config</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

1.2.3 配置文件

server:
  port: 6001
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zidieq/mypro
          search-paths: config
eureka:
  client:
    service-url:
      defaultZone: http://172.18.6.173:2001/eureka,http://172.18.6.173:2002/eureka
  instance:
    ip-address: 172.18.6.192
    prefer-ip-address: true

1.2.4 主程序添加 @EnableConfigServer@EnableDiscoveryClient

package cn.tedu.sp09config;

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;

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class Sp09ConfigApplication {

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

1.2.5 启动测试

启动后,访问http://localhost:6001/order-service/dev
得到类似如下内容,则说明配置成功

{
  "name": "order-service",
  "profiles": [
    "dev"
  ],
  "label": null,
  "version": "1ddce4896dc83ff77a25fc32afc9ee5c4f3e9c55",
  "state": null,
  "propertySources": [
    {
      "name": "https://gitee.com/zidieq/mypro/config/order-service-dev.yml",
      "source": {
        "spring.application.name": "order-service",
        "server.port": 8201,
        "eureka.client.service-url.defaultZone": "http://172.18.6.173:2001/eureka,http://172.18.6.173:2002/eureka",
        "eureka.instance.ip-address": "172.18.6.192",
        "eureka.instance.prefer-ip-address": true,
        "ribbon.MaxAutoRetries": 0,
        "ribbon.MaxAutoRetriesNextServer": 1
      }
    }
  ]
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水晶心泉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值