服务发现组件概述
上篇文章 Spring Cloud-01服务提供者与服务消费者 中,我们通过配置文件的方式来配置服务提供者的地址,有两个缺点
如果服务提供者的ip和端口发生变化,将会影响服务消费者,服务消费者需要也跟着修改
高可用需要依赖第三方,通常情况下每个微服务都会部署多个实例,实现负载均衡和容灾,上篇博文中的例子在高可用上需要依赖第三方的组件比如Nginx、F5、HAProxy等
显然通过硬编码的方式无法满足上述的需求。
在微服务中,服务发现组件的位置如下
关系如上图
各个微服务在启动时,将自己的网络地址等信息注册到服务发现组件中
服务消费者可以从服务发现组件中查询服务提供者的地址,并使用该地址调用服务提供者的提供的接口
各个微服务与服务发现组件在使用一定的机制(比如心跳机制)来通信。当服务发现组件长时间无法与某微服务实例通信,将注销该实例
微服务网络地址发生变更(比如实例增减或者ip端口发生变化)时,会重新注册到服务发现组件。 使用这种方式,服务消费者就无需人工修改提供者的网络地址了。
Spring Cloud支持Eureka 、Consul 、Zookeeper 等。
这里我们主要介绍的Eureka
Eureka概述
Eureka是Netflix开源的服务发现组件,一个基于REST的服务。 包含Server和Client两部分。 Spring Cloud将它集成在子项目Spring Cloud Netflix中,从而实现微服务的注册与发现
https://github.com/Netflix/eureka
Eureka原理
官网上的图 https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance
由上图可知,Eureka包含 Eureka Server和 Eureka Client
Eureka Server: 提供服务发现的能力,各个微服务启动时,会向Eureka Server注册信息,比如ip、端口、微服务名称等。 Eureka Server会存储这些信息
Eureka Client: Java 客户端,用于简化与Eureka Server的交互
微服务启动后,会周期性(默认30S)向Eureka Server发送心跳以续约自己的“租期”
如果Eureka Server在一定时间内(默认90S)没有接收到某个微服务实例的心跳,Eureka Server将注销该实例。
默认情况下,Eureka Server 同时也是 Eureka Client . 多个Eureka Server之间通过复制的方式来实现服务注册表中数据的同步
Eureka Client会缓存服务注册表中的信息,两个好处 第一,微服务无需每次都请求查询Eureka Server ,降低Server的压力。 第二,即使Eureka Server所有节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者并完成调用。
Maven父子工程的搭建
这一步不是必须的,这里我们为了方案管理依赖
首先我们将上篇博客中用到的provider和consumer中也放到父工程中去管理,简单说下操作步骤
新建个maven 工程作为父工程,删除多余的文件和目录,仅保留pom.xml即可。
在maven父工程上右键,选择maven module,新建子模块即可。
更多的父子工程信息, 请参考 http://www.cnblogs.com/telwanggs/p/7016561.html
如何搭建也可以参考 https://blog.csdn.net/NancyWu_LuckyGirl/article/details/80244589
父工程的pom.xml 如下
<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> <!-- http://www.cnblogs.com/telwanggs/p/7016561.html --> <groupId>com.artisan</groupId> <artifactId>microservice-spring-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>microservice-spring-cloud</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <modules> <module>micorservice-provider-user</module> <module>micorservice-consumer-movie</module> <module>microservice-discovery-eureka</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
http://spring.io/projects/spring-cloud#overview
注意Spring boot 和 Spring Cloud的对应关系,否则启动可能会出错。
Eureka Server的搭建
Finchley版本的官方指导手册: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-eureka-server.html
新建 Maven Module
在父工程 microservice-spring-cloud 上右键 New ,选择
添加spring-cloud-starter-eureka-server依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency>
启动类增加@EnableEurekaServer注解
package com.artisan.microservice.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String args[]) { SpringApplication.run(EurekaApplication.class, args); } }
配置文件配置Eureka等信息
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8761/eureka
eureka.client.register-with-eureka: 是否将自己注册到Eureka Server ,默认为true.因为当前应用是作为Eureka Server用,因此设置为false
eureka.client.fetch-registry:是否从Eureka Server获取注册信息,默认为true, 因为我们这里是个单节点的Eureka Server ,不需要与其他的Eureka Server节点的数据,因此设为false
eureka.client.service-url.defaultZone : 设置与Eureka Server交互的地址,查询服务和注册服务都依赖这个地址,默认为 http://localhost:8761/eureka ,多个地址可使用 , 分隔。
启动Eureka Server测试
访问 http://localhost:8761/
Eureka Server 首页展示的信息包括 当前实例的系统状态、注册到Eureka Server的服务实例、常用信息、实例信息等 。
我们这里看到是空的
因为没有微服务注册上来,下面我们来看下如何将我们改造的用户微服务和电影微服务注册到Eureka Server上来吧。








