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

SpringCloud03-Eureka02-搭建Eureka服务

一、搭建Eureka Server(服务注册中心)

1. 创建Spring Boot项目

使用Spring Boot项目生成工具或IDE创建一个新的Spring Boot项目。

2. 添加依赖

pom.xml文件中添加Spring Cloud和Eureka Server的依赖:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.0</version><relativePath/> <!-- lookup parent from repository -->
</parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.1</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement><dependencies><!-- Spring Boot Web starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Eureka Server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
</dependencies>

3. 创建启动类

创建Eureka Server的启动类,并添加@EnableEurekaServer注解:

package com.example.eureka;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);}
}

4. 配置application.yml

src/main/resources目录下创建或修改application.yml文件:

eureka.client,serviceUrl.defaultZone,配置的是eureka-server自己的地址,在单机模式下,这个配置实际上没有作用!

defaultZone 的完整含义是:"当前服务应该向哪个Eureka Server注册"

5. 启动Eureka Server

运行启动类,然后访问http://localhost:8761就可以看到Eureka Server的管理界面。

服务的实例列表,status: up是正常启动了,down就是挂了。


6、小结


7、Eureka defaultZone配置详解

(1)问题分析

为什么Eureka Server要配置自己的地址作为defaultZone?

这看起来很奇怪:自己的Eureka Server为什么还要指向自己?


(2)defaultZone的真正含义

defaultZone 的完整含义是:"当前服务应该向哪个Eureka Server注册"

用一张表格来说明:

组件defaultZone的作用配置示例
Eureka Client(如user-service)告诉自己应该向哪个Eureka Server注册http://localhost:8761/eureka/
Eureka Server这是一个特殊情况,下面详细说明取决于是单机还是集群

情况1:Eureka Server单机部署

当你只有一个Eureka Server时,配置应该是:

解释:

  • registerWithEureka: false 表示"我自己不注册"
  • fetchRegistry: false 表示"我不从其他地方获取注册表"
  • 因为前两个都是false,所以 defaultZone 这行实际上没有作用,可以写也可以不写

情况2:Eureka Server集群部署(这是重要的!)

当你有多个Eureka Server时,它们之间需要相互注册和同步数据

假设你有3个Eureka Server:

实例端口主机名
Server18761eureka1.example.com
Server28762eureka2.example.com
Server38763eureka3.example.com

Server1的配置

server:port: 8761spring:application:name: eureka-servereureka:instance:hostname: eureka1.example.comregister-with-eureka: true      # ← 改为true!注册到其他Serverfetch-registry: true            # ← 改为true!从其他Server获取数据client:serviceUrl:# Server1向Server2和Server3注册defaultZone: http://eureka2.example.com:8762/eureka/,http://eureka3.example.com:8763/eureka/server:enable-self-preservation: false

Server2的配置

server:port: 8762spring:application:name: eureka-servereureka:instance:hostname: eureka2.example.comregister-with-eureka: truefetch-registry: trueclient:serviceUrl:# Server2向Server1和Server3注册defaultZone: http://eureka1.example.com:8761/eureka/,http://eureka3.example.com:8763/eureka/server:enable-self-preservation: false

Server3的配置

server:port: 8763spring:application:name: eureka-servereureka:instance:hostname: eureka3.example.comregister-with-eureka: truefetch-registry: trueclient:serviceUrl:# Server3向Server1和Server2注册defaultZone: http://eureka1.example.com:8761/eureka/,http://eureka2.example.com:8762/eureka/server:enable-self-preservation: false

关键点:

  • 每个Server都配置 register-with-eureka: true
  • 每个Server都配置 fetch-registry: true
  • 每个Server的 defaultZone 指向其他的Server(不是自己)
  • 这样形成一个集群,Server之间相互同步数据

情况3:你看到的配置(官方示例中)

在很多官方文档或教程中,你会看到Eureka Server的配置是这样的:

eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这是为什么?

这其实是一个保险起见的配置方式。即使你设置了 registerWithEureka: false,也还是指定了defaultZone。

这样做的好处是:

  • 如果以后想改成集群模式,只需要改一下 registerWithEurekafetchRegistry 的值,不需要改defaultZone
  • 具有前向兼容性

总结

配置项单机模式集群模式
registerWithEurekafalsetrue
fetchRegistryfalsetrue
defaultZone指向自己(实际不用)指向其他Server
作用简单、独立高可用、容错

你看到的配置

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这其实是在为将来的集群部署预留配置空间。在单机模式下,因为 registerWithEureka: false,这行配置实际上不起作用。


二、搭建Eureka Client(服务提供者user-service)

1. 创建Spring Boot项目

创建一个新的Spring Boot项目作为服务提供者。

2. 添加依赖

pom.xml文件中添加Eureka Client的依赖:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.0</version><relativePath/>
</parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.1</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement><dependencies><!-- Spring Boot Web starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Eureka Client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
</dependencies>

3. 创建启动类

创建Eureka Client的启动类,并添加@EnableEurekaClient注解:

package com.example.service;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class UserServiceApplication {public static void main(String[] args) {SpringApplication.run(UserServiceApplication.class, args);}
}

4. 创建业务Controller

创建一个简单的Controller来提供服务:

package com.example.service.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowiredprivate DiscoveryClient discoveryClient;@GetMapping("/user/list")public String getUserList() {return "User list from service";}@GetMapping("/services")public Object getServices() {// 获取所有注册的服务return discoveryClient.getServices();}@GetMapping("/service-instance")public Object getServiceInstance() {// 获取user-service的所有实例return discoveryClient.getInstances("user-service");}
}

