【Spring Cloud】Eureka服务注册中心搭建
简介
Eureka服务器用作服务注册服务器。
Eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。
Eureka特点
- servlet 应用 Jersey 框架实现自身的 RESTful HTTP接口
- 服务的注册通过 HTTP 协议实现 通过 JDK
- 自带的 Timer 实现定时任务:心跳、定时清理过期服务、节点同步
- 使用Google的guava包实现内存缓存
Eureka客户端
- 服务启动时,向注册中心注册服务,同时从一个服务注册服务中查询所有可用服务实例的库,并缓存到本地;
- 用来简化与服务器的交互、作为轮询负载均衡器,并提供故障切换支持;
- 内置使用轮询负载均衡算法;默认发送心跳的周期是30s;当一个服务器不可用,需要3个心跳才能让服务器和客户端的元数据相同。
eureka.instance.leaseRenewalIntervalInSeconds
eureka.instance.leaseExpirationDurationInSeconds
Eureka服务端:
- 服务注册中心;
- 为服务实例注册管理和查询可用实例提供了REST API;
架构图
配置maven父项目的pom
<groupId>com.xfdmao</groupId>
<artifactId>fcat-springcloud</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>fcat-center</module>
<module>fcat-user</module>
</modules>
<name>fcat-springcloud</name>
<description>fcat-spring cloud project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
Eureka服务注册中心——服务端
新建项目模块:fcat-center
pom引用jar包
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
创建注册中心启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class CenterApplication {
public static void main(String[] args) {
SpringApplication.run(CenterApplication.class,args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
application.yml配置文件中配置
spring:
application:
name: fcat-center
server:
port: 8760
eureka:
client:
register-with-eureka: false # 实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true
fetch-registry: false # 此客户端是否获取eureka服务器注册表上的注册信息,默认为true
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
Eureka客户端
新建项目模块:fcat-user
pom引用相应jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
- 1
- 2
- 3
- 4
新建用户模块启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
新建controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping(value = "",method = RequestMethod.GET)
public String home(){
return "FCat User";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
application.yml 配置文件
spring:
application:
name: fcat-user
server:
port: 8761
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8760/eureka/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
启动
main方法一起启动:CenterApplication、UserApplication
访问
服务注册中心: http://localhost:8960
用户模块: http://localhost:8961
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统