在SpringBoot项目中如何集成eureka

Eureka 是 Netflix 开源的一个服务注册与发现的组件,通常用于 Spring Cloud 微服务架构中。集成 Eureka 和 Spring Boot 可以帮助我们创建一个高可用的服务注册中心,允许服务在集群中自动注册和发现。

下面是一个简单的 Spring Boot 项目集成 Eureka 的教程,分为两部分:Eureka Server 和 Eureka Client。

目录

[1. 创建 Eureka Server](#1. 创建 Eureka Server)

[1.1 创建一个 Spring Boot 项目](#1.1 创建一个 Spring Boot 项目)

[1.2 配置 Eureka Server](#1.2 配置 Eureka Server)

[1.3 启动 Eureka Server](#1.3 启动 Eureka Server)

[2. 创建 Eureka Client](#2. 创建 Eureka Client)

[2.1 创建一个新的 Spring Boot 项目](#2.1 创建一个新的 Spring Boot 项目)

[2.2 配置 Eureka Client](#2.2 配置 Eureka Client)

[2.3 启动 Eureka Client](#2.3 启动 Eureka Client)

[2.4 测试 Eureka Client](#2.4 测试 Eureka Client)

[3. 多个客户端测试](#3. 多个客户端测试)

[4. 负载均衡和服务调用](#4. 负载均衡和服务调用)

[4.1 添加 Feign 依赖](#4.1 添加 Feign 依赖)

[4.2 配置 Feign 客户端](#4.2 配置 Feign 客户端)

[4.3 创建一个简单的控制器](#4.3 创建一个简单的控制器)

[5. 总结](#5. 总结)


1. 创建 Eureka Server

1.1 创建一个 Spring Boot 项目

使用 Spring Initializr 创建一个新的 Spring Boot 项目,添加以下依赖:

  • Spring Boot DevTools
  • Spring Web
  • Eureka Server
1.2 配置 Eureka Server

src/main/resources/application.properties 文件中,添加以下配置:

bash 复制代码
spring.application.name=eureka-server
server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

这段配置指定了应用名称为 eureka-server,端口为 8761,并且表示这个服务本身不会注册到 Eureka 中(因为它是服务器)。

1.3 启动 Eureka Server

在主类中启用 Eureka Server:

java 复制代码
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);
    }
}

现在,启动应用程序,并在浏览器中访问 http://localhost:8761,你应该会看到 Eureka Server 的仪表盘页面。

2. 创建 Eureka Client

接下来,创建一个微服务,它将注册到 Eureka Server。

2.1 创建一个新的 Spring Boot 项目

再次使用 Spring Initializr 创建一个新的 Spring Boot 项目,添加以下依赖:

  • Spring Boot DevTools
  • Spring Web
  • Eureka Discovery Client
2.2 配置 Eureka Client

src/main/resources/application.properties 文件中,添加以下配置:

bash 复制代码
spring.application.name=eureka-client
server.port=8080

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

这里的配置指定了应用名称为 eureka-client,端口为 8080,并且定义了 Eureka Server 的地址。

2.3 启动 Eureka Client

在主类中启用 Eureka Client:

java 复制代码
package com.example.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}
2.4 测试 Eureka Client

启动 EurekaClientApplication 应用程序,并回到 Eureka Server 的仪表盘 (http://localhost:8761)。你应该会在页面上看到名为 eureka-client 的服务已经注册到 Eureka 中。

3. 多个客户端测试

你可以创建更多的客户端应用程序,或者复制并修改现有的 eureka-client 项目,将其部署到不同的端口,以便测试多个服务的注册和发现。

4. 负载均衡和服务调用

Eureka 的一个常见用法是与 Ribbon 或 Feign 一起使用,用于负载均衡和服务调用。可以在 eureka-client 中添加 Ribbon 或 Feign 的依赖,并编写一个服务调用逻辑:

4.1 添加 Feign 依赖

pom.xml 中添加以下依赖:

bash 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
4.2 配置 Feign 客户端

EurekaClientApplication 类上添加 @EnableFeignClients 注解:

java 复制代码
@EnableFeignClients
@SpringBootApplication
public class EurekaClientApplication {
    ...
}

创建一个 Feign 接口,用于调用其他服务:

java 复制代码
package com.example.eurekaclient;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient("eureka-client")
public interface ClientService {

    @GetMapping("/hello")
    String getHello();
}
4.3 创建一个简单的控制器

EurekaClientApplication 中创建一个 REST 控制器来测试:

java 复制代码
package com.example.eurekaclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private ClientService clientService;

    @GetMapping("/hello")
    public String hello() {
        return "Hello from eureka-client!";
    }

    @GetMapping("/call")
    public String callService() {
        return clientService.getHello();
    }
}

启动多个 eureka-client 实例,然后通过 /call 端点调用其他实例的 /hello 端点,这样你可以测试负载均衡和服务调用功能。

5. 总结

以上步骤展示了如何在 Spring Boot 项目中集成 Eureka,并创建一个简单的 Eureka Server 和 Client。你可以使用 Eureka 来管理微服务集群中的服务注册和发现,简化服务之间的通信。

相关推荐
自珍JAVA6 分钟前
高效处理Long列表与集合运算:基于RoaringBitmap的工具类解析与应用场景
后端
小码哥_常9 分钟前
Spring Boot项目上线秘籍:日志、监控、异常处理全攻略
后端
GreenTea1 小时前
AI 时代,工程师的不可替代性在哪里
前端·人工智能·后端
朦胧之1 小时前
AI 编程开发思维
前端·后端·ai编程
独自归家的兔3 小时前
OCPP 1.6 协议详解:StatusNotification 状态通知指令
开发语言·数据库·spring boot·物联网
希望永不加班3 小时前
Spring AOP 代理模式:CGLIB 与 JDK 动态代理区别
java·开发语言·后端·spring·代理模式
eggwyw3 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
浮游本尊4 小时前
一次合同同步背后的多阶段流水线:从外部主数据到本地歧义消解
后端
lv__pf4 小时前
springboot原理
java·spring boot·后端