22SpringCloud - Spring Cloud Config 集成 Eureka 服务

简介: 22SpringCloud - Spring Cloud Config 集成 Eureka 服务

分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config组件中,分两个角色,一是config server,二是config client,业界也有些知名的同类开源产品,比如百度的disconf

相比较同类产品,SpringCloudConfig最大的优势是和Spring无缝集成,支持Spring里面EnvironmentPropertySource的接口,对于已有的spring应用程序的迁移成本非常低,在配置获取的接口上是完全一致,结合SpringBoot可使你的项目有更加统一的标准(包括依赖版本和约束规范),避免了应为集成不同开软件源造成的依赖版本冲突。

添加依赖

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

开启服务注册

在程序的启动类 ConfigServerApplication.java 通过 @EnableEurekaClient开启 Eureka 提供者服务

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

修改配置

修改配置文件 application.properties ,添加 eureka 注册中心地址 http://localhost:8761/eureka/

spring.application.name=config-server
server.port=8888
spring.cloud.config.label=master
spring.cloud.config.server.git.uri=https://github.com/souyunku/spring-cloud-config.git
spring.cloud.config.server.git.search-paths=spring-cloud-config
#spring.cloud.config.server.git.username=your username
#spring.cloud.config.server.git.password=your password
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
  • spring.cloud.config.server.git.uri:配置git仓库地址
  • spring.cloud.config.server.git.searchPaths:配置仓库路径
  • spring.cloud.config.label:配置仓库的分支
  • spring.cloud.config.server.git.username:访问git仓库的用户名
  • spring.cloud.config.server.git.password:访问git仓库的用户密码
  • eureka.client.serviceUrl.defaultZone:eureka注册中心地址

Git仓库如果是私有仓库需要填写用户名密码,示例是公开仓库,所以不配置密码。

远程Git仓库

spring-cloud-config 文件夹下有 application-dev.properties,application-test.properties 三个文件,内容依次是:content=hello dev,content=hello test,content=hello pre

测试服务

启动程序 ConfigServerApplication

访问 Config Server服务:http://localhost:8888/springCloudConfig/dev/master

{
    "name": "springCloudConfig",
    "profiles": [
        "dev"
    ],
    "label": "master",
    "version": "b6fbc2f77d1ead41d5668450e2601a03195eaf16",
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/souyunku/spring-cloud-config.git/application-dev.properties",
            "source": {
                "content": "hello dev"
            }
        }
    ]
}

证明配置服务中心可以从远程程序获取配置信息。

http请求地址和资源文件映射如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

客户端端配置

添加依赖

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

开启服务注册

在程序的启动类 EurekaProviderApplication,通过 @Value 获取服务端的 content 值的内容

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaProviderApplication {
    @Value("${content}")
    String content;
    @Value("${server.port}")
    String port;
    @RequestMapping("/")
    public String home() {
        return "Hello world ,port:" + port+",content="+content;
    }
    public static void main(String[] args) {
        SpringApplication.run(EurekaProviderApplication.class, args);
    }
}

添加配置

修改配置文件 application.properties 添加 Eureka注册中心,配置从springCloudConfig配置中心读取配置,指定springCloudConfigService 服务名称.

spring.application.name=eureka-provider
server.port=8081
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri=http://localhost:8888/
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
  • spring.cloud.config.label 指明远程仓库的分支
  • spring.cloud.config.profile
  • dev开发环境配置文件
  • test测试环境
  • pro正式环境
  • #spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址(注释掉)
  • spring.cloud.config.discovery.enabled=true 是从配置中心读取文件。
  • spring.cloud.config.discovery.serviceId=config-server 配置中心的servieId,服务名称,通过服务名称去 Eureka注册中心找服务

测试服务

依次启动项目:

  • spring-cloud-eureka-service
  • spring-cloud-config-server
  • spring-cloud-eureka-provider-1
  • spring-cloud-eureka-provider-2
  • spring-cloud-eureka-provider-3
  • spring-cloud-feign-consumer

启动该工程后,访问服务注册中心,查看服务是否都已注册成功:http://localhost:8761/

查看eureka 监控,看服务是否都注册成功

命令窗口,通过curl http://127.0.0.1:9000/hello 访问服务,或者在浏览器访问http://127.0.0.1:9000/hello F5 刷新.

修改了Git仓库的配置后,需要重启服务,才可以得到最新的配置。

目录
相关文章
|
人工智能 Java Serverless
【MCP教程系列】搭建基于 Spring AI 的 SSE 模式 MCP 服务并自定义部署至阿里云百炼
本文详细介绍了如何基于Spring AI搭建支持SSE模式的MCP服务,并成功集成至阿里云百炼大模型平台。通过四个步骤实现从零到Agent的构建,包括项目创建、工具开发、服务测试与部署。文章还提供了具体代码示例和操作截图,帮助读者快速上手。最终,将自定义SSE MCP服务集成到百炼平台,完成智能体应用的创建与测试。适合希望了解SSE实时交互及大模型集成的开发者参考。
15630 60
|
人工智能 API 开发工具
GitHub官方开源MCP服务!GitHub MCP Server:无缝集成GitHub API,实现Git流程完全自动化
GitHub MCP Server是基于Model Context Protocol的服务器工具,提供与GitHub API的无缝集成,支持自动化处理问题、Pull Request和仓库管理等功能。
3796 2
GitHub官方开源MCP服务!GitHub MCP Server:无缝集成GitHub API,实现Git流程完全自动化
|
Prometheus 监控 Cloud Native
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务实现步骤
1024 0
|
人工智能 自然语言处理 Java
对话即服务:Spring Boot整合MCP让你的CRUD系统秒变AI助手
本文介绍了如何通过Model Context Protocol (MCP) 协议将传统Spring Boot服务改造为支持AI交互的智能系统。MCP作为“万能适配器”,让AI以统一方式与多种服务和数据源交互,降低开发复杂度。文章以图书管理服务为例,详细说明了引入依赖、配置MCP服务器、改造服务方法(注解方式或函数Bean方式)及接口测试的全流程。最终实现用户通过自然语言查询数据库的功能,展示了MCP在简化AI集成、提升系统易用性方面的价值。未来,“对话即服务”有望成为主流开发范式。
10355 7
|
存储 Java 开发工具
SpringCloud实战7-Config分布式配置管理
分布式环境下的统一配置框架,已经有不少了,比如百度的disconf,阿里的diamand 官方文档对spring Cloud Config的描述如下:   Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持,使用Config Server,您可以在所有环境中管理应用程序的外部属性。
3093 0
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1500 0
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1448 0
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
620 0
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
1189 0

热门文章

最新文章