Spring Cloud 服务的注册与发现(Eureka)
Eureka服务注册中心
一、Eureka Server
Eureka Server是服务的注册中心,这是分布式服务的基础,我们看看这一部分如何搭建。
首先,Spring Cloud是基于Spring Boot的,所以我们的项目都是Spring Boot项目。需要引入最基础的Spring Boot的jar包,如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
然后,再引入Eureka Server的jar包:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
接下来我们编写启动类:
@SpringBootApplication
@EnableEurekaServer
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
其中@EnableEurekaServer标志着这个服务是一个Eureka Server。
下面就是最重要的application.yml的配置,我们先从最简单的单例模式说起。
单例模式
这种模式下,Eureka Server是一个单点,如果发生故障,则整个注册中心不可用,只适用于测试环境,具体配置如下:
spring:
profiles: standalone
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8761
fetch-registry: false
register-with-eureka: false
server:
port: 8761
在单例模式下,我们关掉客户端的行为。其中fetch-registry是抓取注册的服务,register-with-eureka是将自己本身向其他的Eureka Server 注册。 这两项在集群配置时是必须打开的,这样才能使注册的服务同步到其他节点上。
在单例模式下,eureka.instance.hostname必须是localhost,而且defaultZone不能使用ip,要使用eureka.instance.hostname且走域名解析才可以。 这里我们配置的是localhost,不需要修改hosts文件。这块不知道为什么Spring Cloud要这么设计,小编在搭建集群时折腾了好长时间。 我们启动服务,访问http://localhost:8761,可以看到Eureka的管理页面。
集群模式
我们可以通过创建多个Eureka Server实例,并让他们相互注册来实现集群。相互注册就是我们前面提到的fetch-registry和register-with-eureka, 它们默认都是true,所以不需要配置,我们需要制定其他节点的url就可以了,我们以3个节点为例:
spring:
application:
name: eureka-server ---
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
client:
service-url:
defaultZone: http://peer2:9200/eureka/,http://peer3:9300/eureka/
server:
port: 9100
---
spring:
profiles: peer2
eureka:
instance:
hostname: peer2
client:
service-url:
defaultZone: http://peer1:9100/eureka/,http://peer3:9300/eureka/
server:
port: 9200
--- spring:
profiles: peer3
eureka:
instance:
hostname: peer3
client:
service-url:
defaultZone: http://peer1:9100/eureka/,http://peer2:9200/eureka/
server:
port: 9300
我们在一台机器上起3个实例,peer1、peer2、peer3,端口分别为:9100、9200、9300。 这里我们还是要注意一下instance.hostname和service-url
- 3个实例的instance.hostname不能重复,否则集群搭建失败
- service-url不能使用ip+端口直接访问,否则也会失败
在单机情况下,我们需要配置hosts来解析peer1、peer2、peer3
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3
集群搭建成功:
DS Replicas显示两个副本节点,available-replicas显示两个可用的副本节点。
如果在真实的物理机上,我们可以不通过配置hosts的方式也是可以的,记住这是 真实的物理机的情况下, 每台机器的ip都不一样。配置如下:
spring:
application:
name: eureka-server ---
spring:
profiles: peer1
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://ip2:9200/eureka/,http://ip3:9300/eureka/
server:
port: 9100
---
spring:
profiles: peer2
eureka:
instance:
hostname: peer2prefer-ip-address: true
client:
service-url:
defaultZone: http://ip1:9100/eureka/,http://ip3:9300/eureka/
server:
port: 9200
--- spring:
profiles: peer3
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://ip1:9100/eureka/,http://ip2:9200/eureka/
server:
port: 9300
实例的名称可以使用ip去注册,当然每个ip不一样,也不会重复,不会导致失败。
至此,我们的Eureka Server就搭建完了,具体参照GitHub地址:https://github.com/liubo-tech/spring-cloud-eureka
Spring Cloud 服务的注册与发现(Eureka)的更多相关文章
- Spring Cloud服务的注册与发现(Eureka)
一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...
- Spring Cloud服务的注册与发现
Spring Cloud简介: Spring Cloud为开发人员提供了快速构建分布式系统中的一些通用模式(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分 ...
- SpringCloud-微服务的注册与发现Eureka(二)
一.SpringCloud简介 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- SpringCloud-微服务的注册与发现Eureka
一.SpringCloud简介 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(转载)
SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本) 转载请标明出处:http://blog.csdn.net/forezp/article/details ...
- Spring Cloud 服务端注册与客户端调用
Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...
- 服务的注册与发现Eureka(二)
1.服务治理概念 在传统rpc远程调用中,服务与服务依赖关系,管理比较复杂,所以需要使用服务治理,管理服务与服务之间依赖关系,可以实现服务调用.负载均衡.容错等,实现服务发现与注册. 2.服务的注册与 ...
- 创建服务的注册与发现 Eureka (四)
一.eureka注册中心 1.创建一个工程 工程名:microservicecloud-eureka-7001 2.向pom文件中增加如下: <dependencies> <!--e ...
- 第一篇:服务的注册与发现Eureka(Finchley版本)
一.创建服务注册中心(Eureka) 1. 首先创建一个maven主工程 创建一个主Maven工程,在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Clo ...
随机推荐
- 抽屉之Tornado实战(3)--注册
知识点应用:标签绑定事件,jQuery获取用户值-->AJAX发送数据-->后台路由系统-->业务逻辑处理-->ORM数据操作-->write返回-->AJAX回调 ...
- IDEA指定启动JDK版本
使用场景: 开发人员在自己的机器上可能装了多个版本的JDK,但是在环境变量中只能配置一个 JAVA_HOME ,so你的IDEA Eclipse 可能因为你在 JAVA_HOME 配置JDK1.8 以 ...
- byte数组存储到mysql
public int AddVeinMessage(byte[] data)//插入数据库 { using (BCSSqlConnection = new MySqlConnection(strCon ...
- Matlab中添加路径与去除路径
今天在使用Matlab调用内部的PCA函数的时候,报错: 错误使用 pca输入参数太多. 如下图所示: 网上查找原因之后发现是因为我之前下载过开源的工具包toolbox,并且将它的路径add到了Mat ...
- JavaScript学习(二)
比如isNaN("100")会返回true 注意:parseInt()的参数必须以数字开头
- vux 使用swiper 垂直滚动文字 报错[Intervention] Ignored...
[Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example becaus ...
- 微服务——RestTemplate
GET请求: 第一种:getForEntity: 此方法返回的是ResponseEntity,该对象是Spring对HTTP请求响应的封装. RestTemplate rt = new RestTem ...
- Python 全栈开发九 日志模块
日志是一种可以追踪某些软件运行时所发生事件的方法.软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情.一个事件可以用一个可包含可选变量数据的消息来描述.此外,事件也有重要性的概念 ...
- Spring MVC 简介及入门小例子
说明:文章内容全部截选自实验楼教程[Spring MVC 简易教程] 一.什么是 Spring MVC Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring ...
- centos 打包报错License for package Android SDK Build-Tools 25.0.3 not accepted
报错如下: 提示没有25.0.3的安卓环境,那么,接下来就需要安装这个环境 1.android list sdk -a 会显示需要更新 类似如下(截图只是一部分,前后还有一部分): 2.android ...