欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 在SpringBoot项目中如何集成eureka

在SpringBoot项目中如何集成eureka

2024/10/24 15:15:39 来源:https://blog.csdn.net/nndsb/article/details/141397448  浏览:    关键词:在SpringBoot项目中如何集成eureka

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

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

目录

1. 创建 Eureka Server

1.1 创建一个 Spring Boot 项目

1.2 配置 Eureka Server

1.3 启动 Eureka Server

2. 创建 Eureka Client

2.1 创建一个新的 Spring Boot 项目

2.2 配置 Eureka Client

2.3 启动 Eureka Client

2.4 测试 Eureka Client

3. 多个客户端测试

4. 负载均衡和服务调用

4.1 添加 Feign 依赖

4.2 配置 Feign 客户端

4.3 创建一个简单的控制器

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 文件中,添加以下配置:

spring.application.name=eureka-server
server.port=8761eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

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

1.3 启动 Eureka Server

在主类中启用 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);}
}

现在,启动应用程序,并在浏览器中访问 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 文件中,添加以下配置:

spring.application.name=eureka-client
server.port=8080eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

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

2.3 启动 Eureka Client

在主类中启用 Eureka Client:

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 中添加以下依赖:

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

EurekaClientApplication 类上添加 @EnableFeignClients 注解:

@EnableFeignClients
@SpringBootApplication
public class EurekaClientApplication {...
}

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

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 控制器来测试:

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 {@Autowiredprivate 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 来管理微服务集群中的服务注册和发现,简化服务之间的通信。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com