Eureka使用

单节点搭建

注意:在使用Eureka之前应将本机防火墙关闭。可在 控制面板\系统和安全\Windows 防火墙 中关闭。

在spring cloud中Eureka也是一个微服务,搭建过程和普通微服务开发过程相比更加简洁。

pom依赖

<!--Eureka服务端端依赖-->
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

yml配置

 hostname: 对应hosts文件中添加的值

server:
  port: 7001

eureka:
  instance:
    hostname: localhost
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己端就是注册中心
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
  server:
    #关闭自我保护机制
    enable-self-preservation: false
    #检测时间改为2000毫秒
    eviction-interval-timer-in-ms: 2000

主启动类

主启动类上添加@EnableEurekaServer注解

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

运行结果

补充:控制台参数说明:https://www.cnblogs.com/linjiqin/p/10087462.html

集群搭建

在Windows系统下需要修改hosts文件添加如下信息

修改yml配置文件

对hostname和defauktZone进行修改

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com
  #hostname: localhost
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己端就是注册中心
    fetch-registry: false
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
      defaultZone:  http://eureka7002.com:7002/eureka
  server:
    #关闭自我保护机制
    enable-self-preservation: false
    #检测时间改为2000毫秒
    eviction-interval-timer-in-ms: 2000

服务启动后:

微服务进驻Eureka

在需要进驻Eureka的微服务的pom依赖中添加Eureka客户端依赖

<!--Eureka客户端依赖-->
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

yml配置

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
      #defaultZone: http://localhost:7001/eureka,http://eureka7002.com:7002/eureka

主启动类

添加@EnableEurekaClient注解

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableEurekaClient
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值