云原生微服务治理 第四章 Spring Cloud Netflix 服务注册/发现组件Eureka

系列文章目录

第一章 Java线程池技术应用
第二章 CountDownLatch和Semaphone的应用
第三章 Spring Cloud 简介
第四章 Spring Cloud Netflix 之 Eureka
在这里插入图片描述


文章目录

  • 系列文章目录
    • @[TOC](文章目录)
  • 前言
  • 1、Eureka 两大组件
  • 2、Eureka 服务注册与发现
  • 3、案例
    • 3.1、创建主工程
      • 3.1.1、主工程pom.xml配置
    • 3.2、创建子公共模块common-api
      • 3.2.1、添加module
      • 3.2.2、pom.xml配置
      • 3.2.3、maven非springboot项目,增加main入口
    • 3.3、创建Eureka注册中心模块eureka-server
      • 3.3.1、配置pom.xml
      • 3.3.2、配置application.yml
      • 3.3.3、启动eureka-server
        • 3.3.3.1、编译eureka-server
        • 3.3.3.2、运行EurekaServerApplication.java文件
    • 3.4、创建用户服务模块user-service![在这里插入图片描述](https://img-blog.csdnimg.cn/e52d0b166629469baa1cd934714c9831.png)
      • 3.4.1、配置pom.xml
      • 3.4.2、配置application.yml
      • 3.4.3、启动user-service
        • 3.4.3.1、编译user-service
        • 3.4.3.2、运行UserApplication.java文件
        • 3.4.3.3、测试
    • 3.5、查看编译后的包
  • 小结

前言

今天我们讲解Spring Cloud微服务的第一代实现:Spring Cloud Netflix
Eureka 是 Netflix 公司开发的一款开源的服务注册与发现组件。
Spring Cloud 使用 Spring Boot 思想为 Eureka 增加了自动化配置,开发人员只需要引入相关依赖和注解,就能将 Spring Boot 构建的微服务轻松地与 Eureka 进行整合。

附简单的Netflix微服务架构图(Zuul太麻烦直接由Gateway替代):
在这里插入图片描述
Alibaba微服务架构图:
在这里插入图片描述
想在Netflix、Alibaba之间切换的,可以看下图,替换掉相关组件,核心业务代码基本没有太大变化。
在这里插入图片描述

1、Eureka 两大组件

Eureka 采用 CS(Client/Server,客户端/服务器) 架构,它包括以下两大组件:Eureka Server、Eureka Client

组件介绍
Eureka ServerEureka 服务注册中心,主要用于提供服务注册功能
Eureka ClientEureka 客户端,通常指的是微服务系统中各个微服务

2、Eureka 服务注册与发现

在这里插入图片描述

功能介绍
服务注册中心(Register Service)它是一个 Eureka Server,用于提供服务注册和发现功能。
服务提供者(Provider Service)它是一个 Eureka Client,用于提供服务。它将自己提供的服务注册到服务注册中心,以供服务消费者发现。
服务消费者(Consumer Service)它是一个 Eureka Client,用于消费服务。它可以从服务注册中心获取服务列表,调用所需的服务。

3、案例

3.1、创建主工程

名称:SpringCloud
在这里插入图片描述

3.1.1、主工程pom.xml配置

在这里插入图片描述

<?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><packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.13</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.hqyj</groupId><artifactId>drp</artifactId><version>0.0.1-SNAPSHOT</version><name>drp-parent</name><description>Demo project for Spring Boot</description><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><junit.version>4.12</junit.version><log4j.version>1.2.17</log4j.version><lombok.version>1.16.18</lombok.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.5</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.5.1</version><configuration><source>${maven.compiler.source}</source><target>${maven.compiler.target}</target></configuration></plugin></plugins></build></project>

3.2、创建子公共模块common-api

3.2.1、添加module

在这里插入图片描述
在这里插入图片描述

3.2.2、pom.xml配置

<?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><parent><groupId>com.hqyj</groupId><artifactId>SpringCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>common-api</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

3.2.3、maven非springboot项目,增加main入口

添加Main.java,指定main入口,防止Maven package / install打包失败

public class Main {public static void main(String[] args) {System.out.println("common-api");}}

3.3、创建Eureka注册中心模块eureka-server

在这里插入图片描述

3.3.1、配置pom.xml

<?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><parent><groupId>com.hqyj</groupId><artifactId>SpringCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>eureka-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><skip-tests>true</skip-tests></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>com.github.enesusta</groupId><artifactId>spring-devtools</artifactId><version>1.0.1</version><optional>true</optional></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>

3.3.2、配置application.yml

在resource目录下,新建application.yml文件
在这里插入图片描述

编辑application.yml文件,添加eureka配置

server:port: 7001eureka:instance:hostname: localhost #eureka服务端的实例名称,client:register-with-eureka: false #false表示不向注册中心注册自己。fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务service-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机版服务注册中心

3.3.3、启动eureka-server

创建EurekaServerApplication.java启动文件


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class,args);}
}
3.3.3.1、编译eureka-server

