概要

  • 什么是Spring Cloud Eureka?

  • 使用Eureka获取服务调用

  • Eureka整合Spring Config Server

  • 构建Eureka Server集群

什么是Spring Cloud Eureka?

Spring Cloud Eureka 模块提供的功能是被动式的服务发现.
什么是服务发现?
服务发现就像聊天室一个,每个用户来的时候去服务器上注册,这样他的好友们就能看到你,你同时也将获取好友的上线列表.
在微服务中,服务就相当于聊天室的用户,而服务注册中心就像聊天室服务器一样,目前服务发现的解决方案有Eureka,Consul,Etcd,Zookeeper,SmartStack,等等.

本文就来讲讲Eureka,如图所示,Eureka Client通过HTTP(或者TCP,UDP)去Eureka Server注册和获取服务列表,为了高可用一般会有多个Eureka Server组成集群.Eureka会移除那些心跳检查未到达的服务.

使用Eureka获取服务调用

这节我们将构建一个Eureka Server,5个Eureka Client(分别提供主语,动词,量词,形容词,名词服务),再构建一个Sentence Eureka Client 来用前面五个服务造句.

1.创建mmb-eureka-server

  • 添加依赖-spring-cloud-starter-parent,spring-cloud-starter-eureka-server(pom.xml)

 <parent><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-parent</artifactId><version>Brixton.SR4</version><relativePath/></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency></dependencies>
  • 配置应用信息-端口和应用名称 application.yml

server:port: 8010spring:application:name: mmb-eureka-server
  • 启动服务

@SpringBootApplication
@EnableEurekaServer
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
  • 打开管理页面,检查是否成功


2.创建mmb-eureka-client

  • 添加依赖-spring-cloud-starter-parent,spring-cloud-starter-eureka (pom.xml)

  <parent><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-parent</artifactId><version>Brixton.SR4</version><relativePath/></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies>
  • 配置应用信息-eureka server信息,实际使用的words信息,端口号 (application.yml)

eureka:client:service-url:defaultZone: http://localhost:8010/eureka/words: 你,我,他server:port: ${PORT:${SERVER_PORT:0}}
#  这个的意思是随机指定个没使用的端口
  • 配置启动信息-应用名称 (bootstrap.xml)

spring:application:name: mmb-eureka-client-subject
  • 添加Controller-随机获取words中的一条

@RestController
public class Controller {@Value("${words}") String words;@RequestMapping("/")public  String getWord() {String[] wordArray = words.split(",");int i = (int)Math.round(Math.random() * (wordArray.length - 1));return wordArray[i];}
}
  • 启动服务

@SpringBootApplication
@EnableEurekaClient
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
  • 访问127.0.0.1/port(看日志可以得到各个应用的port) 看到words里的词就启动成功了,

其它的verb,acticle,adjective,noun工程类似,就把words,和spring.application.name改成对应的工程名字就好了

3.创建sentence工程

  • 添加依赖-spring-cloud-starter-parent,spring-cloud-starter-eureka,spring-boot-starter-web,spring-boot-starter-actuator (pom.xml)

  <parent><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-parent</artifactId><version>Brixton.SR4</version><relativePath/></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><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和端口号 (application.yml)

eureka:client:serviceUrl:defaultZone: http://localhost:8010/eureka/server:port: 8020
  • 配置启动信息-应用名称 (bootstrap.yml)

spring:application:name: mmb-eureka-sentence
  • 添加Controller-用其他eureka-clients(subject,verb,acticle,adjective,noun)的各个服务造句

@RestController
public class Controller {@AutowiredDiscoveryClient client;@RequestMapping("/sentence")public  String getSentence() {returngetWord("mmb-eureka-client-subject") + " "+ getWord("MMB-EUREKA-CLIENT-VERB") + " "+ getWord("mmb-eureka-client-article") + " "+ getWord("mmb-eureka-client-adjective") + " "+ getWord("mmb-eureka-client-noun") + ".";//大小写不区分}public String getWord(String service) {List<ServiceInstance> list = client.getInstances(service);if (list != null && list.size() > 0 ) {URI uri = list.get(0).getUri();if (uri !=null ) {return (new RestTemplate()).getForObject(uri,String.class);}}return null;}
}
  • 启动服务

@SpringBootApplication
@EnableEurekaServer
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
  • 先启动Eureka Server,再启动Eureka Client,在管理页面上看到服务都起成功时,访问127.0.0.1/8020/sentence 可以得到一个随机组成的句子

Eureka整合Spring Config Server

