Spring Cloud Eureka服务注册中心

导读:本篇文章讲解 Spring Cloud Eureka服务注册中心,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Eureka注册中心

  在微服务架构中,服务注册与发现是核心组件之一,手动指定每个服务是很低效的,Spring Cloud 提供了多种服务注册与发现的实现方式,例如:Eureka、Consul、Zookeeper。Spring Cloud 支持得最好的是 Eureka,其次是 Consul,再次是 Zookeeper。

服务注册:将服务所在主机、端口、版本号、通讯协议等信息登记到注册中心上
服务发现:服务消费者向注册中心请求已经登记得服务列表,然后得到某个服务得主机、端口、版本号、通讯协议等信息,从而实现对具体服务得调用。

  Eureka是一个服务治理组件,它主要包括服务注册和服务发现,主要用来搭建服务注册中心。
  Eureka是居于REST得服务,用来定位服务,进行中间服务层得负载均衡和故障转移;
  Eureka 是 Netflix 公司开发的,Spring Cloud 封装了 Netflix 公司开发的Eureka 模块来实现服务注册和发现,也就是说 Spring Cloud 对Netflix Eureka做了二次封装
  Eureka 采用了 C-S(客户端/服务端)的设计架构,即是Eureka 由两个组件组成:Eureka 服务端和 Eureka 客户端。Eureka Server 作为服务注册的服务端,它是服务注册中心,而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server 服务端,并维持心跳连接,Eureka 客户端是一个 Java 客户端,用来简化与服务器的交互、负载均衡,服务的故障切换等;有了 Eureka 注册中心,系统的维护人员就可以通过EurekaServer 来监控系统中各个微服务是否正常运行。

快速搭建与配置 Eureka 服务注册中心

  创建一个spring boot项目(03_springcloud_eureka_server),添加web和test相关依赖坐标
添加eureka server得起步依赖

<!--Spring Cloud 的 eureka-server 起步依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--SpringCloud所有依赖管理的坐标-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--spring cloud包不能从maven下载这是配置包地址-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

Spring Cloud Server启动类:

package com.nj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ApplicationEurekaServer {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationEurekaServer.class, args);
    }
}

配置文件application.properties

#内置tomcat端口
server.port=8083

#配置注册中心hostname
eureka.instance.hostname=localhost
#由于目前创建的应用是一个服务注册中心,而不是普通的应用。默认情况下这个应用会向注册中心(及是自己)注册它自己
#设置为false表示禁止这种自己向自己注册的行为
eureka.client.register-with-eureka=false
#表示不去检索其他的服务,因为注册中心本身的职责就是维护服务实例,它不需要去检索其他的服务
eureka.client.fetch-registry=false
#指定注册中心位置
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

创建完成之后启动注册中心:
在这里插入图片描述

创建项目向 Eureka 服务注册中心注册服务

  创建一个spring boot项(01_springcloud_eureka_provider)添加web和test相关依赖坐标
  添加eureka client的起步依赖

<!--SpringCloud 集成 eureka 客户端的起步依赖-->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

  在 Spring Boot 的入口方法处,通过添加@EnableEurekaClient 注解来表明自己是一个 eureka 客户端,让我的服务提供者可以连接 eureka 注册中心;
提供者入

package com.nj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ApplicationProvider {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationProvider.class, args);
    }
}

配置文件application.properties:

#spring boot内嵌tomcat改动端口
server.port=9100

#配置服务名称和注册中心地址
spring.application.name=01springcloudprovider
eureka.client.service-url.defaultZone=http://localhost:8083/eureka

#测试关闭自我保护机制,保证不可用服务被踢出
eureka.server.enable-self-preservation=false
#每间隔5秒向服务端发送一次心跳,证明自己仍然存活
eureka.instance.lease-renewal-interval-in-seconds=5
#告诉服务端如果10S之内没有发送心跳就代表故障了,将我踢出
eureka.instance.lease-expiration-duration-in-seconds=10

***创建消费者:***(02_springcloud_eureka_provider),添加web和test相关依赖坐标
添加eureka client的起步依赖

<!--SpringCloud 集成 eureka 客户端的起步依赖-->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

入口类:

package com.nj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableEurekaClient
public class ApplicationCustomer {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationCustomer.class, args);
    }
}

配置文件application.properties:

#spring boot内嵌tomcat改动端口
server.port=9200

#配置服务名称和注册中心地址
spring.application.name=02springcloudcustomer
eureka.client.service-url.defaultZone=http://localhost:8083/eureka

