SpringCloud 服务注册与发现Eureka Hoxton版本

2023-10-18 02:38

本文主要是介绍SpringCloud 服务注册与发现Eureka Hoxton版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring Cloud Netflix简介:Spring Cloud Netflix是对Netflix OSS组件的集成,主要模块包括服务治理Eureka,断路器Hystrix,客户端负载均衡Ribbon,声明式REST客户端Feign,路由网关Zuul等。

Spring Cloud Eureka简介:Spring Cloud Eureka是Spring Cloud Netflix子项目的核心组件之一,它实现了服务治理的功能。Spring Cloud Eureka提供服务端与客户端,服务端即是Eureka服务注册中心,客户端完成微服务向Eureka服务的注册与发现。

在通过Spring Cloud Eureka实现服务治理之前,需要确定SpringCloud与SpringBoot的版本对应关系,从SpringCloud官网截取的SpringCloud与SpringBoot的版本对应关系如下:
SpringBoot和SpringCloud的版本对应关系
下面通过Spring Cloud Eureka实现服务治理,其中SpringBoot使用的2.2.2.RELEASE版本,SpringCloud使用的Hoxton.SR1版本。

一、创建Eureka注册中心

通过Maven新建一个名为spring-cloud-netflix-eureka-server的项目。

1.编写pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.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"><modelVersion>4.0.0</modelVersion><groupId>com.rtxtitanv</groupId><artifactId>spring-cloud-netflix-eureka-server</artifactId><version>1.0.0</version><packaging>jar</packaging><name>spring-cloud-netflix-eureka-server</name><description>eureka server</description><parent><!-- SpringBoot 起步依赖 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR1</spring-cloud.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- Spring Cloud Eureka Server 起步依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- SpringSecurity 起步依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2.主启动类

package com.rtxtitanv;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.EurekaServerApplication* @description eureka server 主启动类* @date 2020/2/13 11:34*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}

@EnableEurekaServer:表示启动Eureka注册中心。

3.编写配置文件

application.yml中进行如下配置:

server:port: ${PORT:8080}spring:application:name: eureka-serversecurity:# 配置spring security登录用户名和密码,给Eureka注册中心添加认证user:name: rtxtitanvpassword: rtxtitanveureka:instance:# 该服务实例主机名hostname: ${EUREKA_HOSTNAME:localhost}# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ipinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒lease-renewal-interval-in-seconds: 20# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒lease-expiration-duration-in-seconds: 60server:# Eureka自我保护模式设置,true:开启,false:关闭,开发测试环境关闭,生产环境开启enable-self-preservation: false# 清理失效节点的时间间隔,默认60000毫秒eviction-interval-timer-in-ms: 60000client:# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册register-with-eureka: false# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取fetch-registry: falsehealthcheck:# Eureka的健康检查,只能在application.yml中设置,true:开启,false:关闭enabled: trueservice-url:# 配置Eureka注册中心即Eureka服务端的地址,单机状态配置自己的地址defaultZone: ${EUREKA_SERVER:http://rtxtitanv:rtxtitanv@${eureka.instance.hostname}:${server.port}/eureka/}

4.关闭SpringSecurity的CSRF检验

由于这里引入了SpringSecurity来给Eureka注册中心添加认证,默认情况下SpringSecurity启用了CSRF检验,需要手动关闭CSRF检验,代码如下:

package com.rtxtitanv.config;import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.config.CsrfSecurityConfig* @description 关闭security的csrf检验* @date 2020/2/15 12:01*/
@EnableWebSecurity
public class CsrfSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable();super.configure(http);}
}

5.启动注册中心

(1)单节点Eureka服务

启动eureka-server,访问http://localhost:8080,结果见下图,由于Eureka注册中心添加了认证,需要输入用户名和密码。
Eureka注册中心认证
登录认证后访问结果见下图,进入了注册中心界面,说明单节点Eureka注册中心启动成功,此时还没有任何服务注册到Eureka注册中心。
单节点Eureka注册中心

(2)Eureka服务集群