在这里插入图片描述
在这里插入图片描述

3.3.3.2、运行EurekaServerApplication.java文件

在这里插入图片描述
启动:http://localhost:7001/

3.4、创建用户服务模块user-service在这里插入图片描述

3.4.1、配置pom.xml

<?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><parent><groupId>com.hqyj</groupId><artifactId>SpringCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>user-service</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--devtools 开发工具--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--Spring Boot 测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--junit 测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!-- 修改后立即生效,热部署 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId><version>1.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>com.hqyj</groupId><artifactId>common-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>

3.4.2、配置application.yml

server:port: 8001
spring:application:name: user-service  #微服务名称
eureka:client: #将客户端注册到 eureka 服务列表内service-url:defaultZone: http://localhost:7001/eureka  #这个地址是 7001注册中心在 application.yml 中暴露出来额注册地址 (单机版)

3.4.3、启动user-service

创建UserApplication.java启动文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class,args);}
}
3.4.3.1、编译user-service

在这里插入图片描述
在这里插入图片描述

3.4.3.2、运行UserApplication.java文件

在这里插入图片描述

3.4.3.3、测试

http://localhost:8001/user/userInfoList

3.5、查看编译后的包

在这里插入图片描述
在这里插入图片描述


小结

Eureka 作为Netflix 公司开发的一款开源的服务注册与发现组件,还是十分经典的。在服务注册与发现这一块,也是十分优秀,有兴趣的同学可以去下载Eureka的源码研究一下,看看Eureka是如何实现服务注册,调用端是如何发现服务并调用,最后再看看Eureka里同一个服务多台机器实例是如何实现负载均衡调用的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/144619.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【Java 进阶篇】深入理解 SQL 聚合函数

在 SQL 数据库中&#xff0c;聚合函数是一组强大的工具&#xff0c;用于处理和分析数据。它们可以帮助您对数据进行统计、计算总和、平均值、最大值、最小值等操作。无论您是数据库开发者、数据分析师还是希望更好地了解 SQL 数据库的用户&#xff0c;了解聚合函数都是非常重要…

Android12之Codec2.0配置编解码器为H264实现(四十五)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长! 优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀 人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药. 更多原创,欢迎关注:Android…

Qt使用I.MX6U开发板上的按键(原理:将电脑键盘方向键↓在Qt中的枚举值与开发板中按键定义的枚举值一致,这样电脑端测试效果就与开发板的一致)

在上篇介绍了Qt点亮I.MX6U开发板的一个LED&#xff0c;对于Qt控制I.MX6U开发板的一个蜂鸣器原理也是一样的&#xff0c;就不做详细介绍&#xff0c;具体可参考Qt控制I.MX6U开发板的一个蜂鸣器&#xff0c;本篇介绍Qt使用I.MX6U开发板上的按键的相关内容。 文章目录 1. 开发板硬…

李宏毅机器学习第一课(结尾附作业模型详细分析)

机器学习就是让机器找一个函数f&#xff0c;这个函数f是通过计算机找出来的 如果参数少的话&#xff0c;我们可以使用暴搜&#xff0c;但是如果参数特别多的话&#xff0c;我们就要使用Gradient Descent Regression (输出的是一个scalar数值) Classification &#xff08;在…

QT配置MySQL数据库 ninja: build stopped: subcommand failed

QT配置MySQL数据库 我当前的软件版本&#xff1a;QT Creator 10.0.2 (community)&#xff0c;MingW 6.4.3 (QT6)&#xff0c;MySQL 8.0。 MySQL不配置支持的数据库有QList("QSQLITE", "QODBC", "QPSQL")&#xff0c;这个时候是不支持MYSQL数据…

数据集笔记:2015上海地铁一卡通数据

数据地址&#xff1a;上海地铁数据_免费高速下载|百度网盘-分享无限制 (baidu.com) 数据介绍 上海2015年几天的地铁一卡通出入站信息 卡号、交易日期、交易时间、公交线路/地铁站点中文名称、行业名称(公交、地铁、出租、轮渡、PR停车场)、交易金额、交易性质(非优惠、优惠、…

PHP生成二维码带图标代码实例

PHP生成二维码带图标代码实例&#xff08;PHP QR Code二维码生成类库&#xff09; public static function png($text, $outfilefalse, $levelQR_ECLEVEL_L, $size3, $margin4, $saveandprintfalse) { $enc QRencode::factory($level, $size, $margin); return $enc->…

token登录的实现

token登录的实现 我这种token只是简单的实现token&#xff0c;就是后端利用UUID 生成简单随机码&#xff0c;利用随机码作为在Redis中的键&#xff0c;然后存储的用户信息作为值&#xff0c;在每次合理请求的时候对token的有效时间进行刷新&#xff08;利用拦截器&#xff09;&…

