• 服务注册与发现eureka


    前言

      微服务中,各个微服务动态变化,所以需要服务注册与发现功能。单个服务更加细粒度,多服务管理更加困难。服务注册与发现一般来说需要服务端和客户端两部分。

      spring cloud netflix eureka是spring cloud提供的服务发现和注册的基础组件之一,另外两个是consul和zookeeper。eureka是开箱即用的,屏蔽了server、client的交互细节。

    基础:

      eureka是一个restful风格的服务发现与注册的基础服务组件。由server和client两部分组成,前者提供注册与发现功能,后者提供简化透明的与server交互的功能,完成服务的注册与发现。

    ********************************************************************************************

    Eureka服务搭建

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5     <parent>
     6         <groupId>org.springframework.boot</groupId>
     7         <artifactId>spring-boot-starter-parent</artifactId>
     8         <version>2.2.5.RELEASE</version>
     9         <relativePath/> <!-- lookup parent from repository -->
    10     </parent>
    11     <groupId>org.yang</groupId>
    12     <artifactId>cloud</artifactId>
    13     <version>0.0.1-SNAPSHOT</version>
    14     <name>cloud</name>
    15     <description>Demo project for Spring Boot</description>
    16 
    17     <properties>
    18         <java.version>1.8</java.version>
    19         <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    20     </properties>
    21 
    22     <dependencies>
    23         <dependency>
    24             <groupId>org.springframework.boot</groupId>
    25             <artifactId>spring-boot-starter-actuator</artifactId>
    26         </dependency>
    27         <dependency>
    28             <groupId>org.springframework.boot</groupId>
    29             <artifactId>spring-boot-starter-web</artifactId>
    30             <exclusions>
    31                 <exclusion>
    32                     <groupId>org.springframework.boot</groupId>
    33                     <artifactId>spring-boot-starter-tomcat</artifactId>
    34                 </exclusion>
    35             </exclusions>
    36         </dependency>
    37         <dependency>
    38             <groupId>org.springframework.boot</groupId>
    39             <artifactId>spring-boot-starter-jetty</artifactId>
    40         </dependency>
    41 
    42         <dependency>
    43             <groupId>org.springframework.cloud</groupId>
    44             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    45         </dependency>
    46 
    47         <dependency>
    48             <groupId>org.springframework.boot</groupId>
    49             <artifactId>spring-boot-starter-test</artifactId>
    50             <scope>test</scope>
    51             <exclusions>
    52                 <exclusion>
    53                     <groupId>org.junit.vintage</groupId>
    54                     <artifactId>junit-vintage-engine</artifactId>
    55                 </exclusion>
    56             </exclusions>
    57         </dependency>
    58     </dependencies>
    59 
    60     <dependencyManagement>
    61         <dependencies>
    62             <dependency>
    63                 <groupId>org.springframework.cloud</groupId>
    64                 <artifactId>spring-cloud-dependencies</artifactId>
    65                 <version>${spring-cloud.version}</version>
    66                 <type>pom</type>
    67                 <scope>import</scope>
    68             </dependency>
    69         </dependencies>
    70     </dependencyManagement>
    71 
    72     <build>
    73         <plugins>
    74             <plugin>
    75                 <groupId>org.springframework.boot</groupId>
    76                 <artifactId>spring-boot-maven-plugin</artifactId>
    77             </plugin>
    78         </plugins>
    79     </build>
    80 </project>

    更换了servlet容器,引入了cloud的监控工具actuator模块。

     1 server:
     2   port: 8083
     3 
     4 management: #默认http暴露health和info 且所有端点都在management下管理
     5   endpoints: #可通过endpoints.<method>.exposure.exclude/include单独配置需要暴露的端点
     6     web:
     7       exposure:
     8         include: info,health,beans #对外http仅暴露/info,/health,/beans三个端点
     9   endpoint:
    10     health:
    11       show-details: always #展示细节
    12 eureka:
    13   instance:
    14     hostname: eureka1
    15     instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    16   client:
    17     register-with-eureka: true #向eureka服务器(下面的defaultZone)注册自己的信息
    18     fetch-registry: true #向eureka服务器获取注册信息
    19     service-url:
    20       defaultZone: http://*。*。*。*:8084/eureka/ #eureka server注册中心的地址 *注意这里配置的是自己,一个eureka server同时也是一个client
    21       #它也会尝试注册自己到server,所以需要至少一个注册中心的url来定位对等点peer,如果不提供这样一个注册点也可以,但是会在日志中打印无法向peer注册自己的信息,单机模式关闭同步和拉取即可
    22 spring:
    23   application:
    24     name: eureka-server1 #service1服务类型
    25 logging: 26  level: 27 org.springframework: INFO #SPRINGBOOT-DEBUG 可以选择debug模式方便查找问题

    启动脚本: 

     1 #!/bin/sh
     2 
     3 eureka1=eureka1-1.0-SNAPSHOT.jar
     4 
     5 tpid=`ps -ef|grep -i $eureka1|grep -v 'grep'|grep -v 'kill'|awk '{print $2}'`
     6 
     7 if [ ${tpid} ] ; then
     8 echo 'stop eureka1'
     9 kill -15 $tpid
    10 fi
    11 
    12 sleep 5
    13 
    14 tpid=`ps -ef|grep -i $eureka1|grep -v 'grep'|grep -v 'kill'|awk '{print $2}'`
    15 
    16 if [ ${tpid} ] ; then
    17 echo 'kill eureka1'
    18 kill -9 $tpid
    19 else
    20 echo 'kill success!'
    21 fi
    22 
    23 tpid=`ps -ef|grep -i $eureka1|grep -v 'grep'|grep -v 'kill'|awk '{print $2}'`
    24 
    25 if [ ${tpid} ] ; then
    26     echo 'App is running.'
    27 else
    28     echo 'App is NOT running.'
    29 fi
    30 
    31 nohup java -jar $eureka1  >eureka1log.txt &
    32 
    33 echo $! > tpid
    34 echo Start Success!

     ********************************************************************************************

     搭建基础

      Eureka Server:eureka server的自动配置需要注解@EnableEurekaServer,为项目自动配置必须的注册中心配置类,并将此服务标识位注册中心。

     Eureka Client:目前最新的eureka client,只要引入client依赖,此服务就会自动注册到配置文件中的defaultZone注册中心,不需要做任何操作,如果没有配置文件就使默认的;localhost:默认端口.

     配置负载均衡RestTemplate

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
    return new RestTemplate();
    }

    在服务调用者里调用远程服务,注意下面url中的eureka-service2,此为一个服务类型,resttemplate通过ribbon进行自动负载均衡从注册中心的eureka-service2服务中选择一个去发送此Http请求。

      RestTemplate是spring提供的同步HTTP网络客户端接口,强制使用RESTful风格,他会自己处理HTTP连接和关闭,只需要用户提供服务器地址和模板参数

      ribbon是一个管理Htpp和Tcp服务客户端的负载均衡器,通过指定不同的配置类,可以指定ribbon的负载均衡策略。

      RestTemplate和OpenFeign具有负载均衡的关键就是springCloud提供的两者与ribbon的无缝对接,两者都会被默认实现。

      @ribbonClient注解可以定制ribbon,name和configuration两个属性可以定制ribbon对象的名字和配置类。在配置类中会重建IPing和IRule两个组件,提供选择不同的IPing和IRule,ribbon会具有不同的功能性,即ribbon是可配置的。ribbon可以和eureka注册中心一起工作,获取服务列表信息,也可以提供listOfServers字段指定服务地址,可以在测试的时候使用。IRule是定义负载均衡策略的接口,它的子类代表了不同的负载均衡策略,提供6种,分为两类,一类是部根据服务器进行负载均衡的,另一类就是相反的。默认类zoneavoidancerule,根据服务器所属的服务区的整体运行状况来轮询选择,考虑服务器状态。还有roundRobinRule,此轮询不考虑服务器状态。

      另外ribbon可以提供自己完成网络请求。集成Netty,将网络请求的任务将给netty去做。

    ********************************************************************************************

     Eureka Client

      eureka client是eureka client与eureka server交互的关键,它将与eureka server的交互的细节隐藏起来,自主完成。它的流程如下:

    1. 读取与eureka server交互的配置信息,封装成eurekaclientconfig。

    2. 读取自身服务实例封装成eurekaInstanceConfig。

    3. 此eureka server中拉取注册表信息并缓存到本地。

    4. 服务注册。

    5. 初始化执行发送心跳,缓存刷新,按需注册定时任务。

    6. 定时发送心跳,拉取信息,按需注册。

    7. 如需下线,则从eureka server中注销自身服务。

      springboot 通过EurekaDiscoveryClientConfiguration类完成对eureka client必要bean的属性读取和配置,包括eurekaClientConfig和EurekaInstanceConfig。另外还有DiscoveryClient客户端,这是spring中定义用服务发现的客户端接口。

      DiscoveryClient是springcloud的服务发现的顶级接口,eureka和consul中都有各自的实现。

      

     DiscoveryClient做的事情是:

      注册实例到eureka server;

      发送心跳更新与eureka server的租约;

      在服务关闭时,取消租约;

      查询在eureka server中注册的服务实例列表。

    Eureka Server

      服务剔除:针对长时间不续约,不下线的服务,eureka server会将他们剔除,以保证服务的可靠性,但服务剔除方法evict有很多限制,以此来保证eureka server的高可用。

    1. 自我保护期间不能进行服务剔除

    2. 过期操作时分批进行的。

    3. 服务剔除时随机逐个剔除,均匀分布在所有的应用中,防止程序崩溃。

      注册中心集群:

      eureka server 集群包括两个部分:一部分是eureka server在启动过程中会拉去peer节点的注册表信息。注册到自身注册表中 ;另一部分是自身每次对本地注册表进行操作时,同时会将操作同步到它的peer节点中。

      另外eureka server本身也是一个eureka client,所以它也会进行discoveryClient的初始化。

  • 相关阅读:
    网络基础
    Web开发几种常用工具
    win组合键概述(windows10)
    Alfred使用
    Java8之新特性--modules
    jsp九大内置对象和四大作用域
    authtype
    Myeclipse中的快捷键
    如何在Jenkins CI 里调试
    写好unit test的建议和例子
  • 原文地址:https://www.cnblogs.com/YsirSun/p/12526638.html
Copyright © 2020-2023  润新知