开局一张图,截取了本人学习资料中的一张图,很好地展示了Eureka的架构。

Eureka服务器

管理服务的作用。细分为服务注册,服务发现。

所有的客户端在Eureka服务器上注册服务,再从Eureka服务器获取所有注册的客户端的信息列表,包括客户端名称,主机,端口等信息的列表,缓存在本地。客户端之间的调用,则是通过查找该列表上的信息,得到服务提供端(另一个客户端)的访问地址,从而调用服务。

下面开始搭建一个Eureka服务器。

一、首先创建一个父工程

新建一个maven的工程study-spring-cloud-parent,packaging选择pom。parent选择spring-boot-starter-parent的2.3.4.RELEASE。添加spring cloud版本管理的依赖管理。如下:

 <modelVersion>4.0.0</modelVersion>
<groupId>com.pzz.study</groupId>
<artifactId>study-spring-cloud-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build/>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>

<dependencyManagement>
<dependencies>
<!-- 管理Spring Cloud 的版本使用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

二、创建Eureka服务器

在父工程study-spring-cloud-parent 中新增一个Maven的模块study-eureka-server。添加Eureka的依赖。完整的pom如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.pzz.study</groupId>
<artifactId>study-spring-cloud-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>study-spring-cloud-eureka</artifactId>
<name>study-spring-cloud-eureka</name>
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies> <build>
<finalName>study-eureka-server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

新建一个EurekaServerApp的Java类,增加注解@EnableEurekaServer,表示该项目启动是一个Eureka服务器。增加注解@SpringBootApplication,使用SpringBoot的框架启动。在main函数中编写启动代码,具体如下:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApp {
public static void main( String[] args ){
new SpringApplicationBuilder(EurekaServerApp.class).run(args);
}
}

在resources下新建目录config,新建属性文件application.properties文件。内容如下:

#端口号
server.port=8001
#主机名称
eureka.instance.hostname=localhost
#注册中心设置为false,代表不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http\://${eureka.instance.hostname}\:8001/eureka/

单机,如果没有 eureka.client.register-with-eureka(默认true)和eureka.client.fetch-registry(默认true),启动报错。

运行EurekaServerApp的main方法,或使用springboot启动项目,成功后,在浏览器输入http://localhost:8001/,如下图,表示Eureka服务器已经搭建完成。

Eureka 服务器集群

服务器集群,构建高可用的服务器。在其中一部分服务器宕掉后,只要还存活其他的Eureka服务器,则不影响客户端的使用。

在上边服务器的基础上,将application.properties的文件名改成application-peer1.properties,并复制粘贴成另一个新的文件application-peer2.properties的文件。分别设置端口号8001和8002,并注释掉eureka.client.register-with-eureka和eureka.client.fetch-registry两个属性。并将eureka.client.service-url.defaultZone属性设置成对方的地址,分别向对方注册自己,以实现服务清单的同步,达到高可用的效果。代码分别如下:

application-peer1.properties的配置:

#端口号
server.port=8001
#主机名称
eureka.instance.hostname=localhost
#注册中心设置为false,代表不向注册中心注册自己
#eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
#eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http\://${eureka.instance.hostname}\:8002/eureka/

application-peer2.properties:

server.port=8002

eureka.instance.hostname=localhost
#注册中心设置为false,代表不向注册中心注册自己
#eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
#eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http\://${eureka.instance.hostname}\:8001/eureka/

修改启动类,分别使用application-peer1.properties启动和application-peer2.properties启动。

首先使用application-peer1.properties文件启动

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApp {
public static void main( String[] args ){
new SpringApplicationBuilder(EurekaServerApp.class).profiles("peer1").run(args);
}
}

启动后,控制台会输出如下错误日志:

2020-10-29 14:25:15.531 ERROR 17044 --- [           main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8002/eureka/}

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
2020-10-29 14:26:57.825 ERROR 17044 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_UNKNOWN/windows10.microdone.cn:8001 - was unable to refresh its cache! status = Cannot execute request on any known server

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.25.jar:1.9.25]

不用管这些错误日志,因为另一个服务器还没有启动,所以该服务器还不能作为客户端注册到另一个服务器,也不能从另一个服务器抓取客户端的列表。待按照如下代码,再启动一次就ok:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApp {
public static void main( String[] args ){
new SpringApplicationBuilder(EurekaServerApp.class).profiles("peer2").run(args);
}
}

启动成功后,在浏览器输入http://localhost:8001/ 能看到8002已经注册到该服务器

在浏览器输入 http://localhost:8002/,可以看到8001已经注册到该服务器

