zoukankan      html  css  js  c++  java
  • SpringCloud入门实战(2)-Eureka使用

    Eureka是Netflix公司开源的产品,提供了完整的Service Registry和Service Discovery实现,也是Spring Cloud体系中最重要最核心的组件之一,可以应用在任何需要使用注册中心的场景。本文主要介绍Eureka的概念及基本使用,文中使用到的软件版本:Spring Boot 2.2.5.RELEASE、Spring Cloud Hoxton.SR3、Java 1.8.0_191。

    1、Eureka架构

    1.1、Eureka Server

    Eureka Server提供如下功能:

    服务注册
    服务提供者启动时,会通过Eureka Client向Eureka Server注册信息,Eureka Server会存储该服务的信息,其内部有二层缓存机制来维护整个注册表。

    提供注册表
    服务消费者在调用服务时,如果Eureka Client没有缓存注册表的话,会从Eureka Serve 获取最新的注册表

    同步状态
    Eureka Client通过注册、心跳机制和Eureka Server同步当前客户端的状态。

    1.2、Eureka Client

    Eureka Client用于简化与Eureka Server的交互,它会拉取、更新和缓存 Eureka Server 中的信息;因此当所有的Eureka Server节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者,但是当服务有更改的时候会出现信息不一致。

    Eureka Client又分为:Application Service(Service Provider)和Application Client(Service Consumer)。

    1.2.1、Application Service

    服务提供方,是注册到Eureka Server中的服务。

    1.2.2、Application Client

    服务消费方,通过Eureka Server发现服务,并消费。

    一个应用可以同时是Application Service和Application Client。

    1.3、路径

    Register(服务注册):把自己的IP和端口等信息注册给Eureka Server。
    Renew(服务续约):发送心跳包,每30秒发送一次,告诉Eureka Server自己还活着。
    Cancel(服务下线):Eureka Client在程序关闭时向Eureka Server发送取消请求;发送请求后,该客户端实例信息将从Eureka Server的实例注册表中删除。
    Get Registry(获取服务注册列表):获取注册列表。
    Replicate(集群中数据同步):Eureka Server集群中的数据复制与同步。
    Make Remote Call(远程调用):完成远程服务调用。

    2、Eureka常用配置参数

    2.1、eureka.instance

    hostname 与此实例相关联的主机名,是其他实例可以用来进行请求的准确名称
    lease-renewal-interval-in-seconds Eureka Client发送心跳给Eureka Server的时间间隔,默认为30 秒
    lease-expiration-duration-in-seconds Eureka Server在接收到实例的最后一次发出的心跳后,等待多久才可以将此实例删除,默认为90秒

    2.2、eureka.server

    enable-self-preservation

    自我保护模式,当出现出现网络分区、eureka在短时间内丢失过多客户端时,会进入自我保护模式,即一个服务长时间没有发送心跳,Eureka Server也不会将其删除,默认为true

    2.3、eureka.client

    register-with-eureka Eureka Client是否在Eureka Server上注册自己的信息以供其他服务发现,默认为true
    fetch-registry Eureka Client是否获取eureka服务器注册表上的注册信息,默认为true

    3、Eureka Server搭建

    3.1、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.inspur</groupId>
        <artifactId>scdemo-eureka</artifactId>
        <version>1.0</version>
        
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.5.RELEASE</version>
            <relativePath />
        </parent>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Hoxton.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

    3.2、application.yml

    spring:
      application:
        name: scdemo-eureka
    
    server:
      port: 9000
    
    eureka:
      instance:
        prefer-ip-address: true
        #hostname: localhost
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://localhost:${server.port}/eureka
      server:
        enable-self-preservation: false

    3.3、启动类

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaApplication.class, args);
        }
    }

    3.4、控制台

    启动Eureka Server后,可以在控制台查看注册的服务及相关的状态信息;根据上面的配置,控制台地址为:

    http://localhost:9000/

    3.5、Eureka Server集群搭建

    3.5.1、application.yml

    集群的搭建需要修改下application.yml,设置eureka.client.register-with-eureka、eureka.client.fetch-registry为true,eureka.client.service-url.defaultZone改为集群的地址;假设在10.49.196.10、10.49.196.11、10.49.196.12三台机器上部署Eureka Server,则application.yml修改如下:

    spring:
      application:
        name: scdemo-eureka
    
    server:
      port: 9000
    
    eureka:
      instance:
        prefer-ip-address: true
        #hostname: localhost
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://10.49.196.10:9000/eureka,http://10.49.196.11:9000/eureka,http://10.49.196.12:9000/eureka
      server:
        enable-self-preservation: false

    3.5.2、部署

    分别在三台机器上启动Eureka Server,查看各控制台:

    http://10.49.196.10:9000/

     http://10.49.196.10:9001/

     

      http://10.49.196.12:9001/

    4、Eureka Client使用

    4.1、引入Eureka Client依赖

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

    4.2、配置Eureka

    eureka:
      instance:
        prefer-ip-address: true
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://localhost:9000/eureka/

    4.3、启动类启用Eureka Client

    @SpringBootApplication
    @EnableEurekaClient
    public class ServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ServerApplication.class, args);
        }
    }
  • 相关阅读:
    Nginx自动加载配置文件方案
    git使用说明
    zabbix监控项截图整理
    Redhat 6.7 x64升级SSH到OpenSSH_7.4p1完整文档
    一次完整的浏览器请求流程
    JavaScript
    模块
    常用模块学习
    jenkins+Gitlab+maven+tomcat实现自动集成、打包、部署
    在线抠图网站速抠图sukoutu.com全面技术解析之canvas应用
  • 原文地址:https://www.cnblogs.com/wuyongyin/p/13447167.html
Copyright © 2011-2022 走看看