服务注册中心 Eureka 使用步骤

首先是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/,集群搭建完毕
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值