注册中心Eureka

ops/2024/10/15 1:06:08/

Eureka

文章目录

  • 前言
  • 一、Eureka是什么?
  • 二、使用步骤
    • 1.搭建一个SpringCloud项目
    • 2.启动类上增加注解 @EnableEurekaServer
    • 3.启动项目 访问 配置文件里定义的端口号
    • 4.启动成功访问 localhost:7000
    • 5.以同样的方式创建子项目 eureka-client-xx 作为 服务客户端 然后向eureka-server 服务中心注册
    • 5.client端启动成功再次访问 注册中心 eureka-server localhost:7000


前言

Eureka是Netflix开发的基于REST的服务发现框架,主要用于服务注册,管理,负载均衡和服务故障转移.
官方声明在Eureka2.0版本停止维护,不建议使用.但是Eureka是SpringCloud服务注册/发现的默认实现,所以目前还是有很多公司在使用.

一、Eureka是什么?

Eureka 是一个注册中心主要分为两部分:
Eureka Server: 作为注册中心Server端,向微服务应用程序提供服务注册,发现,健康检查等能力。
Eureka Client: 服务提供者,服务启动时,会向Eureka Server 注册自己的信息(IP,端口,服务信息等),Eureka Server 会存储这些信息。
常见的注册中心:

  1. Zookeeper:Zookeeper的官方并没有说它是一个注册中心,但是国内Java体系,大部分的集群环境都是依赖Zookeeper来完成注册中心的功能。
  2. Eureka:Eureka是Netflix开发的基于REST的服务发现框架,主要用于服务注册,管理,负载均衡和服务故障转移.
    官方声明在Eureka2.0版本停止维护,不建议使用.但是Eureka是SpringCloud服务注册/发现的默认实现,所以目前还是有很多公司在使用。
  3. Nacos:Nacos是Spring Cloud Alibaba架构中重要的组件,除了服务注册,服务发现功能之外,Nacos还支持配置管理,流量管理,DNS.动态DNS等多种特性。

二、使用步骤

1.搭建一个SpringCloud项目

在这里插入图片描述

在这里插入图片描述

3.在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><groupId>org.zch</groupId><artifactId>spring-cloud-demo</artifactId><version>1.0-SNAPSHOT</version><properties><!-- 项目统一管理版本 --><java.version>1.8</java.version></properties><!--  等有了子项目 用于添加字模块 暂时没有创建 先注释掉了 等有了子项目就添加到这里 --><!--<modules><module>子模块1</module><module>子模块2</module><module>子模块3</module><module>子模块4</module><module>子模块5</module><module>子模块6</module><module>子模块7</module><module>子模块8</module></modules>--><!--  管理依赖的版本号,并将版本号传递给子项目,此时只是管理,并不会下载依赖。springBoot和SpringCloud的版本号需要相互对应  --><dependencyManagement><dependencies><!--  spring-cloud依赖版本管理器 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.6.RELEASE</version><!--将版本号传递给子项目,必须加--><type>pom</type><scope>import</scope></dependency><!-- spring-cloud依赖版本管理器 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR9</version><!--将版本号传递给子项目,必须加--><scope>import</scope><type>pom</type></dependency><!-- springboot依赖版本管理器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.2.3.RELEASE</version><!--将版本号传递给子项目,必须加--><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- 添加项目构建插件 maven 我本地的maven3.6.1 jdk1.8  --><build><plugins><!-- 添加插件maven3.6.1 编译器   --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><!-- 添加spring-boot编译器   --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.2.RELEASE</version></plugin></plugins></build></project>

4.创建eureka-server项目 也是一个子模块
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

5.子模块创建出来了 ,添加依赖 配置子模块 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><!-- 指向父级 --><parent><groupId>com.example</groupId><artifactId>spring-cloud-demo01</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> </parent><!-- 子级 子模块信息 --><groupId>com.example</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-server</name><description>eureka-server</description><properties><java.version>1.8</java.version></properties><dependencies><!-- 添加 eureka-server 依赖  --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 添加 web 依赖  --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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></dependencies><!-- 添加项目构建插件   --><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

6.修改子项目配置文件 .yml 如果创建出来的不是yml建议改成yml

server:port: 7000spring:application:name: eureka-server
eureka:client:fetch-registry: falseregister-with-eureka: falseservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/instance:hostname: localhost

