本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3

1 创建eureka client

1.1 新建Srping boot工程:eureka-client

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

1.3 EurekaClientApplication添加注解@EnableEurekaClient

package spring.cloud.demoo.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication { public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
} }

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

1.4 添加application.yml配置内容

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

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

1.5 创建测试controller:EurekaClientController

package spring.cloud.demoo.eurekaclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@RestController
public class EurekaClientController { @Value("${server.port}")
private String port; @RequestMapping("/info")
public String syaHello(HttpServletRequest request) {
String message = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getServletPath();
return message;
} }

1.6 启动eureka-client服务

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

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

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

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

彩蛋

eureka client集群的搭建

1.1 配置本地host文件

127.0.0.1		eureka1.client.com
127.0.0.1 eureka2.client.com
127.0.0.1 eureka3.client.com

1.2 增加application.yml配置文件

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

  • application.yml
spring:
application:
name: eureka-client
server:
port: 8801 eureka:
instance:
hostname: eureka1.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
  • application-client1.yml
spring:
application:
name: eureka-client
server:
port: 8802 eureka:
instance:
hostname: eureka2.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
  • application-client2.yml
spring:
application:
name: eureka-client
server:
port: 8803 eureka:
instance:
hostname: eureka3.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
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. Quartz系列(一):基础介绍

    新建一个.NET Core控制台项目,NuGet引用Quartz引用. class Program { static void Main(string[] args) { var task = Tas ...

  2. 使用 .NET Core 3.0 的 AssemblyLoadContext 实现插件热加载

    一般情况下,一个 .NET 程序集加载到程序中以后,它的类型信息以及原生代码等数据会一直保留在内存中,.NET 运行时无法回收它们,如果我们要实现插件热加载 (例如 Razor 或 Aspx 模版的热 ...

  3. vue 引入 fontawesome 报错 Could not find one or more icon(s) 解决

    在 vue 项目中引用 fontawesome , 按照官方说明如下步骤操作 1. 终端中执行 $ npm i --save @fortawesome/fontawesome-svg-core $ n ...

  4. python win32com 读取带密码的excel

    之前用到的win32com读取带密码excel的相关内容,今天刚好准备整理下,突然发现方法又不灵了. 以下为错误示范: # 已知excel密码去除 def del_password(filename, ...

  5. 为什么用Markdown,而不用Word?

    写博客.写文章比较多的人都知道 Markdown 是什么. Markdown 是一种轻量级标记语言,创始人为 John Gruber.它允许人们「使用易读易写的纯文本格式编写文档,然后转换成有效的 X ...

  6. 使用jsr303实现数据校验

    除了前端的js验证,服务端也可加入数据验证,springmvc中有两种方式可以验证输入 利用spring自带的验证框架 利用jsr303实现 jsr303实现数据校验 jsr303是java为bean ...

  7. IntelliJ IDEA下载安装及破解(100%成功)教程

    原文链接:http://www.studyshare.cn/software/details/1182/0 一.下载 1.IntelliJ IDEA下载 网盘下载:https://pan.baidu. ...

  8. 初探内核之《Linux内核设计与实现》笔记下

    定时器和时间管理 系统中有很多与时间相关的程序(比如定期执行的任务,某一时间执行的任务,推迟一段时间执行的任务),因此,时间的管理对于linux来说非常重要. 主要内容: 系统时间 定时器 定时器相关 ...

  9. QR 码详解(下)

    快速响应矩阵码(下) 书接上回,继续下半场. 纠错码 QR 码采用纠错算法生成一系列纠错码字,添加在数据码字序列之后,使得符号可以在遇到损坏时可以恢复.这就是为什么二维码即使有残缺也可以扫出来.没有残 ...

  10. python编程基础之五

    转义字符:就是不方便从键盘之间输出,或者是原字符有特殊含义的一些字符, 下面列举几个常用的转义字符 \',\",\''',\""",\\,都是表示原字符的意思, ...