当前位置: 首页 > article >正文

Spring Cloud集成Eurake

Spring Cloud 集成 Eureka 是一个常见的微服务架构实现,用于服务发现和注册。Eureka 是 Netflix 开源的一个服务注册和发现工具,Spring Cloud Netflix 提供了对 Eureka 的支持。下面是如何在 Spring Cloud 项目中集成 Eureka 的步骤:

先创建一个maven项目父项目模块,然后在父模块下创建各种服务

1. 创建 Eureka Server

首先,你需要创建一个 Eureka Server,它将负责管理服务注册表并处理服务实例的注册和注销。

1.1 引入依赖

在你的 pom.xml 中引入 Eureka Server 的依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
1.2 配置 Eureka Server

在主应用类上添加 @EnableEurekaServer 注解,使该应用成为 Eureka Server:

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
1.3 配置文件

application.ymlapplication.properties 文件中添加配置:

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  server:
    enable-self-preservation: false

这将配置 Eureka Server 在 localhost:8761 上运行,并禁用自我注册。

2. 创建 Eureka Client

现在,你需要创建一个或多个 Eureka Client,即你的微服务应用,它们将向 Eureka Server 注册自己,并从中获取其他服务的地址。

2.1 引入依赖

在你的 pom.xml 中引入 Eureka Client 的依赖:

<dependencies>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
2.2 配置 Eureka Client

在你的微服务主应用类上添加 @EnableEurekaClient 注解,使其成为 Eureka Client:

package com.example.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}
2.3 配置文件

application.ymlapplication.properties 中配置 Eureka Client:

server:
  port: 8080

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true
    instance-id: eurake-client1
spring:
  application:
    name: eurake-client

这个配置文件指定 Eureka Server 的地址为 http://localhost:8761/eureka/,并将服务实例注册到该地址。

3. 启动 Eureka Server 和 Client

  1. 启动 Eureka Server(在 localhost:8761 运行)。
  2. 启动 Eureka Client,客户端应用会自动注册到 Eureka Server。
  3. 你可以访问 http://localhost:8761 查看 Eureka Dashboard,看到所有注册的服务实例。

4. 调用其他服务(可选)

你可以使用 @LoadBalanced 注解的 RestTemplate 或 Feign 客户端来调用其他已注册的服务。

使用 RestTemplate
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MyService {

    @Autowired
    private RestTemplate restTemplate;

    public String callService() {
        return restTemplate.getForObject("http://SERVICE-NAME/endpoint", String.class);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
使用 Feign 客户端
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "SERVICE-NAME")
public interface MyFeignClient {

    @GetMapping("/endpoint")
    String callService();
}

总结

以上步骤详细介绍了如何在 Spring Cloud 项目中集成 Eureka,包含了 Eureka Server 和 Eureka Client 的配置及使用。通过这些配置,你可以实现微服务之间的自动发现和通信。


http://www.kler.cn/news/304247.html

相关文章:

  • 《PneumoLLM:利用大型语言模型的力量进行尘肺病诊断》|文献速递--基于深度学习的医学影像病灶分割
  • mysql笔记4(数据类型)
  • Nginx 实现会话保持的方式配置
  • echarts饼图让部分数据显示在图外,部分显示在图内
  • 数据结构应用实例(五)——关键路径
  • 学python要下什么包吗,有推荐的教程或者视频吗?
  • SprinBoot+Vue山西文旅网的设计与实现
  • 软件测试学习笔记丨Postman实战练习
  • 黑链、黑帽、明链分别是什么意思
  • JavaScript --函数作用域变量的使用规则(局部和访问)
  • 研究生深度学习入门的十天学习计划------第十天
  • LLM 工程师入门:生成式AI的简易指南
  • 【Vue】移动端访问Vue项目页面无数据,但是PC访问有数据
  • Linux定时启动jar应用shell脚本分享
  • 基于springboot的二手物品管理系统的设计与实现 (含源码+sql+视频导入教程)
  • C语言实现一个简单的点歌系统
  • XSS和sql注入部分场景测试用例样例
  • 将复杂类型列展开成多行,附带json解析
  • pandas 将多条记录整合成一条记录,每条记录的year和month字段组成新的字段名
  • MySQL从C盘迁移到D盘
  • Git的学习笔记
  • 服务器与个人计算机之间的区别
  • Java项目: 基于SpringBoot+mybatis+maven课程答疑系统(含源码+数据库+毕业论文)
  • 【Ubuntu】Ubuntu双网卡配置 实现内外网互不影响同时可用
  • KubeCon China 回顾|快手的 100% 资源利用率提升:从裸机迁移大规模 Redis 到 Kubernetes
  • 深度学习--对抗生成网络(GAN, Generative Adversarial Network)
  • Pr 入门系列之三:挑选与添加媒体到序列(上)
  • UQpy | 不确定性量化Python工具箱推荐
  • Spring和MyBatis常见面试题总结
  • 房屋租赁|基于springboot的房屋租赁管理系统设计与实现(附项目源码+论文+数据库)