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

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避