栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud Eureka实现服务注册与发现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud Eureka实现服务注册与发现

前言

Eureka是一种基于REST(具像状态传输)的服务,主要用于AWS云中定位服务,以实现中间层服务器的负载平衡和故障转移。本文记录一个简单的服务注册与发现实例。

GitHub地址:https://github.com/Netflix/eureka

官网文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.RC2/single/spring-cloud-netflix.html

Eureka-Server

服务注册中心

新建一个Maven项目,并删除src文件夹,保留pom.xml,作为parent,当然也可以不用

在parent里面新建一个springboot项目的Module,Eureka Server

项目结构

maven引入jar

parent的 pom.xml

是为了统一版本



 4.0.0

 cn.huanzi.qch
 parent
 1.0.0
 pom

 
  org.springframework.boot
  spring-boot-starter-parent
  2.1.1.RELEASE
   
 

 
 
  UTF-8
  1.8

  
  UTF-8
  UTF-8

  
  UTF-8
  5.1.34

  
  5.22
  1.4.7.RELEASE

  
  3.4.3.1
  3.1.12

  
  2.0.3
  2.3.2
  2.2.1
  2.4
  2.1.1
  2.3.2

  Greenwich.RC1

 

 
  
   
    org.springframework.cloud
    spring-cloud-dependencies
    ${spring-cloud.version}
    pom
    import
   
  
 

 
  
   spring-milestones
   Spring Milestones
   https://repo.spring.io/milestone
  
 

eureka-server的 pom.xml

继承parent



 4.0.0
 cn.huanzi.qch.eureka
 eureka-server
 0.0.1-SNAPSHOT
 eureka-server
 eureka 注册中心

 
 
  cn.huanzi.qch
  parent
  1.0.0
 

 
  
  
   org.springframework.cloud
   spring-cloud-starter-netflix-eureka-server
  

  
  
   org.springframework.boot
   spring-boot-starter-test
   test
  
 

 
 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
  
 

配置文件

server.port=1111
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

启动类

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

 }

}

启动应用,访问 http://localhost:1111/,注册中心启动成功,此时有0个服务

Eureka-Client

服务发现,可以新建一个springboot项目,我们直接使用之前写的一个myspringboot项目

maven中引入相关jar

 
  
   org.springframework.cloud
   spring-cloud-starter-netflix-eureka-client
  

如果没有repositories还需要加入


  
   
    org.springframework.cloud
    spring-cloud-dependencies
    ${spring-cloud.version}
    pom
    import
   
  
 
 
  
   spring-milestones
   Spring Milestones
   https://repo.spring.io/milestone
  
 

配置文件加入注册中心的地址,也就是Eureka-Server的配置文件里面eureka.client.serviceUrl.defaultZone

#eureka
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

启动类添加注解

@EnableEurekaClient
@SpringBootApplication
@RestController
public class MyspringbootApplication{

 public static void main(String[] args) {
  SpringApplication.run(MyspringbootApplication.class, args);
 }

 
 @GetMapping("/index")
 public String index(){
  return "hello springboot!";
 }

}

启动客户端服务

成功在注册中心注册成功,可以对外提供服务

健康检查

默认情况下,Eureka使用客户端心跳来确定客户端是否启动。除非另有说明,否则发现客户机不会根据Spring引导执行器传播应用程序的当前健康检查状态。因此,在成功注册后,Eureka总是宣布应用程序处于“UP”状态。可以通过启用Eureka健康检查来更改此行为,比如我现在将myspringboot服务停掉,但注册中心依旧显示为UP,这样就会造成我服务已经挂掉了,但注册中心依然会认为这个实例还活着。

Eureka-Client

#健康检查(需要spring-boot-starter-actuator依赖)
eureka.client.healthcheck.enabled=true
# 续约更新时间间隔(单位秒,默认30秒)
eureka.instance.lease-renewal-interval-in-seconds=10
# 续约到期时间(单位秒,默认90秒)
eureka.instance.lease-expiration-duration-in-seconds=10
 
  
   org.springframework.boot
   spring-boot-starter-actuator
  

Eureka-Server

#设为false,关闭自我保护
eureka.server.enable-self-preservation=false
#清理间隔(单位毫秒,默认是60*1000)
eureka.server.eviction-interval-timer-in-ms=10000

健康检查,注册中心将死去的服务剔除

总结

Eureka-Server

1、引入的是spring-cloud-starter-netflix-eureka-server,使用的是@EnableEurekaServer

Eureka-Client

1、引入的是spring-cloud-starter-netflix-eureka-client,使用的是@EnableEurekaClient

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139220.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号