SpringCloud Eureka实现服务注册与发现

网友投稿 216 2023-07-12

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

是为了统一版本

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.huanzi.qch.eureka

eureka-server

0.0.1-SNAPSHOT&ltnOuNwSG;/version>

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.huanzi.qch.eureka

eureka-server

0.0.1-SNAPSHOT&ltnOuNwSG;/version>

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://lohttp://calhost:${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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:平衡二叉树的左右旋以及双旋转的图文详解
下一篇:MapReduce核心思想图文详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~