以3个节点为例,这里启动3个Eureka注册中心节点,互相向对方节点注册,首先修改如下配置:

eureka:client:# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册register-with-eureka: true# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取fetch-registry: true

然后在主机hosts文件中添加如下域名信息:

127.0.0.1 eureka-server-01
127.0.0.1 eureka-server-02
127.0.0.1 eureka-server-03

然后在IDEA中创建启动脚本EurekaServer01EurekaServer02EurekaServer03
EurekaServer01
EurekaServer01
VM options

-DPORT=8001
-DEUREKA_HOSTNAME=eureka-server-01
-DEUREKA_SERVER=http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/

EurekaServer02
EurekaServer02
VM options

-DPORT=8002
-DEUREKA_HOSTNAME=eureka-server-02
-DEUREKA_SERVER=http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/

EurekaServer03
EurekaServer03
VM options

-DPORT=8003
-DEUREKA_HOSTNAME=eureka-server-03
-DEUREKA_SERVER=http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/

然后分别启动三个服务,启动后控制台会输出相关日志,UP表示启动成功状态:

2020-02-15 13:41:58.358  INFO 9456 --- [      Thread-27] c.n.e.registry.AbstractInstanceRegistry  : Registered instance EUREKA-SERVER/eureka-server:10.0.0.11:8003 with status UP (replication=true)
2020-02-15 13:41:58.359  INFO 9456 --- [      Thread-27] c.n.e.registry.AbstractInstanceRegistry  : Registered instance EUREKA-SERVER/eureka-server:10.0.0.11:8002 with status UP (replication=true)
2020-02-15 13:41:58.359  INFO 9456 --- [      Thread-27] c.n.e.registry.AbstractInstanceRegistry  : Registered instance EUREKA-SERVER/eureka-server:10.0.0.11:8001 with status UP (replication=true)
2020-02-15 13:41:58.359  INFO 9456 --- [      Thread-27] c.n.e.r.PeerAwareInstanceRegistryImpl    : Got 3 instances from neighboring DS node

分别访问三个Eureka注册中心节点:

  • http://eureka-server-01:8001/
  • http://eureka-server-02:8002/
  • http://eureka-server-03:8003/

其中访问eureka-server-01节点,登录后访问结果见下图并且查看另两个节点的界面也类似,说明Eureka注册中心集群搭建成功。
eureka-server-01节点界面
以上是在本机开发工具IDEA模拟Eureka集群的测试,在生产环境上都是部署在LInux服务器上,传统的部署方式就是先将Eureka服务打jar包,上传到Linux服务器启动运行。这里打的jar包名为spring-cloud-netflix-eureka-server-1.0.0.jar,用一台Linux服务器来测试,实际部署在多台服务器上方法类似,首先在Linux服务器的hosts文件中添加域名信息,执行以下命令:

vim /etc/hosts

hosts中添加域名信息:

127.0.0.1 eureka-server-01
127.0.0.1 eureka-server-02
127.0.0.1 eureka-server-03

将jar包上传到Linux服务器,在jar包所在目录下执行以下命令:

java -jar spring-cloud-netflix-eureka-server-1.0.0.jar --server.port=8001 --eureka.instance.hostname=eureka-server-01 --eureka.client.service-url.defaultZone=http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
java -jar spring-cloud-netflix-eureka-server-1.0.0.jar --server.port=8002 --eureka.instance.hostname=eureka-server-02 --eureka.client.service-url.defaultZone=http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
java -jar spring-cloud-netflix-eureka-server-1.0.0.jar --server.port=8003 --eureka.instance.hostname=eureka-server-03 --eureka.client.service-url.defaultZone=http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/

在客户端主机的hosts文件中添加如下域名信息:

服务器ip eureka-server-01
服务器ip eureka-server-02
服务器ip eureka-server-03

分别访问三个Eureka注册中心节点:

  • http://eureka-server-01:8001/
  • http://eureka-server-02:8002/
  • http://eureka-server-03:8003/

