SpringCloud config(配置中心)

SpringCloud config (配置中心)

Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。一般外部配置文件存在github.com上。
下面是config的代码实现:
新建配置中心模块:
在这里插入图片描述
配置pom

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

配置application.yml:

server:
   port: 8888
spring:
   cloud:
      config:
         server:
            git:
            #存放外部配置文件的路径
               uri: https://github.com/panjiejieNB/configstu

路径https://github.com/panjiejieNB/configstu的源文件
在这里插入图片描述

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

配置main方法

package cn.pjj;

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

@SpringBootApplication
//配置中心服务注解
@EnableConfigServer
public class ConfigServer {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServer.class, args);
  }
}


启动配置中心(运行main方法)就成功了
其他模块调用外置配置:
使用bootstrap.yml替换之前application

spring:
  application:
  #服务名称
    name: userservice
  profiles:
    #环境切换
    active: dev
    # 配置中心 的ip和端口
    # 这个文件等价于 http://localhost:8888/userservice-dev.yml
  cloud:
    config:
      uri: http://localhost:8888

运行即可!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值