1. 本文采用Spring cloud本文为2.1.8RELEASEversion=Greenwich.SR3

1 创建eureka client

1.1 新建Srping boot工程:eureka-client

1.2 pom.xml所需要依赖的jar包

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  8. </dependency>

1.3 EurekaClientApplication添加注解@EnableEurekaClient

  1. package spring.cloud.demoo.eurekaclient;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @EnableEurekaClient
  6. @SpringBootApplication
  7. public class EurekaClientApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaClientApplication.class, args);
  10. }
  11. }

@EnableEurekaClient 向eureka服务中心注册服务(推荐使用),如果是其他服务注册中心,例如:consul、zookeeper,建议使用@EnableDiscoveryClient

1.4 添加application.yml配置内容

  1. spring:
  2. application:
  3. name: eureka-client
  4. server:
  5. port: 8801
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. # 表示eureka client间隔多久去拉取服务注册信息,默认为30秒,如果要迅速获取服务注册状态,可以缩小该值
  10. lease-renewal-interval-in-seconds: 5
  11. # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
  12. # 默认为90秒
  13. # 如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
  14. # 如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
  15. # 该值至少应该大于 leaseRenewalIntervalInSeconds
  16. lease-expiration-duration-in-seconds: 10
  17. client:
  18. service-url:
  19. defaultZone: http://localhost:8701/eureka/

配置中http://localhost:8701/eureka/ 调用eureka-server,可以参考: eureka-server注册中心搭建

1.5 创建测试controller:EurekaClientController

  1. package spring.cloud.demoo.eurekaclient.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import javax.servlet.http.HttpServletRequest;
  6. /**
  7. * @auther: maomao
  8. * @DateT: 2019-09-17
  9. */
  10. @RestController
  11. public class EurekaClientController {
  12. @Value("${server.port}")
  13. private String port;
  14. @RequestMapping("/info")
  15. public String syaHello(HttpServletRequest request) {
  16. String message = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getServletPath();
  17. return message;
  18. }
  19. }

1.6 启动eureka-client服务

访问http://localhost:8801/info,并返回结果,如下图所示。

这时在访问服务注册中心,如果下入所示:

可以看到eureka-client已经注册到服务注册中心。

至此,一个简单的单机eureka client服务提供者就搭建完成。服务的提供者提供一个Restful服务。

彩蛋

eureka client集群的搭建

1.1 配置本地host文件

  1. 127.0.0.1 eureka1.client.com
  2. 127.0.0.1 eureka2.client.com
  3. 127.0.0.1 eureka3.client.com

1.2 增加application.yml配置文件

增加application-clinet1.yml和application-clinet2.yml文件,修改原application.yml文件。

  • application.yml
  1. spring:
  2. application:
  3. name: eureka-client
  4. server:
  5. port: 8801
  6. eureka:
  7. instance:
  8. hostname: eureka1.client.com
  9. lease-renewal-interval-in-seconds: 5
  10. lease-expiration-duration-in-seconds: 10
  11. client:
  12. service-url:
  13. defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
  • application-client1.yml
  1. spring:
  2. application:
  3. name: eureka-client
  4. server:
  5. port: 8802
  6. eureka:
  7. instance:
  8. hostname: eureka2.client.com
  9. lease-renewal-interval-in-seconds: 5
  10. lease-expiration-duration-in-seconds: 10
  11. client:
  12. service-url:
  13. defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
  • application-client2.yml
  1. spring:
  2. application:
  3. name: eureka-client
  4. server:
  5. port: 8803
  6. eureka:
  7. instance:
  8. hostname: eureka3.client.com
  9. lease-renewal-interval-in-seconds: 5
  10. lease-expiration-duration-in-seconds: 10
  11. client:
  12. service-url:
  13. defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

1.3 分别启动eureka-client三个服务

分别访问http://eureka1.client.com:8801/info、http://eureka2.client.com:8802/info、 http://eureka3.client.com:8803/info

显示结果如下:



此时打开服务注册中心,显示

截图中红框中代表三个client已经注册成功。

总结

本文简单实现了服务提供者单机和集群的搭建,后续继续更新其他关于spring cloud其他内容。

代码地址

gitHub地址


《Srping Cloud 2.X小白教程》目录


  • 写作不易,转载请注明出处,喜欢的小伙伴可以关注公众号查看更多喜欢的文章。
  • 联系方式:4272231@163.com

spring cloud 2.x版本 Eureka Client服务提供者教程的更多相关文章

  1. spring cloud 2.x版本 Eureka Server服务注册中心教程

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1.创建服务注册中心 1.1 新建Spring boot工程:eureka-server 1 ...

  2. spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  3. spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  4. spring cloud 2.x版本 Zuul路由网关教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  5. spring cloud 2.x版本 Gateway动态路由教程

    摘要 本文采用的Spring cloud为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的几篇Spring cloud Gateway文章的实现. 参考 Gatew ...

  6. spring cloud 2.x版本 Gateway路由网关教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  7. spring cloud 2.x版本 Hystrix Dashboard断路器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  8. spring cloud 2.x版本 Config配置中心教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...

  9. spring cloud 2.x版本 Gateway自定义过滤器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

随机推荐

  1. word2vec预训练词向量

    NLP中的Word2Vec讲解 word2vec是Google开源的一款用于词向量计算 的工具,可以很好的度量词与词之间的相似性: word2vec建模是指用CBoW模型或Skip-gram模型来计算 ...

  2. springcloud --- spring cloud sleuth和zipkin日志管理(spring boot 2.18)

    前言 在spring cloud分布式架构中,系统被拆分成了许多个服务单元,业务复杂性提高.如果出现了异常情况,很难定位到错误位置,所以需要实现分布式链路追踪,跟进一个请求有哪些服务参与,参与的顺序如 ...

  3. SpringBoot返回JSON

    目录 1.SpringBoot返回JSON简介 2.整合jackson-databind 3.整合Gson 4.整合fastjson 1.SpringBoot返回JSON简介 随着web开发前后端分离 ...

  4. Ansible Roles角色

    Roles小技巧: 1.创建roles目录结构,手动或使用ansible-galaxy init test roles 2.编写roles的功能,也就是tasks. nginx rsyncd memc ...

  5. Cocos Creator 3D 打砖块教程(二) | 子弹发射与摄像机平滑移动

    在线体验链接: http://example.creator-star.cn/block3d/ 前面一篇文章,我们讲了[打砖块]游戏中的3D物体的场景布局.材质资源.物理刚体与碰撞组件,接下来本篇文章 ...

  6. BeetleX服务网关之限流和缓存

    限流和缓存相关是网关中两个非常重要的功能,前者是保障服务更可靠地运行,后者则可以大大提高应用的吞吐能力.Beetlex.Bumblebee微服务网关提供了两个扩展插件来实现这两个功能,分别是Beetl ...

  7. openpyxl中遇到TypeError: 'generator' object is not subscriptable的问题和解决方案

    今天在搭建驱动数据框架用到了一个叫 openpyxl的包用来解析excel数据 随后就出现了TypeError: 'generator' object is not subscriptable的bug ...

  8. Mint(Linux)系统设置优化及其常用软件安装笔记

    LInux /home下中文目录如何修改成英文? 打开终端,在终端中输入命令: export LANG=en_US xdg-user-dirs-gtk-update 跳出对话框询问是否将目录转化为英文 ...

  9. BZOJ 2535: [Noi2010]Plane 航空管制2

    Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上 ...

  10. asp.net mvc select用法

    var statusSelectItems = new List<SelectListItem> { "}, "}, "}, "}, "} ...