当前位置:   article > 正文

Eureka Server 与 Eureka Client 的搭建与调用_enableeurekaserver

enableeurekaserver

        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:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- 添加springcloud-erueka服务端依赖 -->
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  10. </dependency>
  11. </dependencies>
  12. <!-- spring-cloud版本依赖 -->
  13. <dependencyManagement>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.cloud</groupId>
  17. <artifactId>spring-cloud-dependencies</artifactId>
  18. <version>Dalston.SR1</version>
  19. <type>pom</type>
  20. <scope>import</scope>
  21. </dependency>
  22. </dependencies>
  23. </dependencyManagement>

3.注解:

@EnableEurekaServer

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

4.配置:

  1. server.port=10001
  2. spring.application.name=Eureka-Server
  3. #设置当前实例的主机名称
  4. eureka.instance.hostname=localhost
  5. #IP地址
  6. eureka.instance.ip-address=localhost
  7. #eureka默认空间的地址
  8. eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
  9. # 注册时显示ip
  10. eureka.instance.prefer-ip-address=true
  11. #如下配置需自行配置(不配置为默认值)
  12. #关闭自我保护(生产时打开该选项)
  13. #关闭注册中心的保护机制,Eureka 会统计15分钟之内心跳失败的比例低于85%将会触发保护机制,不剔除服务提供者,如果关闭服务注册中心将不可用的实例正确剔除
  14. eureka.server.enable-self-preservation=true
  15. #是否注册为服务
  16. eureka.client.register-with-eureka=false
  17. #是否检索服务
  18. eureka.client.fetch-registry=false
  19. #定义服务续约任务(心跳)的调用间隔,单位:秒
  20. eureka.instance.lease-renewal-interval-in-seconds=30
  21. #定义服务失效的时间,单位:秒
  22. eureka.instance.lease-expiration-duration-in-seconds=90
  23. #状态页面的URL,相对路径,默认使用 HTTP 访问,如果需要使用 HTTPS则需要使用绝对路径配置
  24. eureka.instance.status-page-url-path=/info
  25. #健康检查页面的URL,相对路径,默认使用 HTTP 访问,如果需要使用 HTTPS则需要使用绝对路径配置
  26. eureka.instance.health-check-url-path=/health
  27. #健康检查页面的URL,绝对路径
  28. eureka.instance.health-check-url=/
  29. #扫描失效服务的间隔时间(缺省为60*1000ms)
  30. eureka.server.eviction-interval-timer-in-ms=5000

5.启动类:

  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public class EurekaServerApplication {
  4. public static void main(String args[]){
  5. SpringApplication.run(EurekaServerApplication.class, args);
  6. }
  7. }

 

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

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

1.依赖jar包:

spring-cloud-starter-netflix-eureka-client

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

2.pom.xml:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.9.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-eureka</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-feign</artifactId>
  19. </dependency>
  20. </dependencies>
  21. <!-- spring-cloud版本依赖 -->
  22. <dependencyManagement>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-dependencies</artifactId>
  27. <version>Dalston.SR1</version>
  28. <type>pom</type>
  29. <scope>import</scope>
  30. </dependency>
  31. </dependencies>
  32. </dependencyManagement>

3.注解:

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

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

@EnableEurekaClient

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

 

@EnableDiscoveryClient

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

4.配置:

  1. server.port=10002
  2. spring.application.name=Eureka-Client-1
  3. #设置当前实例的主机名称
  4. eureka.instance.hostname=localhost
  5. #IP地址
  6. eureka.instance.ip-address=localhost
  7. #eureka默认空间的地址
  8. eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
  9. # 注册时显示ip
  10. eureka.instance.prefer-ip-address=true
  11. #关闭自我保护(生产时打开该选项)
  12. #关闭注册中心的保护机制,Eureka 会统计15分钟之内心跳失败的比例低于85%将会触发保护机制,不剔除服务提供者,如果关闭服务注册中心将不可用的实例正确剔除
  13. eureka.server.enable-self-preservation=true

5.启动类:

  1. @EnableAutoConfiguration
  2. @EnableEurekaClient
  3. @SpringBootApplication
  4. public class EurekaClientApplication {
  5. public static void main(String args[]){
  6. SpringApplication.run(EurekaClientApplication.class,args);
  7. }
  8. }

 

三、Feign()

1.依赖jar包:

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

spring-cloud-starter-feign

2.pom.xml:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.9.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-eureka</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-feign</artifactId>
  19. </dependency>
  20. <!-- 此jar包是方便管理服务本地建的,以后增加服务可在此应用里维护 -->
  21. <dependency>
  22. <groupId>Eureka-Common</groupId>
  23. <artifactId>Common-demo</artifactId>
  24. <version>1.0-SNAPSHOT</version>
  25. <scope>system</scope>
  26. <systemPath>/Users/jeremy/Documents/GitHub/Eureka-Common/target/Common-demo-1.0-SNAPSHOT.jar</systemPath>
  27. </dependency>
  28. </dependencies>
  29. <!-- spring-cloud版本依赖 -->
  30. <dependencyManagement>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-dependencies</artifactId>
  35. <version>Dalston.SR1</version>
  36. <type>pom</type>
  37. <scope>import</scope>
  38. </dependency>
  39. </dependencies>
  40. </dependencyManagement>

3.配置:

  1. server.port=10003
  2. spring.application.name=Eureka-Client-2
  3. #设置当前实例的主机名称
  4. eureka.instance.hostname=localhost
  5. #IP地址
  6. eureka.instance.ip-address=localhost
  7. #eureka默认空间的地址
  8. eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
  9. # 注册时显示ip
  10. eureka.instance.prefer-ip-address=true
  11. #关闭自我保护(生产时打开该选项)
  12. #关闭注册中心的保护机制,Eureka 会统计15分钟之内心跳失败的比例低于85%将会触发保护机制,不剔除服务提供者,如果关闭服务注册中心将不可用的实例正确剔除
  13. eureka.server.enable-self-preservation=true

4.启动类:

  1. @EnableAutoConfiguration
  2. @EnableEurekaClient
  3. @EnableFeignClients(basePackages = "com.jeremy.eureka_common")
  4. @ComponentScan(basePackages = "com.jeremy.client_demo_2")
  5. @SpringBootApplication
  6. public class EurekaClientApplication {
  7. public static void main(String args[]){
  8. SpringApplication.run(EurekaClientApplication.class,args);
  9. }
  10. }

服务调用(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

 

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

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/955835
推荐阅读
相关标签
  

闽ICP备14008679号