第P7周—咖啡豆识别(1)

数据集及wen件目录介绍&#xff1a; 数据集&#xff1a;工作台 - Heywhale.com 一、前期工作 1.1 数据详情 import torch import torch.nn as nn import torchvision.transforms as transforms import torchvision from torchvision import transforms, datasets import os,…

笔试强训Day7

T1&#xff1a;合法括号序列判断 链接&#xff1a;合法括号序列判断__牛客网 给定一个字符串A和其长度n&#xff0c;请返回一个bool值代表它是否为一个合法的括号串&#xff08;只能由括号组成&#xff09;。 经典括号匹配问题&#xff0c;考察栈的使用 #include<iostre…

Pytest+Allure+Excel接口自动化测试框架实战

1. Allure 简介 简介 Allure 框架是一个灵活的、轻量级的、支持多语言的测试报告工具&#xff0c;它不仅以 Web 的方式展示了简介的测试结果&#xff0c;而且允许参与开发过程的每个人可以从日常执行的测试中&#xff0c;最大限度地提取有用信息。 Allure 是由 Java 语言开发的…

常识判断 --- 科技常识

目录 力与热 光和声 航空成就 垃圾分类 百科知识 血型 二十四节气歌 春雨惊春清谷天 夏满忙夏暑相连 秋处露秋寒霜降 冬雪雪冬小大寒 力与热 光和声 航空成就 垃圾分类 百科知识 血型

【Ambari】银河麒麟V10 ARM64架构_安装Ambari2.7.6HDP3.3.1问题总结

&#x1f341; 博主 "开着拖拉机回家"带您 Go to New World.✨&#x1f341; &#x1f984; 个人主页——&#x1f390;开着拖拉机回家_大数据运维-CSDN博客 &#x1f390;✨&#x1f341; &#x1fa81;&#x1f341; 希望本文能够给您带来一定的帮助&#x1f338;文…

NFT Insider#109:The Sandbox推出了首个足球小将 NFT 作品集,YGG Web3 游戏峰会即将开启!

引言&#xff1a;NFT Insider由NFT收藏组织WHALE Members、BeepCrypto联合出品&#xff0c;浓缩每周NFT新闻&#xff0c;为大家带来关于NFT最全面、最新鲜、最有价值的讯息。每期周报将从NFT市场数据&#xff0c;艺术新闻类&#xff0c;游戏新闻类&#xff0c;虚拟世界类&#…

API 设计/开发/测试工具:Apifox与怎么通过拦截器

目录 一、测试接口如何创建&#xff1f; 二、如何添加body和header&#xff1f; 三、如果项目设置的有拦截器&#xff1f; 四、拦截器概念&#xff1a; 4.1使用拦截器概念 4.2 先写一个配置类WebMvcConfig.java 4.3 AuthInitInterceptor拦截器中实现 一、测试接口如何创建…

数据结构:简单记录顺序表、链表、栈、队列

初学者很容易认为顺序表、链表、栈、队列是四种并列的数据结构&#xff0c;其实仔细想想并不是。 注意区分&#xff1a; 顺序表和链表是指数据的存储结构&#xff0c;是线性表的一种&#xff0c;顺序表一般指的就是数组&#xff0c;数据存储的逻辑顺序和物理顺序都是连续的&a…

【图像处理】SIFT角点特征提取原理

一、说明 提起在OpenCV中的特征点提取&#xff0c;可以列出Harris&#xff0c;可以使用SIFT算法或SURF算法来检测图像中的角特征点。本篇围绕sift的特征点提取&#xff0c;只是管中窥豹&#xff0c;而更多的特征点算法有&#xff1a; Harris & Stephens / Shi–Tomasi 角点…

支付宝电脑网站支付,异步通知

一&#xff1a;异步通知是支付宝回调商户的服务器&#xff0c;所以这个地址需要通过外网访问&#xff0c;在真实项目中都会有对应的服务器&#xff0c;但是在测试中只有使用内网穿透工具 推荐使用NATAPP-内网穿透 基于ngrok的国内高速内网映射工具 配置好内网穿透之后不要忘记…

CIP或者EtherNET/IP中的PATH是什么含义?

目录 SegmentPATH举例 最近在学习EtherNET/IP&#xff0c;PATH不太明白&#xff0c;翻了翻规范&#xff0c;在这里记个笔记。下面的叙述可能是中英混合&#xff0c;有一些是规范中的原文我直接搬过来的。我翻译的不准确。 Segment PATH是CIP Segment中的一个分类。要了解PATH…

PHP8的继承和多态-PHP8知识详解

我们在前面的时候讲过《面向对象编程的特点》时&#xff0c;面向对象编程具有3大特点&#xff1a;封装性、继承性和多态性。 继承和多态的根本作用就是完成代码的重用。下面就来讲解php8的继承和多态。 1继承 子类可以继承父类的所有成员变量和成员方法&#xff0c;包括构造方…