springcloud 搭建 eureka 集群

概述

高可用集群配置、当注册中心扛不住高并发的时候,这时候 要用集群来扛;一个挂了还能有其他的eureka 来顶。

集群搭建

普通操作

1、这里我们先建3个 module microservice-eureka-server-2001microservice-eureka-server-2002microservice-eureka-server-2003
在这里插入图片描述
2、三个项目的pom.xml 文件都一样 这里就填一份 microservice-eureka-server-2001 的其他的只有对应的项目名称变了

<?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.cpc</groupId>
        <artifactId>cpc-spring-cloud</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>

        <!-- actuator监控引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

</project>

3、在每个项目的启动类上加 @EnableEurekaServer 这个注解
在这里插入图片描述
4、前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置
在这里插入图片描述
配置好了后执行下面这个命里来让配置生效(在命令行窗口执行)

// 删除DNS缓存内容,从而达到更新DNS目的 
ipconfig /flushdns

在这里插入图片描述

5、修改三个项目的application.yml文件,主要是修改 hostname和defaultZone,
2001修改:

server:
  port: 2001
  context-path: /

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

还有一点就是我们的被调用方服务,也就是客户端这边,原来是单机eureka所以它的配置文件中只配置了一个eureka的链接,现在是弄集群:
在这里插入图片描述

eureka:
  instance:
    #eureka客户端主机实例名称
    hostname: localhost
    #客户端服务名
    appname: microservice-student
    #客户端实例名称
    instance-id: microservice-student:1001
    #显示IP
    prefer-ip-address: true
  client:
    service-url:
      # 单机
      # defaultZone: http://localhost:2001/eureka   #把服务注册到eureka注册中心
      # 集群
      defaultZone: http://eureka2001.cpc.com:2001/eureka/,http://eureka2002.cpc.com:2002/eureka/,http://eureka2003.cpc.com:2003/eureka/

最后我们测试下:启动三个注册中心,以及服务提供者项目;
然后浏览器地址栏输入:

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

这里贴其中的一个页面:
在这里插入图片描述

骚操作

上面eureka服务搭建,除了yml文件不一样,其他文件都一样,那么我们有什么办法能够将多个eureka服务集合到一个工程中去呢?

创建一个microservice-eureka-server(三合一)子工程
在这里插入图片描述
pom.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.cpc</groupId>
        <artifactId>cpc-spring-cloud</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>

        <!-- actuator监控引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

</project>

Yml文件(关键)
这是利用springboot多配置的特点,在启动的时候激活指定的配置

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

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

Eureka自我保护机制

下面的红字出现的原因是因为eureka的自我保护机制,因为服务没有被调用的原因,要解决这个问题
在这里插入图片描述
某时刻某一个微服务不可用了,eureka不会立刻清理,依旧会对该微服务的信息进行保存
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
不推荐禁用.建议要更改也是改注销实例的时间:
eureka.instance.lease-expiration-duration-in-seconds

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值