SpringClud一站式学习之Eureka服务治理(二)
SpringClud一站式学习之Eureka服务治理
- 引言
- 1. 搭建Eureka Server
- 1.1. 添加Eureka Server依赖
- 1.2. 添加 Eureka Server注解
- 1.3. 配置Eureka Server
- 1.4. 运行Eureka Server
- 2. 搭建Eureka Client 服务提供者
- 2.1. 添加依赖
- 2.2. 添加注解
- 2.3. 配置Eureka Client
- 2.4. 启动服务
- 3. 搭建Eureka Client 消费者
- 3.1. 添加依赖
- 3.2. 配置
- 3.3. 服务消费者获取服务提供者信息
- 3.4. 启动服务消费者
- 4 搭建集群
引言
Eureka是Netflix开源的一款服务发现框架,它主要用于在微服务架构中定位服务,是服务之间调用的枢纽和关键。在微服务架构中,服务实例可能会动态地增加或减少,Eureka提供了服务注册和发现的功能,使得服务实例可以相互发现对方,而不需要硬编码服务地址。
Eureka的两个主要组件:
Eureka Server:服务注册中心,用于维护各个微服务实例的注册信息,各个微服务实例在启动时会向Eureka Server注册自己的信息,并定期发送心跳以表明自己的存活状态。当服务实例关闭或者网络问题导致心跳丢失时,Eureka Server会从注册信息中移除该实例。
Eureka Client:服务提供者和消费者都会使用Eureka Client来与Eureka Server进行通信。服务提供者在启动时会向Eureka Server注册自己的服务地址和端口,服务消费者通过Eureka Server查询服务提供者的地址,然后直接调用。

Eureka是Spring Cloud体系中的核心组件之一,它与Spring Cloud的其他组件(如Ribbon、Feign、Hystrix等)协同工作,提供了一套完整的微服务解决方案。通过Eureka,开发者可以更容易地实现服务的注册与发现,从而构建和管理复杂的微服务系统。
1. 搭建Eureka Server
1.1. 添加Eureka Server依赖
新建立springboot工程,添加Eureka Server依赖,
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>eurakaservertrue</artifactId><version>0.0.1-SNAPSHOT</version><name>eurakaservertrue</name><description>Demo project for Spring Boot</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>RELEASE</version><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
1.2. 添加 Eureka Server注解
在启动类上添加注解,表明为Eureka Server
@EnableEurekaServer
1.3. 配置Eureka Server
spring.application.name=eurakaservertrue
server.port=8761
#从注册表中获取信息
eureka.client.fetch-registry=false
#允许自己注册到服务中
eureka.client.register-with-eureka=false
1.4. 运行Eureka Server

访问http://localhost:8761/ 出现此界面表示已经搭建好了服务

2. 搭建Eureka Client 服务提供者
2.1. 添加依赖
新建立一个spirngboot工程,并添加Eureka Client依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>erakaclientprovide</artifactId><version>0.0.1-SNAPSHOT</version><name>erakaclientprovide</name><description>Demo project for Spring Boot</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><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><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>RELEASE</version><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.2. 添加注解
@EnableDiscoveryClient
2.3. 配置Eureka Client
spring.application.name=erakaclientprovide
server.port=8081eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
#自定义实例ID
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
2.4. 启动服务

3. 搭建Eureka Client 消费者
3.1. 添加依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>erakaclientconsumer</artifactId><version>0.0.1-SNAPSHOT</version><name>erakaclientconsumer</name><description>Demo project for Spring Boot</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><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><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>RELEASE</version><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
3.2. 配置
spring.application.name=erakaclientconsumer
server.port=8082eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
#自定义实例ID
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
3.3. 服务消费者获取服务提供者信息
编写controller 方法获取
package com.example.erakaclientconsumer.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;
import java.util.stream.Collectors;/*** @Auther: lifang* @Description* @Date: $ $* $* $**/
@RestController
public class EurekeController {@Autowiredprivate DiscoveryClient discoveryClient;//获取Eureka服务中所有的提供者信息@GetMapping("/instance")public List<ServiceInstance> getApplication(){List<ServiceInstance> instances=discoveryClient.getServices().stream().map(sid->discoveryClient.getInstances(sid)).collect(Collectors.toList()).stream().flatMap(list->list.stream()).collect(Collectors.toList());return instances;}
}
3.4. 启动服务消费者
访问 http://localhost:8761/