2.启动类上增加注解 @EnableEurekaServer

package com.example.eurekaserver;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}

3.启动项目 访问 配置文件里定义的端口号

在这里插入图片描述

4.启动成功访问 localhost:7000

在这里插入图片描述

eurekaclientxx___eurekaserver__261">5.以同样的方式创建子项目 eureka-client-xx 作为 服务客户端 然后向eureka-server 服务中心注册

eureka-client 子模块:

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><parent><groupId>com.example</groupId><artifactId>spring-cloud-demo01</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>eureka-client</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-client</name><description>eureka-client</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></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</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2.yml

server:port: 8001
eureka:client:service-url:#注册服务中心地址 eureka-server 项目的端口defaultZone: http://localhost:7000/eureka/
#指定应用程序的名称
spring:application:name: eureka-client

3.启动类添加注解 @EnableDiscoveryClient

package com.example.eurekaclient;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaClientApplication.class, args);}}

4.启动eureka-client
在这里插入图片描述

eurekaserver____localhost7000_376">5.client端启动成功再次访问 注册中心 eureka-server localhost:7000

在这里插入图片描述

想要注册更多的服务 就按照上面 不断的加进来子项目 实际开发中应该会有很多子模块 注册进来 这里只是模仿一下 服务的注册 与 发现,便于理解,记录生活,记录美好,记录学习过程。


http://www.ppmy.cn/ops/119113.html

相关文章

Docker自定义构建镜像dockerfile和使用数据卷

Docker自定义构建镜像dockerfile和使用数据卷 DockerFile Dockerfile 是一个文本文件&#xff0c;包含了一系列用于构建 Docker 镜像 的指令和配置信息。通过编写 Dockerfile &#xff0c;用户可以定义镜像的构建过程&#xff0c;包括基础镜像、运行命令、设置环境变量、暴露…

科研绘图系列:R语言堆积图(stacked barplot)

文章目录 介绍加载R包导入数据数据预处理画图导出数据系统信息介绍 微生物堆积图是一种数据可视化工具,通常用于展示微生物群落中不同物种的相对丰度。这种图表通过将每个样本中的微生物按照其分类学等级(如门、属等)进行分类,并以不同颜色的块状图表示,每个块的大小代表…

【优选算法】(第四篇)

目录 三数之和&#xff08;medium&#xff09; 题目解析 讲解算法原理 编写代码 四数之和&#xff08;medium&#xff09; 题目解析 讲解算法原理 编写代码 三数之和&#xff08;medium&#xff09; 题目解析 1.题目链接&#xff1a;. - 力扣&#xff08;LeetCode&…

策略(政策)模式

简介 策略模式&#xff08;Strategy Pattern&#xff09;又叫作政策模式&#xff08;Policy Pattern&#xff09;&#xff0c;它将定义的算法家族分别封装起来&#xff0c;让它们之间可以互相替换&#xff0c;从而让算法的变化不会影响到使用算法的用户&#xff0c;属于行为型…

vue 流式加载mp4文件

video组件 传入assetURL视频地址即可&#xff0c;组件内配置了代理&#xff0c;注意配置/video-api 代理 <template><video ended"emits(ended)" autoplay muted ref"video"><source type"video/mp4" />Your browser does no…

腾讯邮箱上传附件卡、慢、无法上传,下载慢问题处理

1、检查文件中转站容量是否已满 2、建议用户打开链接https://exmail.qq.com/qy_mng_logic/wasmHelper?typehashv2&#xff0c;看是否可以正常访问。&#xff08;能打开下载就表示可以正常访问&#xff09; 3、让用户切换到4G或者其他网络再重新上传附件是否会重现问题&#xf…

【WSL——Windows 上使用 Linux 环境】

引入 以前在windows上使用linux工具链&#xff0c;一般都要安装虚拟机&#xff08;VMware/virtualBox)。虚拟机的缺点是&#xff0c;因为是完整的虚拟环境&#xff0c;消耗系统资源比较多。 windows自己开发了WSL功能&#xff0c;实现了虚拟机的功能&#xff0c;但是比虚拟机性…

goland使用redis实现签到功能

签到封装&#xff1a;直接调用即可&#xff0c;基本满足所有签到操作 package mainimport ("context""fmt""time""github.com/go-redis/redis/v8" )type UserSign struct {rdb *redis.Client }func NewUserSign(opt *redis.Options) …