其中访问eureka-server-03节点,登录后访问结果见下图并且查看另两个节点的界面也类似,说明Eureka注册中心集群搭建成功。
eureka-server-03节点界面

二、创建Eureka客户端

通过Maven新建一个名为spring-cloud-netflix-eureka-client的项目。

1.编写pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.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"><modelVersion>4.0.0</modelVersion><groupId>com.rtxtitanv</groupId><artifactId>spring-cloud-netflix-eureka-client</artifactId><version>1.0.0</version><packaging>jar</packaging><name>spring-cloud-netflix-eureka-client</name><description>eureka client</description><parent><!-- SpringBoot 起步依赖 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR1</spring-cloud.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- Spring Cloud Eureka Client 起步依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2.主启动类

package com.rtxtitanv;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.EurekaClientApplication* @description eureka client 主启动类* @date 2020/2/13 20:22*/
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaClientApplication.class, args);}
}

@EnableEurekaClient:表示该服务为一个Eureka客户端。

3.编写配置文件

application.yml中进行如下配置:

server:port: ${PORT:8090}spring:application:name: eureka-clienteureka:client:# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册register-with-eureka: true# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取fetch-registry: true# 配置Eureka注册中心即Eureka服务端的地址,集群地址以,隔开service-url:defaultZone: http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/# 从Eureka服务器获取注册表信息的间隔时间,单位秒,默认30秒registry-fetch-interval-seconds: 20# 更新实例信息的变化到Eureka服务端的间隔时间,单位秒,默认30秒instance-info-replication-interval-seconds: 20# 初始化实例信息到Eureka服务端的间隔时间,单位秒,默认40秒initial-instance-info-replication-interval-seconds: 30# 询问Eureka Server信息变化的时间间隔,单位秒,默认300秒eureka-service-url-poll-interval-seconds: 200# 读取Eureka Server的超时时间,单位秒,默认5秒eureka-server-connect-timeout-seconds: 10# 连接Eureka Server的超时时间,单位秒,默认8秒eureka-server-read-timeout-seconds: 15instance:# 将ip地址注册到Eureka注册中心prefer-ip-address: true# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ipinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒lease-renewal-interval-in-seconds: 20# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒lease-expiration-duration-in-seconds: 60

4.创建测试用的Controller

package com.rtxtitanv.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.controller.EurekaClientController* @description EurekaClientController* @date 2020/2/13 21:30*/
@RestController
public class EurekaClientController {@Value("${server.port}")private String port;@Value("${spring.cloud.client.ip-address}")private String ip;@GetMapping("/home")public String home() {return "ip:" + ip + ",port:" + port;}
}

5.启动Eureka客户端

在主机hosts文件中添加如下域名信息:

127.0.0.1 eureka-client-01
127.0.0.1 eureka-client-02
127.0.0.1 eureka-client-03

通过IDEA启动脚本启动3个Eureka客户端节点,分别访问三个Eureka注册中心节点,其中访问eureka-server-01节点,登录后访问结果见下图并且查看另两个节点的界面也类似,说明3个Eureka客户端节点均成功注册到Eureka注册中心集群。
eureka-server-01节点界面
分别访问以下三个链接:

  • http://eureka-client-01:9001/home
  • http://eureka-client-02:9002/home
  • http://eureka-client-03:9003/home

访问第一个链接的结果见下图,剩下两个链接的访问结果省略。
第一个链接结果
下面通过URL的方式获取服务的注册信息,这里以eureka-server-01为例,访问
http://eureka-server-01:8001/eureka/apps,结果见下图:
获取eureka-server-01中的注册列表
获取其中一个服务EUREKA-CLIENT的注册信息,访问
http://eureka-server-01:8001/eureka/apps/EUREKA-CLIENT,结果见下图:
EUREKA-CLIENT的注册信息

代码示例

  • Github:
    https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-eureka-server
    https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-eureka-client
  • Gitee:
    https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-eureka-server
    https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-eureka-client

这篇关于SpringCloud 服务注册与发现Eureka Hoxton版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/229629

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2