栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring cloud config配置中心

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.yml、item-service-dev.yml、user-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 添加依赖


    
        springcloud1
        cn.tedu
        0.0.1-SNAPSHOT
    
    4.0.0

    sp09-config
    0.0.1-SNAPSHOT
    sp09-config
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    

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
      }
    }
  ]
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711040.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号