Spring Cloud 配置Eureka Server高可用集群之吊打拦路虎

Spring Cloud 配置Eureka Server高可用集群之吊打拦路虎

源码地址:https://download.youkuaiyun.com/download/qq_33624284/10923226

案例:工具采用 idea + 项目结构采用maven多module的结构 + spring boot 版本为 2.1.2

  1. 创建空模板Maven项目 eureka
    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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ztw</groupId>
    <artifactId>eureka</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 父包pom -->
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--聚合子项目名 必须要,不然子模板没法依赖-->
    <modules>
        <module>eureka-server</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!-- spring boot 版本1.5.*以下用 Dalston.SR1 其它查看spring cloud版本-->
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
  1. 在eureka下创建module eureka-server,目录结构如下:
    eureka 目录结构
    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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ztw</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <!-- 父引用 -->
    <parent>
        <groupId>com.ztw</groupId>
        <artifactId>eureka</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!-- 2.0.* 版本以前 spring-cloud-starter-eureka-server-->
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

application.yml配置:

server:
  port: 4001
eureka:
  instance:
    hostname: localhost
  client:
    ###因为是单个服务,不需要注册
    #fetch-registry: false
    #register-with-eureka: false
    fetch-registry: false
    register-with-eureka: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

EurekaServerApplication.class:

package com.ztw;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

http://localhost:4001/ 圈起来的为空,是因为我们还没有客户注册
在这里插入图片描述
4. 在eureka下创建module eureka-client,目录结构如下:
在这里插入图片描述
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ztw</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <!-- 父引用 -->
    <parent>
        <groupId>com.ztw</groupId>
        <artifactId>eureka</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

application.yml配置:

server:
  port: 4002
eureka:
  instance:
    hostname: localhost
  client:
    ###需要注册
    #fetch-registry: false
    #register-with-eureka: false
    serviceUrl:
      ###服务地址,注册
      defaultZone: http://${eureka.instance.hostname}:4001/eureka/
spring:
  application:
    name: eureka-client

EurekaClientApplication.class:

package com.ztw;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

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

http://localhost:4001/ 圈起来的地方已经有eureka-client 证明注册成功了
在这里插入图片描述
拓展:

eureka:
  instance:
    ###续约心跳时间30秒
    #    lease-renewal-interval-in-seconds: 30
    ###服务剔除时间90秒
    #    lease-expiration-duration-in-seconds: 90
    
    server:
    ###响应缓存30秒,不会立即注册
    #    response-cache-update-interval-ms: 30
    ###Eureka自我保护,
    #Eureka Serve接收到的服务续约低于为该值配置的百分比(默认为 15 分钟 内低于 85% ),
    #则服务器开启自我保护模式,即不再剔除注册列表的信息。
    #    enable-self-preservation: false
  1. 配置eureka 高可用集群(两个为例)
    hosts文件配置:
C:\Windows\System32\drivers\etc
hosts
127.0.0.1       peer7
127.0.0.1       peer8

eureka-server application.yml文件配置:

---
spring:
  profiles: peer7
server:
  port: 4007
eureka:
  instance:
  	###实际应用配服务地址,这里为本地开发环境,已"peer7"参数直观代替
    hostname: peer7
  client:
    ###因为是多个服务集群,需要注册
    #fetch-registry: false
    #register-with-eureka: false
    serviceUrl:
      defaultZone: http://peer8:4008/eureka/

---
spring:
  profiles: peer8
server:
  port: 4008
eureka:
  instance:
    hostname: peer8
  client:
    serviceUrl:
      defaultZone: http://peer7:4007/eureka/

eureka-client application.yml文件配置:

server:
  port: 4002
eureka:
  instance:
    hostname: localhost
  client:
    ###需要注册
    #fetch-registry: false
    #register-with-eureka: false
    serviceUrl:
      ###服务地址,向一个服务注册
      defaultZone: http://${eureka.instance.hostname}:4007/eureka/
spring:
  application:
    name: eureka-client
  1. 分别启动对应服务(idea)两次服务启动,需要配置步骤如下:
    在这里插入图片描述
    eureka-client 选择main方法启动即可。
  2. 验证结果
    http://peer7:4007/
    在这里插入图片描述
    http://peer8:4008/
    在这里插入图片描述

通过访问两个eureka 服务,得到界面的结果来看,都有我们的eureka-client,而我们在client只向peer7:4007/这个服务中注册过,结论明显:两个服务共享了,实现了eureka server集群。

以上是详细的eureka 配置过程,(eclipse 的搭建未写,可以自己尝试)搭建项目过程中报错的过程都省略了,对应的解决方法都在上面,可以自己动手配置,一步一步走,遇到问题对照博文修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值