zoukankan      html  css  js  c++  java
  • SpringCloud系列------Eureka-Server

    一、概述:

    Spring Cloud针对服务注册与发现,进行了一层抽象,并提供了三种实现:Eureka , Consul , Zookeeper 本篇文章只对Eureka 进行介绍:

    (部分内容引用  https://blog.csdn.net/hry2015/article/details/78220673,其博主有一系列的相关SpringCloud的整理)

    Eureka:Netflix公司开发的服务发现中间件,用于服务的负载均衡和服务故障转移 

    SpringCloud 集成了EurekaSever,通过简单配置即可启动;Spring Cloud对Eureka的支持最好。

    在Eureka中,所有的Eureka服务都被称为实例(instance),这些实例又分成为两大类:

    1、Eureka Server: Eureka的服务端,即服务注册中心,负责维护所有实例的注册信息
    2、Eureka Client: Eureka的客户端,根据功能又分为两类
          a. Service Provider:服务提供方,向Eureka Server做服务注册、续约和下线等操作,注册的主要数据包括服务名、机器ip、端口号、域名等等;
          b. Service Consumer:服务消费方, 向Eureka Server获取Service Provider的注册信息,并通过远程调用与Service Provider进行通信.

    PS:可以将Service Provider和Service Consumer理解为角色。一个Eureka Client可以只是Service Provider,也可以只是Service Consumer,也可以同时即是Service Provider也是Service Consumer。(根据项目的具体情况,选择搭建架构)

    二、注册中心(Eureka-Server)------单节点

    首先大家要弄清楚下图的这种关系:

    具体操作如下:                                                                                                                                                                                                                                                                           

    1、首先创建一个Eureka-Server的工程,这里可以选择创建Maven项目,也可直接创建Spring的项目,这里我选择第二种方式:

    然后Next

    然后Finish就创建工程完成

    重点来了:进行单节点注册中心的配置如下

    pom.xml文件如下:

     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 http://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.1.3.RELEASE</version>
     9         <relativePath/> <!-- lookup parent from repository -->
    10     </parent>
    11     <groupId>com.dajian.springcloud</groupId>
    12     <artifactId>eurka-server</artifactId>
    13     <version>0.0.1-SNAPSHOT</version>
    14     <name>eurka-server</name>
    15     <description>Demo project for Spring Boot</description>
    16 
    17     <properties>
    18         <java.version>1.8</java.version>
    19         <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    20     </properties>
    21 
    22     <dependencies>
    23         <dependency>
    24             <groupId>org.springframework.cloud</groupId>
    25             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    26         </dependency>
    27 
    28         <dependency>
    29             <groupId>org.springframework.boot</groupId>
    30             <artifactId>spring-boot-starter-test</artifactId>
    31             <scope>test</scope>
    32         </dependency>
    33     </dependencies>
    34 
    35     <dependencyManagement>
    36         <dependencies>
    37             <dependency>
    38                 <groupId>org.springframework.cloud</groupId>
    39                 <artifactId>spring-cloud-dependencies</artifactId>
    40                 <version>${spring-cloud.version}</version>
    41                 <type>pom</type>
    42                 <scope>import</scope>
    43             </dependency>
    44         </dependencies>
    45     </dependencyManagement>
    46 
    47     <build>
    48         <plugins>
    49             <plugin>
    50                 <groupId>org.springframework.boot</groupId>
    51                 <artifactId>spring-boot-maven-plugin</artifactId>
    52             </plugin>
    53         </plugins>
    54     </build>
    55 
    56     <repositories>
    57         <repository>
    58             <id>spring-milestones</id>
    59             <name>Spring Milestones</name>
    60             <url>https://repo.spring.io/milestone</url>
    61         </repository>
    62     </repositories>
    63 
    64 </project>
    pom.xml

     配置代码根据文件不同格式也不同,一般有application.properties和application.yml两种

     1 spring.application.name=eureka-server1
     2 server.port=10100
     3 eureka.instance.hostname=localhost
     4 # 指示此实例是否应将其信息注册到eureka服务器以供其他人发现。在某些情况下,您不希望发现实例,而您只想发现其他实例。
     5 eureka.client.registerWithEureka=false
     6 # 指示该客户端是否应从eureka服务器获取eureka注册表信息。
     7 eureka.client.fetchRegistry=false
     8 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
     9 eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${server.port}}
    10 # 关闭服务保护机制
    11 eureka.server.enable-self-preservation=false
    12 # 清理无效节点时间间隔(单位毫秒,默认是60*100013 eureka.server.eviction-interval-timer-in-ms=30000
    application.properties
     1 server:
     2   port: 8888
     3 
     4 eureka:
     5   instance:
     6     hostname: 192.168.1.11
     7     prefer-ip-address: true
     8   client:
     9     registerWithEureka: false
    10     fetchRegistry: false
    11     service-url:
    12       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    application.yml

    然后通过浏览器访问:http://localhost:10100  看到下面的界面就说明最简单的注册中心配置成功

    二、注册中心集群(Eureka-Servers)------多节点

     前面介绍介绍了注册中心的单节点配置过程,下面来介绍一下多节点配置过程,也就是为了使项目达到高可用的目的,进行集群化配置,首先我们来看一下关系图:

                                          图一:Eureka官网

                               图二:理解一下集群

    为了方面看我将上面的单节点注册中心地配置改为4个yml类型的文件

    1 spring:
    2   profiles:
    3     active: peer1
    4 eureka:
    5   server:
    6     # 关闭自我保护模式
    7     enable-self-preservation: false
    8     # 设置清理间隔,单位为毫秒,默认为0
    9     eviction-interval-timer-in-ms: 3000
    application.yml
     1 spring:
     2   application:
     3     name: eureka-server1
     4 server:
     5   port: 10101
     6 eureka:
     7   client:
     8     service-url:
     9       defaultZone: http://localhost:10102/eureka/,http://localhost:10103/eureka/
    10   instance:
    11     instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
    application-peer1.yml
     1 spring:
     2   application:
     3     name: eureka-server2
     4 server:
     5   port: 10102
     6 eureka:
     7   client:
     8     service-url:
     9       defaultZone: http://localhost:10101/eureka/,http://localhost:10103/eureka/
    10   instance:
    11     instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
    application-peer2.yml
     1 spring:
     2   application:
     3     name: eureka-server3
     4 server:
     5   port: 10103
     6 eureka:
     7   client:
     8     service-url:
     9       defaultZone: http://localhost:10101/eureka/,http://localhost:10102/eureka/
    10   instance:
    11     instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
    application-peer3.yml

     为了方便观察和操作,我的启动方式在1个Idea上启动三个Eureka-Server,具体操作如下:

    当只启动1个或两个时,控制台会报错,这是正常的现象,启动完第三个,在控制台会看到类似下面的代码输出:

    Registered instance EUREKA-SERVER1/eureka-server1:10101 with status UP (replication=true)

    Registered instance EUREKA-SERVER2/eureka-server2:10102 with status UP (replication=true)

    Registered instance EUREKA-SERVER3/eureka-server3:10103 with status UP (replication=true)

    然后通过浏览器,使用10101,10102,10103任意一个端口去访问都可以看到如下图,说名三节点的Eureka-server注册中心集群搭建完成:

    其他服务的配置只需要修改如下代码:

  • 相关阅读:
    GF4 Beta 冲刺Scrum meeting2
    GF4 Beta 冲刺Scrum meeting1
    GF4 实验九 团队作业6:团队项目编码&ALPHA冲刺
    GF4 实验九 【Alpha】Scrum Meeting 7
    GF4 实验九 【Alpha】Scrum Meeting 6
    GF4 实验九 【Alpha】Scrum Meeting 5
    GF4 实验九 【Alpha】Scrum Meeting 4
    docker安装jenkins
    docker安装svn
    idea添加web项目报404问题
  • 原文地址:https://www.cnblogs.com/wdzhz/p/10422945.html
Copyright © 2011-2022 走看看