#测试关闭自我保护机制,保证不可用服务被踢出
eureka.server.enable-self-preservation=false
#每间隔5秒向服务端发送一次心跳,证明自己仍然存活
eureka.instance.lease-renewal-interval-in-seconds=5
#告诉服务端如果10S之内没有发送心跳就代表故障了,将我踢出
eureka.instance.lease-expiration-duration-in-seconds=10

Ribbon实现服务的调用:

服务的发现由 eureka 客户端实现,而服务的真正调用由 ribbon
实现,所以我们需要在调用服务提供者时使用 ribbon 来调用:

package com.nj.springcloud.configer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class SpringConfig {
    /**
     * ribbon负载均衡,默认是轮询
     * @return
     */
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

Controller层代码:

package com.nj.springcloud.controller;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.nj.springcloud.common.MyHystrixCommand;
import com.nj.springcloud.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author 南江
 * @Description: ${todo}
 * @date 2020/12/21 21:38
 */
@RestController
public class SpringCloudCustomer {


    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/getCustomer")
    public String getCustomer(){
        String body = restTemplate.getForEntity("http://localhost:8081/getProviderSpringCloud", String.class).getBody();
        return body;
    }
 }

  完成上面的步骤后,我们就可以启动消费者的 SpringBoot 程序,main 方法运行;启动成功之后,通过在浏览器地址栏访问我们的消费者,看是否可以正常调用远程服务提供者提供的服务。

Eureka注册中心高可用集群搭建:

  eureka 服务注册中心它本身也是一个服务,它也可以看做是一个提供者,又可以看做是一个消费者,我们之前通过配置:
eureka.client.register-with-eureka=false 让注册中心不注册自己,但是我们可以向其他注册中心注册自己;
  Eureka Server 的高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就会形成一组互相注册的服务注册中心,进而实现服务清单的互相同步,往注册中心 A 上注册的服务,可以被复制同步到注册中心 B 上,所以从任何一台注册中心上都能查询到已经注册的服务,从而达到高可用的效果。
在这里插入图片描述
在这里插入图片描述
在下图所示的地方配置启动类所加载的配置文件Spring Cloud Eureka服务注册中心
然后在本地 hosts 文件配置:C:\Windows\System32\drivers\etc\hosts

127.0.0.1 eureka8761
127.0.0.1 eureka8762

在提供者和消费者当中配置注册中心地址:

eureka.client.service-url.defaultZone=http://erueka8761:8761/eureka,http://erueka8762:8762/eureka

启动服务查看如下图所示:
在这里插入图片描述

Eureka 服务注册中心自我保护机制

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT.
RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.emergency! eureka may be incorrectly claiming instances are up when they're not. renewals are
lesser than threshold and hence the instances are not being expired just to be safe.

Eureka通过自我保护模式来解决——当eureka节点在短时间内丢失客户端(可能发生网络故障分区),那么就会把这个微服务节点进行保护。一旦进入保护模式,Eureka Server就会保护注册表中的信息,不删除服务注册表中的数据(不会注销任何微服务)。当网络故障恢复后,该Eureka节点会在自动退出自我保护模式。
用配置项:eureka.server.enable-self-preservation = false 禁用自我保护模式。
  Eureka 的自我保护模式是有意义的,该模式被激活后,它不会从注册列表中剔除因长时间没收到心跳导致注册过期的服务,而是等待修复,直到心跳恢复正常之后,它自动退出自我保护模式。这种模式旨在避免因网络分区故障导致服务不可用的问题。

 &emsp例如,两个微服务客户端实例 A 和 B 之间有调用的关系,A 是消费者,B 是提供者,但是由于网络故障,B 未能及时向 Eureka 发送心跳续约,这时候 Eureka不能简单的将 B 从注册表中剔除,因为如果剔除了,A 就无法从 Eureka 服务器中获取 B 注册的服务,但是这时候 B 服务是可用的;所以,Eureka 的自我保护模式最好还是开启它。

关于自我保护常用几个配置如下:

服务器端配置:
#测试时关闭自我保护机制,保证不可用服务及时踢出
eureka.server.enable-self-preservation=false
客户配置:
#每间隔 2s,向服务端发送一次心跳,证明自己依然"存活"
eureka.instance.lease-renewal-interval-in-seconds=2
#告诉服务端,如果我 10s 之内没有给你发心跳,就代表我故障了,将我踢出掉
eureka.instance.lease-expiration-duration-in-seconds=10

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/77255.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!