5. 配置application.yml

src/main/resources目录下创建或修改application.yml文件:

server:port: 8081spring:application:name: user-serviceeureka:client:serviceUrl:# 指定Eureka Server的地址defaultZone: http://localhost:8761/eureka/instance:# 优先使用IP地址而不是机器名prefer-ip-address: true# 实例的唯一标识instance-id: ${spring.application.name}-${server.port}# 心跳间隔(秒),默认30秒lease-renewal-interval-in-seconds: 30# 过期时间(秒),默认90秒,Eureka Server在90秒内没有收到心跳则将该实例删除lease-expiration-duration-in-seconds: 90

6. 启动Eureka Client

运行启动类,服务会自动注册到Eureka Server。访问http://localhost:8761可以看到该服务已注册。


7、小结


8、启动多次


三、搭建Eureka Client(服务消费者order-service)

1. 创建Spring Boot项目

创建另一个Spring Boot项目作为服务消费者。

2. 添加依赖

与服务提供者相同,添加Eureka Client和Ribbon(用于负载均衡)的依赖:

<dependencies><!-- Spring Boot Web starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Eureka Client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- Ribbon (负载均衡) --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency>
</dependencies>

3. 创建启动类

package com.example.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableEurekaClient
public class OrderServiceApplication {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(OrderServiceApplication.class, args);}
}

4. 创建业务Controller

创建一个Controller来调用其他服务:

package com.example.consumer.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.List;@RestController
public class OrderController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;@GetMapping("/order/create")public String createOrder() {// 方式1:手动从Eureka获取服务信息List<ServiceInstance> instances = discoveryClient.getInstances("user-service");if (instances.isEmpty()) {return "user-service is not available";}ServiceInstance instance = instances.get(0);String url = String.format("http://%s:%s/user/list", instance.getHost(), instance.getPort());// 调用user-serviceString result = restTemplate.getForObject(url, String.class);return "Order created, user info: " + result;}@GetMapping("/order/create-ribbon")public String createOrderWithRibbon() {// 方式2:使用@LoadBalanced注解,RestTemplate会自动进行负载均衡String url = "http://user-service/user/list";String result = restTemplate.getForObject(url, String.class);return "Order created with Ribbon, user info: " + result;}
}

如果要使用Ribbon的负载均衡功能,需要修改启动类中的RestTemplate配置:

@Bean
@LoadBalanced  // 添加这个注解
public RestTemplate restTemplate() {return new RestTemplate();
}

【注意】:

        要是不加=@LoadBalanced 这个注解,使用restTemplate调用服务名是不起作用的!但是调用ip + 端口号,是能成功的!

5. 配置application.yml

server:port: 8082spring:application:name: order-serviceeureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/instance:prefer-ip-address: trueinstance-id: ${spring.application.name}-${server.port}

6. 启动Eureka Client

运行启动类,访问http://localhost:8761可以看到order-service也已注册。


四、测试Eureka服务

  1. 启动Eureka Server(8761端口)
  2. 启动user-service(8081端口)
  3. 启动order-service(8082端口)
  4. 访问http://localhost:8761查看注册的服务
  5. 访问http://localhost:8082/order/create-ribbon测试服务调用

这样就完成了一个简单的Eureka服务注册与发现的搭建。在实际项目中,可能还需要添加更多的配置和优化,如健康检查、熔断器、限流等。

五、小结


http://www.dtcms.com/a/601135.html

相关文章:

  • PySide6/PyQt Ctrl 滚轮 实现文本缩放功能
  • 南通企业网站建设网络营销培训哪个好点
  • Elasticsearch-4--倒排索引的原理?
  • 一个基于Python Streamlit sqlite3 的销售单管理系统,提供商品管理、客户管理、销售单管理及打印,和应收对账单等功能
  • 网站权重是什么华大基因背景调查
  • 华为桌面图标模糊问题解决方案
  • MotionTrans: 从人类VR数据学习机器人操作的运动级迁移
  • [Dify 实战案例] 构建一个 CV 内容优化与润色助手:让 AI 成为你的简历教练
  • 【计算思维】蓝桥杯STEMA 科技素养考试真题及解析 B
  • Kanass实战教程系列(3) - 项目经理如何使用kanass有效管理项目
  • 成都网站建设 网络公司建设工程中标查询网站
  • C语言编译程序属于应用软件 | 理解编译原理与应用场景
  • 蛋糕网站模板汕头网络营销公司
  • HOT100题打卡第37天——贪心算法
  • Python学习历程——模块
  • bin文件反编译C语言 | 深入探讨反编译技术及其应用
  • 测开学习DAY27
  • dede cms 网站模板云匠网怎么样
  • 信息学奥赛一本通 1625:【例 1】反素数 Antiprime | 洛谷 P1463 [POI 2001 R1 / HAOI2007] 反素数
  • 如何做网站长尾关键词布局工程公司取名字大全
  • 深度学习:从零开始手搓一个深层神经网络
  • Docker 多服务镜像构建完整教程
  • Docker 启动 EMQX 5.x 并配置自签名证书
  • 网站招工费怎么做会计分录小小视频免费观看高清
  • C++重点知识梳理(上)
  • 长沙市建设局官方网站wordpress 显示文章标签
  • 基于用户评论分析挖掘的旅游景点推荐系统
  • 宣传旅游网站建设的重点是什么装修公司哪家好排名
  • 【C语言学习笔记】动态内存分配:malloc/free的正确打开方式
  • HOVER:用于人形机器人的多功能全身神经控制器