Eureka单机&集群配置

Eureka是什么

Eureka是基于REST(Representational State Transfer)服务,提供服务发现并实现负载均衡和故障转移。

自我保护机制

  1. 开启保护机制后,注册的进Eureka的服务就不保证100%可用,此时我们可以使用eureka.server.enable-self-preservation=false来关闭保护机制,这样可以确保注册中心中不可用的实例被及时的删除

  2. 如果在15分钟内超过85%的客户端节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,Eureka Server自动进入自我保护机制,此时会出现以下几种情况:

    · Eureka Server不再从注册列表中移除因为长时间没收到心跳而应该过期的服务。

    · Eureka Server仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上,保证当前节点依然可用。

    · 当网络稳定时,当前Eureka Server新的注册信息会被同步到其它节点中。

版本选择

官网最新版。。。(2020.10.16日截图)

下图查看地址

image-20201016104925038

image-20201016105049021

服务搭建

创建项目

IDEA创建,jdk8,其他步骤省略

注意:2.2.5.RELEASE官方推荐使用SpringBoot版本为2.3.3

下图查看地址

image-20201016115950155

导入GAV坐标

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.2.5.RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId> <!--spring-boot-starter-web包中自带javax.servlet,防止冲突,导入时排除-->
                </exclusion>
            </exclusions>
        </dependency>

application启动类添加注解

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

}

配置yml

server:
  port: 8761

eureka:
  instance:
    hostname: 192.168.1.2 #可以是任意名字,方便区分
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #表示自己端就是注册中心,并不需要拉取里面注册了哪些服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 

启动项目

访问 localhost:8761,如正常会显示如下页面,至此,注册中心单机版搭建成功

image-20201016110243972

集群配置

修改上面的yml

server:
  port: 8761
eureka:
  instance:
    hostname: 192.168.1.2 #可以是任意名字,方便区分
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #表示自己端就是注册中心,并不需要拉取里面注册了哪些服务
    service-url:
      defaultZone: http://192.168.1.3:${server.port}/eureka/,http://192.168.1.4:${server.port}/eureka/  #(修改点)其他Eureka服务地址

打jar包到另外一台电脑OR虚拟机

注:如果本机跑两个,需修改端口

修改yml文件中eureka.instance.hostname及defaultZone后启动,分别访问对应服务的地址192.168.1.2:8761、192.168.1.3:8761。。。

看到如下页面后证明成功

image-20201016113834244

1.所写技术都是我工作中用到的
2.所写技术都是自己从项目中提取的
3.所有配置搭建流程都经过2到3遍的测试
4.因都是工作中使用的技术,所以不确定是否有转载的,如果有,请及时通知
原文地址:https://www.cnblogs.com/rb2010/p/13879195.html