Spring Cloud Eureka
单机搭建集群
为了区分hostname,在C:\Windows\System32\drivers\etc\hosts新增3个主机名:
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
Eureka注册中心集群
- 新建3个集群节点服务
cloud-eureka7001
cloud-eureka7002
cloud-eureka7003
- 分别引入依赖
<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>
- cloud-eureka7001节点yml配置
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com
server:
# 关闭自我保护
enable-self-preservation: false
eviction-interval-timer-in-ms: 60000
client:
# 不向注册中心注册自己
register-with-eureka: false
# 不去注册中心检索服务
fetch-registry: false
service-url:
# 集群其它eureka成员地址,多个用英文逗号分隔
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- cloud-eureka7002节点yml配置
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com
server:
# 关闭自我保护
enable-self-preservation: false
eviction-interval-timer-in-ms: 60000
client:
# 不向注册中心注册自己
register-with-eureka: false
# 不去注册中心检索服务
fetch-registry: false
service-url:
# 集群其它eureka成员地址,多个用英文逗号分隔
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
- cloud-eureka7003节点yml配置
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com
server:
# 关闭自我保护
enable-self-preservation: false
eviction-interval-timer-in-ms: 60000
client:
# 不向注册中心注册自己
register-with-eureka: false
# 不去注册中心检索服务
fetch-registry: false
service-url:
# 集群其它eureka成员地址,多个用英文逗号分隔
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
- 启动类分别增加 @EnableEurekaServer 注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Eureka7001Application {
public static void main(String[] args) {
SpringApplication.run(Eureka7001Application.class, args);
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Eureka7002Application {
public static void main(String[] args) {
SpringApplication.run(Eureka7002Application.class, args);
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Eureka7003Application {
public static void main(String[] args) {
SpringApplication.run(Eureka7003Application.class, args);
}
}
- 启动cloud-eureka7001、cloud-eureka7002、cloud-eureka7003
分别访问如下地址可查看集群信息
http://eureka7001.com:7001/ 或 http://localhost:7001/
http://eureka7002.com:7002/ 或 http://localhost:7002/
http://eureka7003.com:7003/ 或 http://localhost:7003/
Eureka客户端cloud-eureka-order服务
- 引入依赖
<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>
- 客户端yml配置
server:
port: 9001
spring:
application:
name: cloud-eureka-order
eureka:
instance:
prefer-ip-address: true
client:
# 向注册中心注册自己
register-with-eureka: true
# 去注册中心检索服务
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- 启动类增加 @EnableEurekaClient 或 @EnableDiscoveryClient 注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
// @EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}