博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud Eureka
阅读量:6886 次
发布时间:2019-06-27

本文共 2972 字,大约阅读时间需要 9 分钟。

hot3.png

Spring Cloud Eureka

目录:

Eureka介绍

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.Eureka云端服务发现,是一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。简单说,eureka是一个服务注册中心,具有服务注册和发现功能,在此基础上可以更好的管理服务,并且配合SpringCloud其他组件,如Zuul、Hystrix、Bus、Feign等,来更加高效、便利地治理微服务环境

单节点注册中心

引入maven依赖:

org.springframework.boot
spring-boot-starter-parent
1.4.5.RELEASE
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.cloud
spring-cloud-dependencies
Brixton.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin

配置文件 application.yml (yaml形式,或者application.properties,参考springboot配置说明)

server:  port: 9999eureka:  instance:    hostname: localhost  client:    #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己    registerWithEureka: false    #由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #注册中心地址 注意:这里需要加http:// 开头,不然页面会报错

添加springboot启动类

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer //eureka支持@SpringBootApplication //springboot支持public class EurekaApp {  public static void main(String[] args) {      SpringApplication.run(EurekaApp.class, args);  }}

启动后,访问 image

Eureka集群

如果Eureka服务挂了,那么其他注册到上面的服务也将不可用。Eureka可以多节点配置,也就是说每个Eureka可以作为单独的节点,并且这些节点互相注册互相作为注册方和消费方。这样其中一个几点挂了,其他节点照样能提供服务。

项目演示:

在application.yml的基础上,新建配置文件:application-pee1.yml application-pee2.yml application-pee3.yml

application.yml:

eureka:  instance:    hostname: localhost  client:    #默认为ture,代表向注册中心注册自己    #registerWithEureka: false    #默认为ture,从注册中心获取服务信息    #fetchRegistry: falsespring:  profiles:    active: pee1 #默认启动pee1环境的配置

application-pee1.yml配置

server:  port: 9901eureka:  client:    serviceUrl:      defaultZone: http://localhost:9902/eureka/,http://localhost:9903/eureka/ #注册中心地址为其他2个节点地址spring:  application:    name: eureka-server1 #服务名称

pee1和pee2配置

pee2同pee1,将端口改为9902,注册中心地址改为 http://9901,http://9903pee3同pee1,将端口改为9903,注册中心地址改为 http://9901,http://9902

分别启动pee1 pee2

启动startApp后,将application.yml中的active分别改为pee2,pee3,分别启动然后分别访问http://localhost:9901 http://localhost:9902 http://localhost:9903

效果如下: image

源码地址

转载于:https://my.oschina.net/yimingkeji/blog/2873493

你可能感兴趣的文章
Clash Detection
查看>>
从CAP理论中分析Eureka与zookeeper的区别
查看>>
20172318 2018-2019-1 《程序设计与数据结构》第2周学习总结
查看>>
文件操作
查看>>
ubuntu忘记root密码解决
查看>>
windows 80端口被占用的解决方法
查看>>
Qt学习五 - 对话框
查看>>
Android 学习 笔记_12. Spinner的简单实使用
查看>>
手册与参考链接
查看>>
做错的题目——this的指向
查看>>
Struts、JSTL标签库的基本使用方法
查看>>
A Tour of Go Numeric Constants
查看>>
android获取硬件信息
查看>>
计算机操作系统的因果
查看>>
C#中int,string,char[],char的转换(待续)
查看>>
wamp环境的安装
查看>>
BZOJ 4025: 二分图
查看>>
使用百度地图实现详细地址自动补全(补全bug''事件只能绑定到一个上的问题')...
查看>>
Emoji表情处理工具类
查看>>
刚刚考过dev401,出去玩了!有时间我把题目给大家贴出来。
查看>>