SpringCloud Eureka服务注册与发现

1.Eureka服务端配置

  1. 新建module eureka-7001
  2. pom文件引入依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  1. yaml配置
server:
  port: 7001

spring:
  application:
    name: eureka-7001

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false    #自己就是注册中心,不用注册自己
    fetch-registry: false   #要不要去注册中心获取其他服务的地址
    service-url:
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机
    #defaultZone: http://eureka7002.com:7002/eureka/  #集群配置,多台后面以','号隔开往后追加,另外需要本地主机修改hosts主机文件做域名映射
  1. 启动类加@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain.class,args);
    }
}

2.Eureka客户端配置

  1. 在需要注册的module的pom文件引入依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. yaml配置
server:
  port: 8081
spring:
  application:
    name: cloud-client
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka  #集群环境需要往后追加其他地址
  instance:
    prefer-ip-address: true #首选IP地址。 默认false,也就是默认不注册ip
    instance-id: client8001 
  1. 启动类加@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient
public class ClientMain {
    public static void main(String[] args) {
        SpringApplication.run(ClientMain.class,args);
    }
}
  1. 访问http://localhost:7001/看到cloud-client已成功注册进eureka
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值