再过一段时间刷新这两个Eureka服务器的地址,在服务列表会发现都已经被拷贝到自己的注册列表,见下图;

给服务命名、地址IP显示

现在服务列表中的服务(Eureka服务器集群时,自己也在服务列表中)名称如上图是“UNKNOW”。我们新建一个属性文件application.properties来存放这些公用的属性。新增spring.application.name=study-eureka-server属性,给服务命名。运行后如下:

新增eureka.instance.instance-id=${spring.cloud.client.ip-address}\:${spring.application.name}\:${server.port}属性,给列表中的status中的服务添加ip地址,运行后如下

新增eureka.instance.prefer-ip-address=true属性,当鼠标移到注册列表的服务连接上时,显示的是IP,如下图:

完整的application.properties文件内容如下:

spring.application.name=study-eureka-server
#地址显示IP的形式
eureka.instance.prefer-ip-address=true
#eureka注册中心服务列表的名称格式
eureka.instance.instance-id=${spring.cloud.client.ip-address}\:${spring.application.name}\:${server.port}

Spring Cloud 学习笔记 (一)-- Eureka 服务器的更多相关文章

  1. Spring Cloud学习笔记【九】配置中心Spring Cloud Config

    Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...

  2. Spring Cloud学习笔记-002

    搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...

  3. Spring Cloud学习笔记-010

    分布式配置中心:Spring Cloud Config Spring Cloud Config是Spring Cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外 ...

  4. spring cloud 学习(二)关于 Eureka 的学习笔记

    关于 Eureka 的学习笔记 个人博客地址 : https://zggdczfr.cn/ ,欢迎光临~ 前言 Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务.Sprin ...

  5. Spring Cloud学习笔记【一】Eureka服务注册与发现

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

  6. Spring Cloud 学习笔记(一)——入门、特征、配置

    [TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...

  7. spring cloud 学习笔记(1)

    SpringCloud + Eureka / Nacos git:https://github.com/huanmsf/springCloudLearn.git 项目目录: 父pom: <?xm ...

  8. Spring Cloud学习笔记-012

    分布式服务跟踪:Spring Cloud Sleuth 随着业务的发展,系统规模也会变得越来越大,各微服务间的调用关系也变得越来越错综复杂.通常一个由客户端发起的请求在后端系统中会经过多个不同的微服务 ...

  9. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

随机推荐

  1. 吴恩达Machine Learning学习笔记(四)--BP神经网络

    解决复杂非线性问题 BP神经网络 模型表示 theta->weights sigmoid->activation function input_layer->hidden_layer ...

  2. 小白也能看懂的Redis教学基础篇——redis神秘的数据结构

    各位看官大大们,周末好! 作为一个Java后端开发,要想获得比较可观的工资,Redis基本上是必会的(不要问我为什么知道,问就是被问过无数次).那么Redis是什么,它到底拥有什么神秘的力量,能获得众 ...

  3. Spring源码系列——容器的启动过程(一)

    一. 前言 Spring家族特别庞大,对于开发人员而言,要想全面征服Spring家族,得花费不少的力气.俗话说,打蛇打七寸,那么Spring家族的"七寸"是什么呢?我心目中的答案一 ...

  4. linux(centos)环境下安装rabbitMq

    1.由于rabbitMq是用Erlang语言写的,因此要先安装Erlang环境 下载Erlang :http://www.rabbitmq.com/releases/erlang/erlang-19. ...

  5. Ubuntu16.04下升级Python到3.7

    本帖从IT老兵博客学习得知: 本帖前提: 开发一个Python的系统,需要安装Python3.6以上的版本,由于使用的操作系统是Ubuntu16.04,默认带的Python是2.7.12和3.5,不满 ...

  6. MySQL 修改表中的字段,使其自增

    例如,我想使字段 id 自增. 1.查看表定义 mysql> DESC user; +----------+-------------+------+-----+---------+------ ...

  7. 插头 dp

    插头dp 洛谷 黑题板子? P5056 给出n×m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? 1.轮廓线 简单地说,轮廓线就是已决策格子和未决策格子的分界线: 2,插 ...

  8. C# 读取路径的各种方式

    //1.获取模块的完整路径. string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; // ...

  9. 深入研究RocketMQ消费者是如何获取消息的

    前言 小伙伴们,国庆都过的开心吗?国庆后的第一个工作日是不是很多小伙伴还沉浸在假期的心情中,没有工作状态呢? 那王子今天和大家聊一聊RocketMQ的消费者是如何获取消息的,通过学习知识来找回状态吧. ...

  10. 三、Requests库的使用

    requests 的底层实现其实就是 urllib3 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 学过关于urllib库的使用,你会发现它是很不方便的.而R ...