访问 http://localhost:8082/instance

[{"metadata": {"management.port": "8082"},"secure": false,"uri": "http://localhost:8082","instanceInfo": {"instanceId": "erakaclientconsumer:192.168.10.1��8082","app": "ERAKACLIENTCONSUMER","appGroupName": null,"ipAddr": "192.168.10.1","sid": "na","homePageUrl": "http://localhost:8082/","statusPageUrl": "http://localhost:8082/actuator/info","healthCheckUrl": "http://localhost:8082/actuator/health","secureHealthCheckUrl": null,"vipAddress": "erakaclientconsumer","secureVipAddress": "erakaclientconsumer","countryId": 1,"dataCenterInfo": {"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo","name": "MyOwn"},"hostName": "localhost","status": "UP","overriddenStatus": "UNKNOWN","leaseInfo": {"renewalIntervalInSecs": 30,"durationInSecs": 90,"registrationTimestamp": 1731075179095,"lastRenewalTimestamp": 1731075329123,"evictionTimestamp": 0,"serviceUpTimestamp": 1731075179095},"isCoordinatingDiscoveryServer": false,"metadata": {"management.port": "8082"},"lastUpdatedTimestamp": 1731075179095,"lastDirtyTimestamp": 1731075179034,"actionType": "ADDED","asgName": null},]
4 搭建集群
集群多个节点组成的服务器群,一个Eureka Server服务是一个节点,不同节点之间服务信息是相互复制的,对于Eureka Server不同的节点代码是一样的,不同的是地址不同,因此我们通过配置不同环境的方法启动2个Eureka Server,创建 application-A.properties 和application-B.properties,在application.properties,指定当前的配置文件,启动后更改下一个节点的配置文件;
server.port=8762
eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
server.port=8761
eureka.client.service-url.defaultZone: http://localhost:8762/eureka/
spring.profiles.active=B

相关文章:
SpringClud一站式学习之Eureka服务治理(二)
SpringClud一站式学习之Eureka服务治理 引言1. 搭建Eureka Server1.1. 添加Eureka Server依赖1.2. 添加 Eureka Server注解1.3. 配置Eureka Server1.4. 运行Eureka Server 2. 搭建Eureka Client 服务提供者2.1. 添加依赖2.2. 添加注解2.3. 配置Eureka Client2.4. 启动服务 3. 搭…...
空间解析几何【上】
文章目录 两向量共线&三向量共面线段定比分点内积&外积&混合积内积(点积)外积(叉积)几何性质混合积轮换对称性对换改变一次符号线性性质几何性质球面方程特点空间平面参数方程行列式方程(点位式)向量式方程三点式方程行列式方程点法式一般式截距式法式方程离…...
Python 获取PDF的各种页面信息(页数、页面尺寸、旋转角度、页面方向等)
目录 安装所需库 Python获取PDF页数 Python获取PDF页面尺寸 Python获取PDF页面旋转角度 Python获取PDF页面方向 Python获取PDF页面标签 Python获取PDF页面边框信息 了解PDF页面信息对于有效处理、编辑和管理PDF文件至关重要。PDF文件通常包含多个页面,每个页…...
独孤思维:曾经副业赚大钱的人,怎么不见了
01 总有一双眼睛默默关注你。 别以为自己每天做项目,日更文章,没人看。 总会有人默默观察你。 看你能坚持多久,看多段时间,你是不是还在。 今天上午,有个2年前认识的副业同行,今天突然跟我发消息。 说…...
OpenGL 异常处理-glCreateShader失败
【1】glCreateShader创建顶点着色器时候报错,如下 【2】原因分析 初始化失败,你使用一个扩extension loader library来访问现代OpenGL,当需要初始化它时,加载器需要一个当前的上下文来加载 【3】解决办法 GLenum glew_err gle…...
【el-pagination的使用及修改分页组件的整体大小修改默认样式的宽度详细教程】
今天遇到个bug,使用element-puls中的分页的时候,长度会超出盒子,今天教大家如何修改el-pagination的宽度,以及修改分页组件的整体大小 直接修改 style"width: 100%; margin-top: 10px"不生效 控制台修改el-pagination…...
Uniapp的学习
uniapp的内容和vue网页开发会有很多区别,但是都是基于vue开发的,大多数业务还是在vue打交道,但是这些uniapp的特殊的知识点也是要掌握好的。 基本配置 创建uniapp项目 npx degit dcloudio/uni-preset-vue#vite-ts 项目名 :用于…...
C#-万物之父object、装箱拆箱
万物之父:object 基于里氏替换原则,可以用object容器装载一切类型的变量。可以用来表示不确定类型,作为函数参数类型 object是所有类型的基类 装箱拆箱 用object存值类型(装箱)→ 把值类型用引用类型存储,…...
AI大模型重塑软件开发流程:从自动化编码到智能协作的未来展望
目录 1. 引言:AI大模型的崛起与软件开发的变革 1.1 AI大模型的兴起与发展背景 1.2 软件开发的现状与痛点 1.3 AI大模型如何解决这些问题 2. AI大模型的工作原理与技术背景 2.1 什么是AI大模型? 2.2 深度学习与自然语言处理技术的演变 2.3 大模型…...
HTB:GreenHorn[WriteUP]
目录 连接至HTB服务器并启动靶机 使用nmap对靶机TCP端口进行开放扫描 再次使用nmap对这三个端口进行脚本、服务扫描 尝试先通过curl访问靶机80端口 将靶机IP与该域名写入hosts使DNS本地解析 使用浏览器访问greenhorn.htb 使用Wappalyzer插件查看该页面技术栈 尝试在sea…...
SelfAttention在Ascend上的实现
1 SelfAttention是什么? Self-Attention(自注意力)机制是深度学习领域的一种重要技术,尤其在自然语言处理(NLP)任务中得到广泛应用。它是 Transformer 架构的核心组成部分之一,由 Vaswani 等人…...
C#设计模式
文章目录 项目地址一、开放封闭原则1.1 不好的版本1.2 将BankProcess的实现改为接口1.3 修改BankStuff类和IBankClient类二、依赖倒置原则2.1 高层不应该依赖于低层模块2.1.1 不好的例子2.1.2 修改:将各个国家的歌曲抽象2.2 抽象不应该依于细节2.2.1 不同的人开不同的车(接口…...
仪表板展示|DataEase看中国:历年双十一电商销售数据分析
背景介绍 2024年“双十一”购物季正在火热进行中。自2009年首次推出至今,“双十一”已经成为中国乃至全球最大的购物狂欢节,并且延伸到了全球范围内的电子商务平台。随着人们消费水平的提升以及电子商务的普及,线上销售模式也逐渐呈现多元化…...
急着骂华为?我劝你别急
文 | AUTO芯球 作者 | 雷慢 赛力斯这下怒了! 要对那些华为黑、问界黑出手了, 就在这几天,赛力斯起诉了一批蓄意抹黑、散步虚假信息的人。 起因是什么,听我慢慢说, 今年7月,佛山一辆问界M7发生交通事故…...
虚拟机linux7.9下安装mysql
1.MySQL官网下载安装包: MySQL :: Download MySQL Community Server https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz 2.解压文件: #tar xvzf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz 3.移动文件&#…...
【Linux】一篇文章轻松搞懂基本指令
本篇所有展示代码均是在超级用户的权限下进行的,如果不是超级用户并且一些命令执行的和我的不太一样,那么可以试着在对应命令前暂且加上sudo,我们在下一篇会讲权限问题,到时候再转换为普通用户。 本篇展示的内容是基于CentOs进行…...
深入浅出理解Spring和SpringBoot,剖析自动配置源码
文章目录 1.Spring2.SpringBoot3.小结 1.摘要 本文旨在带大家理解Spring框架和SpringBoot框架最为核心的部分,自Spring和SpringBoot问世以来,给Web开发掀起了巨大的浪潮,极大的缩短项目开发周期,下面将带大家分析Spring和SpringBo…...
Spring配置文件初始化加载(一)
1.枚举 public enum TestEnum {type_01("01", "zeroTest01ServiceImpl"),type_02("02", "zeroTest02ServiceImpl"),type_03("03", "zeroTest03ServiceImpl");private String type;private String pathClass; } …...
正则表达式 - 简介
正则表达式 - 简介 正则表达式(Regular Expression,简称Regex)是一种用于处理字符串的强大工具,它允许用户通过特定的模式(pattern)来搜索、匹配、查找和替换文本中的数据。正则表达式广泛应用于文本编辑器…...
【电机控制器】STC8H1K芯片——ADC电压采集
【电机控制器】STC8H1K芯片——ADC电压采集 文章目录 [TOC](文章目录) 前言一、ADC1.ADC初始化1.ADC_CONTR2.ADCCFG3.ADCTIM4.代码 2.ADC读取1.ADC_RES、ADC_RESL2.代码 3.VREF电压读取——MCU工作电压1.MCU工作电压计算公式2.代码 4.ADC被转换通道的输入电压读取1.ADC被转换通…...
深入浅出Asp.Net Core MVC应用开发系列-AspNetCore中的日志记录
ASP.NET Core 是一个跨平台的开源框架,用于在 Windows、macOS 或 Linux 上生成基于云的新式 Web 应用。 ASP.NET Core 中的日志记录 .NET 通过 ILogger API 支持高性能结构化日志记录,以帮助监视应用程序行为和诊断问题。 可以通过配置不同的记录提供程…...
日语AI面试高效通关秘籍:专业解读与青柚面试智能助攻
在如今就业市场竞争日益激烈的背景下,越来越多的求职者将目光投向了日本及中日双语岗位。但是,一场日语面试往往让许多人感到步履维艰。你是否也曾因为面试官抛出的“刁钻问题”而心生畏惧?面对生疏的日语交流环境,即便提前恶补了…...
C++_核心编程_多态案例二-制作饮品
#include <iostream> #include <string> using namespace std;/*制作饮品的大致流程为:煮水 - 冲泡 - 倒入杯中 - 加入辅料 利用多态技术实现本案例,提供抽象制作饮品基类,提供子类制作咖啡和茶叶*//*基类*/ class AbstractDr…...
【解密LSTM、GRU如何解决传统RNN梯度消失问题】
解密LSTM与GRU:如何让RNN变得更聪明? 在深度学习的世界里,循环神经网络(RNN)以其卓越的序列数据处理能力广泛应用于自然语言处理、时间序列预测等领域。然而,传统RNN存在的一个严重问题——梯度消失&#…...
ServerTrust 并非唯一
NSURLAuthenticationMethodServerTrust 只是 authenticationMethod 的冰山一角 要理解 NSURLAuthenticationMethodServerTrust, 首先要明白它只是 authenticationMethod 的选项之一, 并非唯一 1 先厘清概念 点说明authenticationMethodURLAuthenticationChallenge.protectionS…...
Razor编程中@Html的方法使用大全
文章目录 1. 基础HTML辅助方法1.1 Html.ActionLink()1.2 Html.RouteLink()1.3 Html.Display() / Html.DisplayFor()1.4 Html.Editor() / Html.EditorFor()1.5 Html.Label() / Html.LabelFor()1.6 Html.TextBox() / Html.TextBoxFor() 2. 表单相关辅助方法2.1 Html.BeginForm() …...
安卓基础(Java 和 Gradle 版本)
1. 设置项目的 JDK 版本 方法1:通过 Project Structure File → Project Structure... (或按 CtrlAltShiftS) 左侧选择 SDK Location 在 Gradle Settings 部分,设置 Gradle JDK 方法2:通过 Settings File → Settings... (或 CtrlAltS)…...
Leetcode33( 搜索旋转排序数组)
题目表述 整数数组 nums 按升序排列,数组中的值 互不相同 。 在传递给函数之前,nums 在预先未知的某个下标 k(0 < k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k1], …, nums[n-1], nums[0], nu…...
算术操作符与类型转换:从基础到精通
目录 前言:从基础到实践——探索运算符与类型转换的奥秘 算术操作符超级详解 算术操作符:、-、*、/、% 赋值操作符:和复合赋值 单⽬操作符:、--、、- 前言:从基础到实践——探索运算符与类型转换的奥秘 在先前的文…...
Linux-进程间的通信
1、IPC: Inter Process Communication(进程间通信): 由于每个进程在操作系统中有独立的地址空间,它们不能像线程那样直接访问彼此的内存,所以必须通过某种方式进行通信。 常见的 IPC 方式包括&#…...
