springcloud(六):config分布式配置中心和eureka实现分布式配置高可用

本文介绍如何构建分布式配置中心,包括本地仓库搭建、config-server与config-client配置流程,以及如何通过Eureka实现服务发现与整合。从github配置文件管理,到本地加载及远程服务调用,详细解析了配置中心的实现机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.分布式配置中心

1.1 构建一个本地仓库

下面是git本地仓库中的内容,config-respo为仓库,config-dev.properties为配置文件,config-dev.properties中的内容为 test ql
在这里插入图片描述

1.2 父项目

只有一个pom.xml
在这里插入图片描述

1.3 子项目config-server

pom.xml
在这里插入图片描述
主函数中开启config server
在这里插入图片描述
配置文件,当github是公开仓库,可以不用输账号密码
在这里插入图片描述

1.4 测试config-server

启动程序
在这里插入图片描述
输入http://localhost:8888/config/dev/master ,可以看到它找到了对应的github仓库地址和文件中的配置文件内容
在这里插入图片描述

1.6 子项目config-client

pom
在这里插入图片描述
controller
在这里插入图片描述
application.properties
在这里插入图片描述
bootstrap.properties
在这里插入图片描述

1.7 测试config-client

在这里插入图片描述

1.8 总结

整个项目的配置文件:github --> config-server --> config-client.最开始是在github这种公共管理的地方保存,后来被config-server这个项目加载到本地,config-client就可以直接访问这个项目中的配置文件。

2. eureka整合config

2.1改造config-client

在这里插入图片描述

主函数增加 @EnableEurekaClient
pom增加:

<!--引入eureka-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.2 config-server改造

application,properties

# erueka
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/

主函数增加:@EnableEurekaClient

pom

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.3 新建一个eureka-server

主函数


@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

application.yml

server:
  port: 8889

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

pom

	<dependencies>
        <!--引入eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>


        <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>

    </dependencies>

2.4 测试

在这里插入图片描述
访问 http://localhost:8881/hi
在这里插入图片描述

源码:https://github.com/LUK-qianliu/springcloud

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值