SpringCloud微服务之二:Eureka服务注册与发现中心实战

SpringCloud微服务之二:服务注册与发现实战

前言

使用版本:
springboot版本:1.5.9.RELEASE;
springcloud版本:Edgware.RELEASE;
开发工具:IDEA
主要介绍Eureka服务端配置、Eureka客户端调用、Eureka高可用集群配置、Eureka用户认证、Eureka元数据、Eureka自我保护模式以及部署服务多网卡ip时的选择。

一、Eureka是什么?

Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。
注册中心主要提供三个核心功能:
1.服务注册
服务提供者启动时,会通过 Eureka Client 向 Eureka Server 注册信息,Eureka Server 会存储该服务的信息,Eureka Server 内部有二层缓存机制来维护整个注册表
2. 提供注册表
服务消费者在调用服务时,如果 Eureka Client 没有缓存注册表的话,会从 Eureka Server 获取最新的注册表
3. 同步状态
Eureka Client 通过注册、心跳机制和 Eureka Server 同步当前客户端的状态

在这里插入图片描述

三大角色
Eureka Server:提供服务的注册和发现
Server Provider:将自身服务注册到Eureka中,从而使消费方能够找到
Server Consumer:服务消费方从Eureka中获取注册服务列表,从而找到消费服务

二、Eureka服务端

1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.yangxf.cloud</groupId>
  <artifactId>microservice-discovery-eureka</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <!-- 引入spring boot的依赖 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

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

  <!-- 引入spring cloud的依赖 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <!-- 添加spring-boot的maven插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

2、配置添加

server:
  port: 8761                    # 指定该Eureka实例的端口
eureka:
  client:
    registerWithEureka: false #是否默认将当前服务注册到Eureka注册汇总新
    fetchRegistry: false #是否从Eureka服务上获取注册信息
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ #客户端注册配置地址,多个之间通逗号分隔

3、启动项添加注解

/**
 * 使用Eureka做服务发现.
 * @author linwd
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaApplication.class, args);
  }
}

三、Eureka客户端

1、pom依赖引入

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

2、注解添加

在启动类上添加注解@EnableDiscoveryClient 或@EnableEurekaClient
Spring Cloud Edgware以及更高版本只需要引入依赖即可。

//@EnableDiscoveryClient(autoRegister = false)//默认true,如改成false,就不会注册服务啦
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderUserApplication {
  public static void main(String[] args) {
    SpringApplication.run(ProviderUserApplication.class, args);
  }
}

3、配置添加

applyication.yml添加如下配置:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}

在这里插入图片描述

四、Eureka高可用集群

1、Eureka集群配置

准备两个节点(ip1,ip2)服务,添加如下配置:

eureka:
  client:
    serviceUrl:
      defaultZone: http://ip1:8761/eureka/,http://ip2:8762/eureka/

2、客户端调用配置

这样就服务注册到Eureka集群上了,

eureka:
  client:
    serviceUrl:
      defaultZone: http://ip1:8761/eureka/,http://ip2:8762/eureka/

五、Eureka用户认证

实际项目中可能出于安全考虑,必须经过用户认证才允许访问Eureka

1、服务端添加用户认证

  • 引入依赖
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  • 添加配置
security:
  basic:
    enabled: true               # 开启基于HTTP basic的认证
  user:
    name: yangxf                  # 配置登录的账号是yangxf
    password: 123456       # 配置登录的密码是123456
server:
  port: 8761                    # 指定该Eureka实例的端口
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://yangxf:123456@localhost:8761/eureka/

2、客户端调整

配置调整为:http://yangxf:123456@localhost:8761/eureka/

eureka:
  client:
    serviceUrl:
      defaultZone: http://yangxf:123456@localhost:8761/eureka/

六、Eureka元数据

元数据分为标准元数据和自定义元数据;
标准元数据包括主机名,ip地址,端口,状态页,健康检查等信息;
自定义元数据放在eureka.instance.metadata-map下,任意key-value;
元数据数据可以用于服务之间获取并调用。

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    metadata-map:
      my-metadata: 我自定义的元数据       # 自定义的元数据,key/value都可以随便写。

可以通过springCloud自带的DiscoveryClient完成信息获取。

package com.yangxf.cloud.study.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yangxf.cloud.study.user.entity.User;

@RestController
public class MovieController {
  @Autowired
  private DiscoveryClient discoveryClient;

  /**
   * 查询microservice-provider-user服务的信息并返回
   * @return microservice-provider-user服务的信息
   */
  @GetMapping("/user-instance")
  public List<ServiceInstance> showInfo() {
    return this.discoveryClient.getInstances("microservice-provider-user");
  }
}

七、Eureka的自我保护模式

出现如下红色文字,即表示Eureka进入了自我保护模式;
在这里插入图片描述

1、解决方式

出现这情况,解决这种情况的方法主要有几种方式:

  1. 等待 Eureka Server 自动恢复
    正常的情况下,等待网络恢复(或者没有频繁的启动与关闭实例)后,等待一段时间 Eureka Server 会自动关闭自我保护模式,但是如果它迟迟没有关闭该模式,那么便可以尝试手动关闭。
  2. 重启 Eureka Server
    通常而言,PRD 环境建议对 Eureka Server 做负载均衡,这样在依次关闭并开启 Eureka Server 后,无效的实例会被清除,并且不会对正常的使用照成影响。
  3. 关闭 Eureka 的自我保护模式
    eureka.server.enable-self-preservation=false,测试可以通过这个配置关闭

2、原理

默认情况下,如果 Eureka Server 在一定时间内没有收到某个微服务实例的心跳,Eureka Server 便会将该实例注销。 (默认是90s),
但是 当网络分区发生故障(延迟、卡顿、拥挤)时,微服务与 Eureka Server 之间是无法正常通信的,在这种情况下微服务本身其实是健康的,本来是不应该注销这个服务的,此时 Eureka 便会通过 “自我保护模式” 来解决这个问题。
综上所述,自我保护模式是针对网络异常的安全保护措施。

注:生产环境中不建议关闭 Eureka 的自我保护模式.

服务端配置如下:

eureka:
  server:
    #关闭自我保护机制,保证不可用服务被及时剔除
    enable-self-preservation: false
    #清理无效节点的时间间隔,默认60000毫秒,60(此处时间间隔设置为2s)
    eviction-interval-timer-in-ms: 2000

客户端配置如下:

eureka:
  instance:
    # Eureka客户端向服务端发送心跳的时间间隔,单位为妙(默认是30s)
    lease-renewal-interval-in-seconds: 1
    # Eureka服务端在收到最后一次心跳后的等待时间上限,单位为秒(默认90s),超时将移除服务
    lease-expiration-duration-in-seconds: 2

八、Eureka的多网卡环境下IP选择

1、忽略指定名称ip

spring:
  cloud:
    inetutils:
      ignored-interfaces:
        - docker0
        - veth.*
eureka:
  instance:
    prefer-ip-address: true

2、使用正则表达式,指定使用的地址

spring:
  cloud:
    inetutils:
      preferredNetworks:
        - 192.168
        - 10.0
eureka:
  instance:
    prefer-ip-address: true

3、手动指定ip

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwd2307997664

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值