  1. 在git的repository里添加application.yml

 eureka:client:serviceUrl:defaultZone: http://localhost:8010/eureka/
  1. 启动实战(一)中的Spring Cloud Config Server

  2. 修改各个client的配置

    • application.yml移除属性eureka.client.serviceUrl.defaultZone

    • bootstrap.yml添加属性 spring.cloud.config.uri: http://localhost:8001

    • pom.xml添加依赖spring-cloud-config-client

  3. 依次启动Config Server,Eureka Server,Eureka Client,在管理页面上看到服务都起成功时,访问127.0.0.1/8020/sentence 可以得到一个随机组成的句子
    -

  4. 如果你想把words信息也放入repository呢?在application.yml中添加,如下信息,各个client启动的时候加上-Dspring.profiles.active对应到相应的启动参数就行了.

    ---spring:profiles: subjectwords: I,You,He,She,It---spring:profiles: verbwords: ran,knew,had,saw,bought---spring:profiles: articlewords: a,the---spring:profiles: adjectivewords: reasonable,leaky,suspicious,ordinary,unlikely---spring:profiles: nounwords: boat,book,vote,seat,backpack,partition,groundhog  

构建Eureka Server集群

  • host文件中添加 (c:WINDOWSsystem32driversetchosts).

  127.0.0.1       eureka-primary127.0.0.1       eureka-secondary127.0.0.1       eureka-tertiary
  • Eureka Server的application.yml添加多个profiles,和instanceId

 ---
spring:application:name: eureka-server-clustered   profiles: primary
server:port: 8011
eureka:instance:hostname: eureka-primary       ---
spring:application:name: eureka-server-clustered      profiles: secondary
server:port: 8012
eureka:instance:hostname: eureka-secondary       ---
spring:application:name: eureka-server-clustered      profiles: tertiary
server:port: 8013
eureka:instance:hostname: eureka-tertiary       
  • 此时Eureka Server 同时也是个Eureka Client,需要设置eureka.client.serviceUrl.defaultZone,值是另外两个,最终会是下面这样

---
spring:application:name: eureka-server-clustered   profiles: primary
server:port: 8011
eureka:instance:hostname: eureka-primary       client:registerWithEureka: truefetchRegistry: true        serviceUrl:defaultZone: http://eureka-secondary:8012/eureka/,http://eureka-tertiary:8013/eureka/---
spring:application:name: eureka-server-clustered      profiles: secondary
server:port: 8012
eureka:instance:hostname: eureka-secondary       client:registerWithEureka: truefetchRegistry: true        serviceUrl:defaultZone: http://eureka-tertiary:8013/eureka/,http://eureka-primary:8011/eureka/---
spring:application:name: eureka-server-clustered      profiles: tertiary
server:port: 8013
eureka:instance:hostname: eureka-tertiary       client:registerWithEureka: truefetchRegistry: true    serviceUrl:defaultZone: http://eureka-primary:8011/eureka/,http://eureka-secondary:8012/eureka/      
  • 以-Dspring.profiles.active=primary (and secondary, and tertiary)为启动参数分别启动Eureka Server

  • 修改所有Eureka Client的eureka.client.serviceUrl.defaultZone值为http://eureka-primary:8011/eu...逗号分隔,无空白),集群启动成功登录管理页面查看,如下图所示即成功

  • 再启动所有的Eureka Clients,查看http://localhost:8020/sentence 是否成功

  • 为了测试容错性,关掉两个Eureka Client,重启若干个Eureka Client,观察启动是否报错,再去查看查看http://localhost:8020/sentence 是否成功

Spring Cloud实战(二)-Spring Cloud Eureka相关推荐

