什么是Eureka
Eureka是Netfix的一个子模块,也是核心模块之一。Eureka是一个基于REST风格的服务,用于定位服务,已实现云端服务发现和故障转移。
有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件
三大角色
- Eureka Server:提供服务的注册与发现
- Service Provider:服务提供者,将服务注册到Eureka中,供消费者消费
- Service Consumer:服务消费者,从Eureka获取注册服务列表,进行服务消费
构建Eureka服务
Eureka服务端 - 单机
- 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
- 配置
server:
port: 7001
eureka:
instance:
hostname: localhost # Eureka服务端的实例名称
client:
register-with-eureka: false # 是否向Eureka服务注册中心注册自己
fetch-registry: false # false,表示自己就是注册中心
service-url:
# 单机环境下,只需要注入自己的地址即可
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
- 启动
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// 服务启动类,启动Eureka服务
@EnableEurekaServer
public class EurekaServer_8002 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_8002.class, args);
}
}
Eureka服务提供方
- 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- 可有可无,主要用于对服务配置相关描述信息 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 配置
# eureka配置,
eureka:
client:
service-url:
# 服务注册中心地址 单机版
defaultZone: http://localhost:8002/eureka
instance:
# 修改Eureka上的默认描述信息
instance-id: springcloud-provider-dept-8001
# actuator info配置
info:
app.name: doudou-springcloud
company.name: blog.doudou
- 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class, args);
}
}
集群 版
- 集群中每个Eureka服务对应的依赖和单机版保持一致
- 配置
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com # Eureka服务端的实例名称
client:
register-with-eureka: false # 是否向Eureka服务注册中心注册自己
fetch-registry: false # false,表示自己就是注册中心
service-url:
# 这里只需要注册集群中的其它节点即可
defaultZone: http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com # Eureka服务端的实例名称
client:
register-with-eureka: false # 是否向Eureka服务注册中心注册自己
fetch-registry: false # false,表示自己就是注册中心
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7003.com:7003/eureka
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com # Eureka服务端的实例名称
client:
register-with-eureka: false # 是否向Eureka服务注册中心注册自己
fetch-registry: false # false,表示自己就是注册中心
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
- 每个服务的启动类和单机版的保持一致
eureka服务提供方
- 依赖与单机版保持一致
- 配置
# eureka配置,
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: springcloud-provider-dept-8001 # 修改Eureka上的默认描述信息
# actuator info配置
info:
app.name: doudou-springcloud
company.name: blog.doudou
- 启动类 与单机版保持一致即可