Eureka Server 与 Eureka Client 的搭建与调用

        Eureka是Spring Cloud框架下的一个功能模块,起到提供服务注册中心、服务注册与服务发现的作用。Netflix在设计EureKa时遵循着AP原则,它基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。Eureka同其他服务注册中心一样,支持高可用配置。当集群中有分片出现故障时,那么Eureka就转入自我保护模式。它允许在分片故障期间继续提供服务的发现和注册,当故障分片恢复运行时,集群的其他分片会把它们的状态再次同步回来。

      本篇主要讲讲如何搭建注册中心和注册服务,最后实现各个服务之间的相互调用。

这里分成三个方面做简述:

一、Eureka-Server(服务注册中心提供服务注册功能)

服务注册Eureka之后,会每隔30s发送心跳更新一次租约,如果客户端因故障无法续租几次,则会在90s内将其从注册中心注册表中移除。注册信息和更新会被复制到集群中的所有Eureka节点。来自任何区域的客户端每隔30s查找注册表信息来找到他们的服务并进行远程调用。

默认情况下,每个Eureka服务器也是一个Eureka客户端,并且至少需要一个服务地址来定位对等体,通常的做法是关闭服务自身注册功能。

1.依赖jar包:

spring-cloud-starter-eureka-server

spring-cloud-dependencies

2.pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 添加springcloud-erueka服务端依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>

<!-- spring-cloud版本依赖 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.注解:

@EnableEurekaServer

标注此应用为Eureka服务注册中心

4.配置:

server.port=10001
spring.application.name=Eureka-Server

#设置当前实例的主机名称
eureka.instance.hostname=localhost
#IP地址
eureka.instance.ip-address=localhost
#eureka默认空间的地址
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
# 注册时显示ip
eureka.instance.prefer-ip-address=true

#如下配置需自行配置(不配置为默认值)
#关闭自我保护(生产时打开该选项)
#关闭注册中心的保护机制,Eureka 会统计15分钟之内心跳失败的比例低于85%将会触发保护机制,不剔除服务提供者,如果关闭服务注册中心将不可用的实例正确剔除
eureka.server.enable-self-preservation=true
#是否注册为服务
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#定义服务续约任务(心跳)的调用间隔,单位:秒
eureka.instance.lease-renewal-interval-in-seconds=30
#定义服务失效的时间,单位:秒
eureka.instance.lease-expiration-duration-in-seconds=90
#状态页面的URL,相对路径,默认使用 HTTP 访问,如果需要使用 HTTPS则需要使用绝对路径配置
eureka.instance.status-page-url-path=/info
#健康检查页面的URL,相对路径,默认使用 HTTP 访问,如果需要使用 HTTPS则需要使用绝对路径配置
eureka.instance.health-check-url-path=/health
#健康检查页面的URL,绝对路径
eureka.instance.health-check-url=/
#扫描失效服务的间隔时间(缺省为60*1000ms)
eureka.server.eviction-interval-timer-in-ms=5000

5.启动类:

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

 

二、Eureka-Client(注册服务到服务中心)

服务提供者,把服务注册到服务中心上,与Eureka-Server配合向外提供服务。

1.依赖jar包:

spring-cloud-starter-netflix-eureka-client

spring-cloud-starter-feign(此客户端调用服务时需加)

2.pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
</dependencies>


<!-- spring-cloud版本依赖 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.注解:

@EnableFeignClients
启用Feign客户端,如此应用调用服务时需添加

如下两个注解均表示声明是Eureka客户端

@EnableEurekaClient

基于spring-cloud-netflix,在服务采用eureka作为注册中心的时候,使用场景较为单一。将应用程序同时进入一个Eureka“实例”(即注册自己)和一个“客户端”(即它可以查询注册表以查找其他服务)

 

@EnableDiscoveryClient

基于spring-cloud-commons如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient。

4.配置:

server.port=10002
spring.application.name=Eureka-Client-1

#设置当前实例的主机名称
eureka.instance.hostname=localhost
#IP地址
eureka.instance.ip-address=localhost
#eureka默认空间的地址
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
# 注册时显示ip
eureka.instance.prefer-ip-address=true

#关闭自我保护(生产时打开该选项)
#关闭注册中心的保护机制,Eureka 会统计15分钟之内心跳失败的比例低于85%将会触发保护机制,不剔除服务提供者,如果关闭服务注册中心将不可用的实例正确剔除
eureka.server.enable-self-preservation=true

5.启动类:

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

 

三、Feign()

1.依赖jar包:

spring-cloud-starter-netflix-eureka-client(此客户端)

spring-cloud-starter-feign

2.pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

<!-- 此jar包是方便管理服务本地建的,以后增加服务可在此应用里维护 -->
    <dependency>
        <groupId>Eureka-Common</groupId>
        <artifactId>Common-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>system</scope>
        <systemPath>/Users/jeremy/Documents/GitHub/Eureka-Common/target/Common-demo-1.0-SNAPSHOT.jar</systemPath>
    </dependency>

</dependencies>


<!-- spring-cloud版本依赖 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.配置:

server.port=10003
spring.application.name=Eureka-Client-2

#设置当前实例的主机名称
eureka.instance.hostname=localhost
#IP地址
eureka.instance.ip-address=localhost
#eureka默认空间的地址
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
# 注册时显示ip
eureka.instance.prefer-ip-address=true

#关闭自我保护(生产时打开该选项)
#关闭注册中心的保护机制,Eureka 会统计15分钟之内心跳失败的比例低于85%将会触发保护机制,不剔除服务提供者,如果关闭服务注册中心将不可用的实例正确剔除
eureka.server.enable-self-preservation=true

4.启动类:

@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients(basePackages = "com.jeremy.eureka_common")
@ComponentScan(basePackages = "com.jeremy.client_demo_2")
@SpringBootApplication
public class EurekaClientApplication {
    public static void main(String args[]){
        SpringApplication.run(EurekaClientApplication.class,args);
    }
}

服务调用(TestFeignService为jar包项目里的Feign调用接口)

 

这里引用一个本地管理服务的jar,申明调用过程如下:

指明要访问的服务名称

指明具体调用的服务

 

如上就完成搭建了一个简单的实现了Eureka服务注册、发现与调用的过程。

注册中心(Eureka-Server)、客户端(Eureka-Client-1)、客户端(Eureka-Client-2)

方便服务管理的Common-Demo。就是Eureka-Client-1、Eureka-Client-2注册到服务器上面,Eureka-Client-2通过Common-Demo调用Eureka-Client-1。

运行结果:

 

github地址:

https://github.com/JeremyHJ/client-demo-1

https://github.com/JeremyHJ/client-demo-2

https://github.com/JeremyHJ/ereka-common

https://github.com/JeremyHJ/server-demo

 

初次写博,望多指教!!!

 

 

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值