首先是Eureka服务注册中心EurekaServer的创建
1. 引入依赖
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2. 配置 yml
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #应用实例主机名
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #我就是注册中心,我的职责就是维护服务实例。false表示并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ # 单机模式指向自己
修改电脑上的hosts文件添加映射
127.0.0.1 eureka7001.com
window10的host文件在 C:\Windows\System32\drivers\etc
3. Springboot主启动类
添加注解@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer // @EurekaServer注解标识该应用为EurekaServer端
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
启动应用访问http://localhost:7001/
。如下界面,EurekaServer创建成功
Eureka客户端EurekaClient的创建
1. 引入依赖
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. 编写yml
server:
port: 8001
spring:
application:
name: cloud-payment-service #应用名称,服务名称
eureka:
client:
register-with-eureka: true # 是否将自己注册进EurekaServer,默认true
fetch-registry: true # 是否抓取已有注册信息,默认true
service-url:
defaultZone: http://localhost:7001/eureka # 单机版
instance:
instance-id: payment8001 # 实例名称
prefer-ip-address: true # 访问路径显示
3. 主启动类
添加注解@EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient // @EurekaClient标识该应用为EurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
启动该应用,访问http://localhost:7001/
。可以看到当前服务成功注册进Eureka服务注册中心
EurekaServer集群
上面我们只配置了一个端口号为7001的EurekaServer,现在我们再配置一个端口号为7002的EurekaServer组成集群。
创建一个新的Module
1. 引入依赖
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2. 配置 yml
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
#集群指向其它eureka
defaultZone: http://eureka7001.com:7001/eureka/
修改电脑上的hosts文件添加映射
127.0.0.1 eureka7002.com
3. Springboot主启动类
添加注解@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer // EurekaServer
public class EurekaMain7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7002.class, args);
}
}
4. 修改端口号为7001的EurekaServer配置
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
#集群指向其它eureka
defaultZone: http://eureka7002.com:7002/eureka/
5. 修改EurekaClient配置
defaultZone指向两台EurekaServer
server:
port: 8001
spring:
application:
name: cloud-payment-service #应用名称,服务名称
eureka:
client:
register-with-eureka: true # 是否将自己注册进EurekaServer,默认true
fetch-registry: true # 是否抓取已有注册信息,默认true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001 # 实例名称
prefer-ip-address: true # 访问路径显示
启动所有应用,访问http://localhost:7001/
,集群搭建完毕