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被转换通…...
谷歌浏览器插件
项目中有时候会用到插件 sync-cookie-extension1.0.0:开发环境同步测试 cookie 至 localhost,便于本地请求服务携带 cookie 参考地址:https://juejin.cn/post/7139354571712757767 里面有源码下载下来,加在到扩展即可使用FeHelp…...
逻辑回归:给不确定性划界的分类大师
想象你是一名医生。面对患者的检查报告(肿瘤大小、血液指标),你需要做出一个**决定性判断**:恶性还是良性?这种“非黑即白”的抉择,正是**逻辑回归(Logistic Regression)** 的战场&a…...
在HarmonyOS ArkTS ArkUI-X 5.0及以上版本中,手势开发全攻略:
在 HarmonyOS 应用开发中,手势交互是连接用户与设备的核心纽带。ArkTS 框架提供了丰富的手势处理能力,既支持点击、长按、拖拽等基础单一手势的精细控制,也能通过多种绑定策略解决父子组件的手势竞争问题。本文将结合官方开发文档,…...

8k长序列建模,蛋白质语言模型Prot42仅利用目标蛋白序列即可生成高亲和力结合剂
蛋白质结合剂(如抗体、抑制肽)在疾病诊断、成像分析及靶向药物递送等关键场景中发挥着不可替代的作用。传统上,高特异性蛋白质结合剂的开发高度依赖噬菌体展示、定向进化等实验技术,但这类方法普遍面临资源消耗巨大、研发周期冗长…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明
AI 领域的快速发展正在催生一个新时代,智能代理(agents)不再是孤立的个体,而是能够像一个数字团队一样协作。然而,当前 AI 生态系统的碎片化阻碍了这一愿景的实现,导致了“AI 巴别塔问题”——不同代理之间…...
TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案
一、TRS收益互换的本质与业务逻辑 (一)概念解析 TRS(Total Return Swap)收益互换是一种金融衍生工具,指交易双方约定在未来一定期限内,基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...
#Uniapp篇:chrome调试unapp适配
chrome调试设备----使用Android模拟机开发调试移动端页面 Chrome://inspect/#devices MuMu模拟器Edge浏览器:Android原生APP嵌入的H5页面元素定位 chrome://inspect/#devices uniapp单位适配 根路径下 postcss.config.js 需要装这些插件 “postcss”: “^8.5.…...
Linux离线(zip方式)安装docker
目录 基础信息操作系统信息docker信息 安装实例安装步骤示例 遇到的问题问题1:修改默认工作路径启动失败问题2 找不到对应组 基础信息 操作系统信息 OS版本:CentOS 7 64位 内核版本:3.10.0 相关命令: uname -rcat /etc/os-rele…...
MySQL 8.0 事务全面讲解
以下是一个结合两次回答的 MySQL 8.0 事务全面讲解,涵盖了事务的核心概念、操作示例、失败回滚、隔离级别、事务性 DDL 和 XA 事务等内容,并修正了查看隔离级别的命令。 MySQL 8.0 事务全面讲解 一、事务的核心概念(ACID) 事务是…...

手机平板能效生态设计指令EU 2023/1670标准解读
手机平板能效生态设计指令EU 2023/1670标准解读 以下是针对欧盟《手机和平板电脑生态设计法规》(EU) 2023/1670 的核心解读,综合法规核心要求、最新修正及企业合规要点: 一、法规背景与目标 生效与强制时间 发布于2023年8月31日(OJ公报&…...