本文采用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. Apache和Tomcat 配置负载均衡(mod-proxy方式)-无session共享、无粘性session

    转:https://blog.csdn.net/wangjunjun2008/article/details/38268483 mod-proxy方式实现负载均衡是利用了Apache 2.x版本自带的 ...

  2. C语言入门-数组

    今天十月一日,上午看阅兵激情澎湃,但是下午还是要继续写C语言,前面的这块很简单 int number[100]; scanf("%d" , &number[i]); 一.定 ...

  3. java+maven+jenkins+svn构建

    操作参照:https://blog.csdn.net/qq_34977342/article/details/82346915 1.创建一个自由风格的项目,起名字 2.设置构建项目最大保存数量,与天数 ...

  4. JS多线程WebWorker

    JS多线程WebWorker 一,介绍与需求 1.1,介绍 Web Worker可以为JavaScript创建多线程,且Web Worker 是运行在后台的 JavaScript,独立于其他脚本,不会 ...

  5. 常用Form表单正则表达式

    前端常用form表单提交,校验正则表达式奉上!/** * 邮箱 * @param {*} s */ export function isEmail (s) { return /^([a-zA-Z0-9 ...

  6. vue运行报错webpack-dev-server: command not found

    翻译过来就是: 'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序 解决方法: 然后总结下成功的步骤: 1. 直接在项目目录下: cnpm install npm run ...

  7. js常用正则整理

    个人博客: http://mcchen.club //校验是否全由数字组成 function isDigit(s) { var patrn=/^[0-9]{1,20}$/; if (!patrn.ex ...

  8. 网页布局——grid语法属性详解

    grid目前兼容性目前还可以,主流浏览器对它的支持力度很大,ie9,10宣布它未来不久会对它有很好的支持,目前则需要使用过时的语法.我相信不久的将来grid将成为每一个前端工作人员必备的布局技能. 属 ...

  9. CSS3属性—— line-clamp控制文本行数

    说明: 限制在一个块元素显示的文本的行数. -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中. 为 ...

  10. MyBatis 开发手册

    这一遍看Mybatis的原因是怀念一下去年的 10月24号我写自己第一个项目时使用全配置文件版本的MyBatis,那时我们三个人刚刚大二,说实话,当时还是觉得MyBatis挺难玩的,但是今年再看最新版 ...