SpringCloud(一):Eureka服务注册与发现

Eureka简介

spring-cloud-netflix-eureka官网

在服务治理框架中,通常都会构建一个注册中心,每个微服务向注册中心登记自己提供的服务,微服务也可以通过注册中心进行远程服务调用。注册中心按照服务名分类组织服务清单,同时还需要以心跳检测的方式去监测清单中的服务是否可用,若不可用需要从服务清单中剔除,以达到排除故障服务的效果。

Eureka是Spring Cloud Netflix下面一个子组件,主要用于微服务架构中的服务注册和发现。Eureka特点如下:

  • 基于CAP理论中AP思想进行设计,即牺牲了数据一致性(C)保证了高可用性(A)和分区容错性(P)。
  • 基于C/S架构,提供两个组件Eureka ServerEureka Client,Eureka Server 提供服务注册,Eureka Client 是一个Java客户端,需要接入微服务中,启动后注册到注册中心,纳入服务管理。
  • Eureka 没有区分中心节点和从节点,集群中每个节点地位相等,节点之间通过相互注册和相互守望。
  • 如果某个服务实例在一定时间范围内没有心跳了,Eureka 会将其移除;但是如果短时间内丢失大量服务实例 Eureka 会认为发生网络故障,并不会移除这些实例,这就是自我保护机制

使用 Eureka 的微服务架构如下

架构
多个微服务提供者实例向多个Eureka注册中心中注册服务,消费者根据服务名称调用服务提供者的某一个实例。

在新版本Spring Cloud中Eureka已经停止维护。

搭建Eureka注册中心(单机版)

使用IDEA工具创建一个空白Maven项目作为父项目,进行统一版本管理。
创建一个空白maven项目
该空白项目只用于依赖管理,只保留pom.xml即可。
修改目录结构
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>

    <groupId>com.pickup</groupId>
    <artifactId>cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- packaing方式改为pom -->
    <packaging>pom</packaging>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <mysql.version>5.1.49</mysql.version>
        <mybatis.spring.boot.version>2.2.2</mybatis.spring.boot.version>
    </properties>

    <!-- 子模块继承之后,提供作用:锁定版本+子module不用写groupId和version -->
    <!-- dependencyManagement标签用于依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <!--spring boot 2.7.4-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.7.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud 2021.0.4-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud alibaba 2021.0.4.0-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.4.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
			<!-- mysql驱动,适用于5版本 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
			<!-- druid连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>
			<!-- mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>

                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在该项目中创建一个cloud-eureka-server7001模块,搭建 Eureka Server 端。在pom中引入相关依赖:

	<dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        
        <!--spring boot web actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

创建配置文件application.yml,添加Eureka注册中心配置。

server:
  port: 7001 # 服务端口号

eureka:
  instance:
    hostname: localhost # 指定主机地址
  client:
    register-with-eureka: false     # false表示不向注册中心注册自己。
    fetch-registry: false     # false表示自己端就是注册中心,并不需要去检索服务
    service-url:
      # 设置Eureka Server交互的地址查询服务和注册服务地址
      # 单机注册自己
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
#     关闭自我保护机制,保证不可用服务被及时踢除
    enable-self-preservation: false

在主启动类中加入@EnableEurekaServer注解,开启注册中心功能。

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaMainApplication {

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

运行主启动类,访问地址http://localhost:7001/可以看到注册中心界面。

Eureka服务中心

搭建Eureka Client

在该项目中创建一个cloud-provider-payment8001模块,搭建 Eureka Client,在pom中添加如下依赖

<dependencies>
	<!--eureka-client-->
    <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-actuator</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
</dependencies>

在配置文件application.yml中添加如下配置

server:
  port: 8001 # 运行端口号

spring:
  application:
    name: cloud-payment-service # 服务名称

eureka:
  client:
    register-with-eureka: true # 注册到服务中心
    fetch-registry: true # 获取注册服务列表
    service-url:
      # 设置Eureka Server交互的地址查询服务和注册服务地址
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: payment8001 # 注册实例id
    prefer-ip-address: true # 显示IP地址

在主启动类上添加@EnableEurekaClient注解表明是 Eureka Client。

@SpringBootApplication
@EnableEurekaClient
public class PaymentMainApplication {

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

先启动 Eureka Server,然后启动 Eureka Client,访问注册中心http://localhost:7001/

注册
最下面Application中显示了注册的服务和对应实例的一些信息。

搭建Eureka注册中心(集群版)

所有服务都会注册到服务中心,也会从服务中心获取服务列表来调用服务。因此如果只有一个服务中心,一旦发生宕机,所有服务调度都会发生问题。

由于配置文件中defaultZone使用了域名,如果用单机搭建双节点集群需要修改本机host文件,实现多个域名映射。

127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com

可以通过增添配置文件,进而使用多个配置文件启动同一个Spring Boot项目,也可以直接新建一个项目。这里选择新建一个cloud-eureka-server7002模块,过程与上述cloud-eureka-server7001模块相同。

创建好模块后修改两个模块配置文件application.yml如下

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com # eureka服务端的实例名称
  client:
    register-with-eureka: false     # false表示不向注册中心注册自己。
    fetch-registry: false     # false表示自己端就是注册中心,并不需要去检索服务
    service-url:
      # 设置Eureka Server交互的地址查询服务和注册服务地址
      # 集群相互注册
      defaultZone: http://eureka7002.com:7002/eureka/
  server:
#     关闭自我保护机制,保证不可用服务被及时踢除
    enable-self-preservation: false
server:
  port: 7002 # 不同项目端口号不同

eureka:
  instance:
    hostname: eureka7002.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,并不需要去检索服务
    service-url:
      #设置Eureka Server交互的地址查询服务和注册服务地址
      defaultZone: http://eureka7001.com:7001/eureka/ # 相互注册
    server:
    #关闭自我保护机制,保证不可用服务被及时踢除
    enable-self-preservation: false

启动两个服务注册中心,访问地址http://localhost:7001/或者http://localhost:7001/都可以看到注册中心界面。

相互注册
http://localhost:7002/中可以看到DS Replicas中出现另一个注册中心。

修改 Eureka Client配置文件,使其注册到集群中。

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # 设置Eureka Server交互的地址查询服务和注册服务地址
      defaultZone: http://localhost:7001/eureka/, http://localhost:7002/eureka/
  instance:
    instance-id: payment8001
    # 显示IP地址
    prefer-ip-address: true

启动后可以看到任意注册中心都能查询到cloud-payment-service。

集群注册

Eureka常用配置

eureka:
  client: # eureka客户端配置
    register-with-eureka: true # 是否将自己注册到eureka服务端上去
    fetch-registry: true # 是否获取eureka服务端上注册的服务列表
    service-url:
      defaultZone: http://localhost:7001/eureka/ # 指定注册中心地址
    enabled: true # 启用eureka客户端
    registry-fetch-interval-seconds: 30 # 定义去eureka服务端获取服务列表的时间间隔
  instance: # eureka客户端实例配置
    lease-renewal-interval-in-seconds: 30 # 定义服务多久去注册中心续约
    lease-expiration-duration-in-seconds: 90 # 定义服务多久不去续约认为服务失效
    metadata-map:
      zone: jiangsu # 所在区域
    hostname: localhost # 服务主机名称
    prefer-ip-address: false # 是否优先使用ip来作为主机名
  server: # eureka服务端配置
    enable-self-preservation: false # 关闭eureka服务端的保护机制
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值