  1. Spring 学习之 二----Spring创建对象的三种方式

    最近在系统的学习Spring,现在就Spring的一些知识进行总结. 我们知道Spring是一个开放源代码的设计层面的框架,他主要解决的是业务逻辑层与其他各层之间松耦合的问题. Spring 有三个核 ...

  2. Spring Security 实战:Spring Boot 下的自动配置

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | 公众号「码农小胖哥」 1. 前言 我们在前几篇 ...

  3. Spring Boot系列二 Spring @Async异步线程池用法总结

    转载 自 https://blog.csdn.net/hry2015/article/details/67640534 1. TaskExecutor Spring异步线程池的接口类,其实质是java ...

  4. Spring Boot2 总结(二) Spring Security的基本配置

      Spring Boot对Spring Security提供了自动化配置方案,同时这也是在Spring Boot项目中使用Spring Security的优势,因此Spring Security整合 ...

  5. 【struts2+hibernate+spring项目实战】Spring计时器任务 Spring整合JavaMail(邮件发送)(ssh)

    一.常用数据频度维护 对于系统使用度较高的数据,客户在查看时希望这些数据最好先出现,此时需要为其添加排序规则.在进行排序时,使用次数成为排序的依据.因此需要设置一个字段用来描述某种数据的使用次数,也就 ...

  6. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  7. Spring Data 系列(二) Spring+JPA入门(集成Hibernate)

    通过[Spring Data 系列(一) 入门]的介绍,通过对比的方式认识到Spring提供的JdbcTemplate的强大功能.通过使用JdbcTemplate,操作数据库,不需要手动处理Conne ...

  8. Spring Cloud实战(三)-监控中心

    接着上一篇 Spring Cloud实战(二)-注册中心 现在开始搭建监控中心 一.监控中心monitor-server 1.添加spring-boot-admin版本控制到cloud-action的 ...

  9. Spring Security 实战干货: RBAC权限控制概念的理解

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 码农小胖哥 来源 | 公众号「码农小胖哥」 1 ...

  10. Spring Security 实战:基于配置的接口角色访问控制

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 码农小胖哥 来源 | 公众号「码农小胖哥」 1 ...

最新文章

  1. LeetCode简单题之重新格式化字符串
  2. [JZOJ4786]小a的强迫症
  3. linux新内核的freeze框架以及意义
  4. linux驱动由浅入深系列:高通sensor架构实例分析之一
  5. python用random产生验证码,以及random的一些其他用法
  6. vb.net datagridview数据批量导入sql_导入:Java实现大批量数据导入导出(100W以上)
  7. Python基础案例(一)
  8. 空间平面,空间直线及它们的方程
  9. TensorFlow2.0正式版安装
  10. 脉脉因“App 整改下架”事件致歉;阿里云全年营收超 600 亿;腾讯防大量群消息骚扰专利获授权|极客头条...
  11. Redis 集群(学习笔记十)
  12. Phoca Gallery Images 去除 logo
  13. Ovi Store标志着App store模式大战正式开启
  14. aliplayer播放器使用
  15. 把握这两点,抢占下一个电商风口|2016最新中国电商App排名研究报告
  16. android反加密反加固,[原创]记一次爱加密反调试分析及绕过思路
  17. 蓝桥杯2021年第十二届真题第一场-双向排序
  18. git加速 用谷歌浏览器插件
  19. latex : 常见编译错误记录
  20. 用python开发一款云笔记_Python成为专业人士笔记–os模块

热门文章

  1. 2012年中国大学最新排名
  2. 各省简称 拼音 缩写_中国省市县地区首字母缩写
  3. [About Design] 各类素材网站
  4. 第九节 html特殊文字符号
  5. 程序员坐牢了,会被安排去写代码吗?
  6. 使用谷歌地图在 Flutter 应用中添加地图
  7. 可以将pdf转换成jpg图片格式的方法
  8. 罗马数字 java_Java算法练习——整数转罗马数字
  9. 算法:罗马数字转整数
  10. python声音模拟_5秒钟让python克隆别人的声音