简介
Eureka是由Netflix开源的一款基于REST的服务发现组件,包括Eureka Server和Eureka Client。
2.1.1 服务发现由来
- 单体架构:少数依赖外部服务,采用配置域名的方式访问
- SOA架构:服务方提供内部域名,配置Nginx实现负载均衡
- 微服务架构(使用docker,ip不固定)
- 方案一:Nginx+手工或者通过脚本的方式动态更新Nginx的配置文件
- 方案二:将服务注册中心作为一个标配的分布式服务组件
2.1.3 服务发现技术选型
2.2 Spring Cloud Eureka入门案例
1.父工程 pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.创建Eureka Server工程模块
- pom.xml 引入eureka server组件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 启动主类添加 @EnableEurekaServer注解
- application.yml 配置文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
3.创建Eureka Client工程模块
- pom.xml引入eureka client组件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 启动类添加 @EnableDiscoveryClient注解
- application.yml 配置文件
server:
port: 8081
spring:
application:
name: demo-client1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
4.依次启动 Eureka Server、Eureka Client工程
- 访问 http://localhost:8761 ,会看到 client已经注册成功
2.3 Eureka Server的REST API简介
- 允许非Java语言的其他应用服务通过 HTTP REST的方式接入Eureka的服务发现中
2.3.1 REST API列表