目录
-
- Eureka的功能
-
- 1.1 客户端与服务端
- 1.2 客户端与客户端
- 1.3 服务端与服务端
- 1.4 服务端内部
-
- HelloWorld入门
-
- 2.1 服务端
-
- 2.1.1 依赖
- 2.1.2 配置
- 2.1.3 @EnableEurekaServer
- 2.2 客户端
-
- 2.2.1 依赖
- 2.2.2 配置
1. Eureka的功能
1.1 客户端与服务端
1、 客户端向服务端注册register(第4节);
2、 客户端向服务端续约heartbeat(第5节);
3、 客户端下线cancel(第6节);
4、 客户端向服务端获取注册信息(即注册在server的Apps)(cashRefresh)(第7节);
1.2 客户端与客户端
1、 客户端调用其他客户端的功能(LoadBalance);
1.3 服务端与服务端
1、 信息同步(replicate)(第4.7节);
1.4 服务端内部
1、 服务剔除(第3.2.5.2节);
2. HelloWorld入门
开始之前先看下eureka相关的配置文件
这些配置文件对应yml文件中对Eureka的配置,在实际使用中如果对某些参数不太了解其用途可以查看这些类
2.1 服务端
2.1.1 依赖
pom.xml文件添加下面两个依赖
<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-server</artifactId>
</dependency>
2.1.2 配置
application.yml添加配置
spring:
application:
name: my-eureka-server
server:
port: 3333
2.1.3 @EnableEurekaServer
@SpringBootApplication
// 声明这是一个Eureka Server
@EnableEurekaServer
public class EurekaServeApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServeApplication.class, args);
}
}
简简单单,一个Eureka Server就完成了,启动后浏览器http://localhost:3333/
就可以打开服务的dashboard
2.2 客户端
2.2.1 依赖
<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>
2.2.2 配置
spring:
application:
name: my-eureka-client
server:
port: 2222
eureka:
client:
service-url:
// 服务端地址
defaultZone: http://localhost:3333/eureka/
如此简单,一个Eureka Client就搭建好了,启动后稍等半分钟,再刷新服务端http://localhost:3333/
就可以看到客户端信息了
证明客户端已经向服务端注册,说明我们搭建的服务端和客户端都是OK的,即便甚是简陋,但是对于源码的研究足够了。
未完待续,欢迎关注公众号三横兰
版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: