zoukankan      html  css  js  c++  java
  • eureka 服务注册与发现

     
    Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
     
    Eureka包含两个组件:Eureka Server和Eureka Client。
    Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
    Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。
    在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。
    Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。
     
    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.8.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
        </properties>
    
        <dependencies>
            <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-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

      在启动类 声明注册中心 

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

     添加application.yml 配置

    spring:
      application:
        name: eurekaServer  #应用名称
    server:
      port: 8761   #eureka 默认端口8761
    
    eureka:
      instance:
        hostname: localhost  #当前实例主机名
      client:
        service-url:
          defaultZone: http://${eureka.instance.hostname}/${server.port}/eureka/
        register-with-eureka: false # 是否注册到eureka服务,默认为true,自己不注册配置为false
        fetch-registry: false  #是否从eureka服务器获得注册信息,默认true

    2.客户端配置

      客户端依赖为client 

      

    <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>
    还在添加 web依赖
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    

    启动类 声明 

    @EnableEurekaClient

    yaml 配置
    
    
    spring:
    application:
    name: eurekaClient

    server:
    port: 8080

    eureka:
    client:
    service-url:
    defaultZone: http://localhost:8761/eureka/

    启动 两个 服务 

     

      成功 注册上来了 

      3 .eureka 高可用   

         

         eureka集群 (3个以上 )  互相注册 

          eureka1 注册  eureka 2 ,eureka 3

         

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/

         eureka2 注册  eureka 1 ,eureka 3

        eureka3 注册  eureka 1 ,eureka 2

     

      客户端则  注册 三个  

       

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
  • 相关阅读:
    python3 装饰器
    Python3 迭代器与生成器
    Python3 循环
    Python3 条件控制
    Python3 字典
    Python3 元组
    Python3 列表
    08.HttpUrlconnection方式调用
    南海区行政审批管理系统接口规范v0.3(规划)
    day63-webservice 11.cxf整合spring
  • 原文地址:https://www.cnblogs.com/qin1993/p/11497555.html
Copyright © 2011-2022 走看看