eureka集群

1、Eureka集群搭建

高可用集群配置
当注册中心扛不住高并发的时候,这时候 我们就需要用集群来抗

在昨天我们只创建了一个今天我们就多创建两个
microservice-eureka-server-2002 microservice-eureka-server-2003
首先我们添加一下pop.xml这几个依赖都是一样的,所以粘贴一个

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xyx</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-eureka-server-2001</artifactId>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--  修改后立即生效,热部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>




如果说需要用到集群

在每个项目的启动类上加 @EnableEurekaServer 这个注解
package com.xyx.microserviceeurekaserver2001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServer2001Application {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceEurekaServer2001Application.class, args);
    }

}

前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,如果搞三个虚拟机的话会有点麻烦,在这里 直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置加上

127.0.0.1  eureka2001.xyx.com
127.0.0.1  eureka2002.xyx.com
127.0.0.1  eureka2003.xyx.com

在这里插入图片描述
如果想要集群,就必须在以下配置除自己意外的其他域名
修改三个项目的application.yml文件,主要是修改 hostname和defaultZone,
2001

server:
  port: 2001
  context-path: /

eureka:
  instance:
    # 单机 hostname: localhost #eureka注册中心实例名称
    hostname: eureka2001.xyx.com # 集群
  client:
    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
    fetch-registry: false     #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
    service-url:
      defaultZone: http://eureka2002.xyx.com:2002/eureka/,http://eureka2003.xyx.com:2003/eureka/ # 集群
      #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

2002

server:
  port: 2002
  context-path: /

eureka:
  instance:
    # 单机 hostname: localhost #eureka注册中心实例名称
    hostname: eureka2002.xyx.com # 集群
  client:
    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
    fetch-registry: false     #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
    service-url:
      defaultZone: http://eureka2001.xyx.com:2001/eureka/,http://eureka2003.xyx.com:2003/eureka/ # 集群
      #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

2003

server:
  port: 2003
  context-path: /

eureka:
  instance:
    # 单机 hostname: localhost #eureka注册中心实例名称
    hostname: eureka2003.xyx.com # 集群
  client:
    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
    fetch-registry: false     #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
    service-url:
      defaultZone: http://eureka2001.xyx.com:2001/eureka/,http://eureka2002.xyx.com:2002/eureka/ # 集群
      #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

路径

http://eureka2001.xyx.com:2001/
http://eureka2002.xyx.com:2002/
http://eureka2003.xyx.com:2003/

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
骚操作
上面eureka服务搭建,除了yml文件不一样,其他文件都一样,那么我们有什么办法能够将多个eureka服务集合到一个工程中去呢?
创建一个microservice-eureka-server(三合一)子工程
Pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xyx</groupId>
        <artifactId>t224springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-eureka-server</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--  修改后立即生效,热部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

yml文件

---
server:
  port: 2001
  context-path: /
eureka:
  instance:
    hostname: eureka2001.xyx.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2002.xyx.com:2002/eureka/,http://eureka2003.xyx.com:2003/eureka/
spring:
  profiles: eureka2001
---
server:
  port: 2002
  context-path: /
eureka:
  instance:
    hostname: eureka2002.xyx.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2001.xyx.com:2001/eureka/,http://eureka2003.xyx.com:2003/eureka/
spring:
  profiles: eureka2002
---
server:
  port: 2003
  context-path: /
eureka:
  instance:
    hostname: eureka2003.xyx.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2001.xyx.com:2001/eureka/,http://eureka2002.xyx.com:2002/eureka/
spring:
  profiles: eureka2003


路径

spring:
  profiles: eureka2001

MicroserviceEurekaServerApplicationTests

package com.xyx.microserviceeurekaserver;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MicroserviceEurekaServerApplicationTests {

    @Test
    void contextLoads() {
    }

}

在这里插入图片描述
跑起来的效果跟普通操作的效果是一致的;

2、Eureka自我保护机制

在这里插入图片描述
Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,但是在保护期内如果服务刚好这个服务提供者非正常下线了,此时服务消费者就会拿到一个无效的服务实例,此时会调用失败,对于这个问题需要服务消费者端要有一些容错机制,如重试,断路器等。

我们在单机测试的时候很容易满足心跳失败比例在 15 分钟之内低于 85%,这个时候就会触发 Eureka 的保护机制,一旦开启了保护机制,则服务注册中心维护的服务实例就不是那么准确了,此时我们可以使用eureka.server.enable-self-preservation=false来关闭保护机制,这样可以确保注册中心中不可用的实例被及时的剔除(不推荐)。

开发环境,我们经常会遇到这个红色的警告;
当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;

默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。

由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;
通过“自我保护模式”,使Eureka集群更加的健壮和稳定;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值