首页 文章 精选 留言 我的

精选列表

搜索[组件化],共10000篇文章
优秀的个人博客,低调大师

SpringCloud微服务(01):Eureka组件,服务注册与发现

本文源码:GitHub·点这里 || GitEE·点这里 一、Eureka基本架构 1、Eureka简介 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。 2、Eureka角色结构图 角色职责如下:1)、Register:服务注册中心,它是一个Eureka Server ,提供服务注册和发现功能。2)、Provider:服务提供者,它是一个Eureka Client ,提供服务。3)、Consumer:服务消费者,它是一个Eureka Cient ,消费服务。 3、Eureka中几个核心概念 1)、Registe服务注册当Client向Server 注册时,Client 提供自身的元数据,比如IP 地址、端口、运行状况指标的Uri 、主页地址等信息。2)、Renew服务续约Client 在默认的情况下会每隔30 秒发送一次心跳来进行服务续约。通过服务续约来告知Server该Client仍然可用。正常情况下,如果Server在90 秒内没有收到Client 的心跳,Server会将Client 实例从注册列表中删除。官网建议不要更改服务续约的间隔时间。3)、Fetch Registries获取服务注册列表信息Client 从Server 获取服务注册表信息,井将其缓存在本地。Client 会使用服务注册列表信息查找其他服务的信息,从而进行远程调用。该注册列表信息定时(每30 秒) 更新一次。4)Cancel服务下线Client 在程序关闭时可以向Eureka Server 发送下线请求。发送请求后,该客户端的实例信息将从Server 的服务注册列表中删除。该下线请求不会自动完成,需要在程序关闭时调用以下代码: DiscoveryManager.getinstance().shutdownComponent(); 5) Eviction服务下线在默认情况下,当Client 连续90 秒没有向Server 发送服务续约(即心跳〉时,Server 会将该服务实例从服务注册列表删除,即服务下线。 二、Eureka案例代码 1、项目基本结构图 主要包括两个注册中心(集群) node01-eureka-7001 node01-eureka-7002 一个服务提供方 node01-provider-8001 一个服务消费方 node01-consume-8002 2、配置本机的Host文件 # cloud host 127.0.0.1 registry01.com 127.0.0.1 registry02.com 127.0.0.1 provider-8001.com 3、注册中心代码 1)Eureka注册中心依赖 <dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> 2)核心配置文件 server: port: 7001 spring: application: name: node01-eureka-7001 eureka: instance: hostname: registry01.com prefer-ip-address: true client: # false表示不向注册中心注册自己 register-with-eureka: false # false表示该端就是注册中心,维护服务实例,不去检索服务 fetch-registry: false service-url: # 单点注册中心 # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 集群注册中心 defaultZone: http://registry02.com:7002/eureka/ 这里采用集群的配置,如果有多个集群,逗号分隔,如下写法就好: defaultZone: http://registry02.com:7002/eureka/, http://registry02.com:7002/eureka/ 3)启动类注解 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer // 注册中心注解 public class Application_7001 { public static void main(String[] args) { SpringApplication.run(Application_7001.class,args) ; } } 这样注册中心代码完成。4)启动项目,如图 暂时没有服务注册进来。 4、服务提供方代码 1)核心依赖 <dependencies> <!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> 2)配置文件如下 server: port: 8003 spring: application: name: node01-provider-8001 eureka: instance: hostname: provider-8001 prefer-ip-address: true client: service-url: # 集群注册中心 defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/ 3)提供一个接口服务 @RestController public class ProviderController { @RequestMapping("/getInfo") public Map<String,String> getInfo (){ Map<String,String> infoMap = new HashMap<>() ; infoMap.put("作者:","知了一笑") ; infoMap.put("时间:","2019-05-18") ; infoMap.put("主题:","SpringCloud微服务框架") ; return infoMap ; } } 4)启动类上需要注解 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient // 本服务启动后会自动注册进eureka服务中 @EnableDiscoveryClient public class Application_8001 { public static void main(String[] args) { SpringApplication.run(Application_8001.class,args) ; } } 5、服务消费方代码 服务消费方和提供方的代码逻辑基本一致。1)配置文件这里不需要注册自己,只是单纯从注册中心获取服务提供方的消息。 server: port: 8002 spring: application: name: node01-consume-8002 eureka: client: register-with-eureka: false service-url: # 集群注册中心 defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/ 2)消费服务代码注意这里的【server_name】就服务提供方的 spring: application: name: node01-provider-8001 使用RestTemplate调用服务。 @RestController public class ConsumeController { @Autowired private RestTemplate restTemplate ; String server_name = "node01-provider-8001" ; @RequestMapping("/showInfo") public Map<String,String> showInfo (){ return restTemplate.getForObject("http://"+server_name+":8001/getInfo",Map.class) ; } } 到这里,一个基于Eureka的服务注册与发现就完成了。3)四个服务全部启动 4)访问如下地址 http://localhost:8002/showInfo 获取服务接口结果 { "主题:": "SpringCloud微服务框架", "作者:": "知了一笑", "时间:": "2019-05-18" } 三、源代码案例 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-cloud-base 码云地址:知了一笑 https://gitee.com/cicadasmile/spring-cloud-base

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册