【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);
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值