您现在的位置是:首页 > 文章详情

Spring Cloud服务发现/注册

日期:2019-03-17点击:438

服务发现/注册

服务发现(Service Discovery)

在计算机网络中,一种自发现设备或者服务的技术,通过服务发现协议(Service Discovery Protocol)实现。

常见协议

  • java:jini(Apache River)
  • REST:HATEOAS
  • Web Services:UDDI(Universal Description Discovery and lntegration)

服务注册(Service Registration)

在计算机网络中,为了更好地治理多个设备或者服务,这些设备或者服务主动或者被动注册到管理中心,以便服务被发现和消费。

常见注册中心

  • Apache Zookeper 性能最差
  • Netflix Eureka 高可用,高 一致性差
  • Consul 高可用,高一致性(某些方面)
    c2

c1

高可用架构

高可用(High Availability)

一种系统特性,致力于确保可接受程度的执行操作,通常采用上线时间作为基准。其中,以一年内的上线时间与自然时间的比率来描述可用性。

基本原则

  • 消灭单点故障
  • 可靠性交迭
  • 故障探测

Spring Cloud Netflix Eureka

服务发现:Eureka

Eureka是由Netflix公司发明的服务发现中间件,包括服务发现服务器和客户端的。

核心组件

  • Eureka Server
  • Eureka Client
    服务端:Eureka Server

Eureka Server是Eureka Client的注册服务中心,管理所有注册服务、以及其实例信息和状态。

运行Eureka Server

  • 依赖:org.springframework.cloud:spring-cloud-starter-eureka-server
  • 激活:@EnableEurekaServer

1.引入Maven依赖

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>

2.激活Erueka服务器

package com.example.springcloudlesson4eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class SpringCloudLesson4EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudLesson4EurekaServerApplication.class, args); } }

3.调整Eureka服务器配置
application.properties

##服务器应用名 spring.application.name=spring-cloud-eureka-server ##服务器端口 server.port=9090 ##管理端口安全失效 management.security.enabled=false

检查Eureka 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:111) ~[eureka-client-1.7.2.jar:1.7.2] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.7.2.jar:1.7.2] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.7.2.jar:1.7.2] at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.7.2.jar:1.7.2] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.7.2.jar:1.7.2] at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1027) [eureka-client-1.7.2.jar:1.7.2]

检查Eureka Server

http://localhost:9090
运行效果:
c3

问题原因:Eureka Server即是注册服务器,也是客户端,默认情况,也需要配置注册中心地区。

客户端:Eureka Client

Eureka Client为当前服务提供注册、同步、查找服务以及其实例信息或状态等能力。

运行Eureka Client

  • 依赖:org.springframework.cloud:spring-cloud-starter-eureka
  • 激活:@EnableEurekaClient或者@EnableDiscoveryClient

1.引入Maven依赖

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>

2.激活Eureka客户端

package com.example.springcloudlesson4eurekaclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class SpringCloudLesson4EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudLesson4EurekaClientApplication.class, args); } }

@EnableDiscoveryClient比@EnableEurekaClient好一些
因为不绑定某种实现,注册不到,服务不会发现而这个重启一下就好了
3.配置Euraka 客户端

 ##客户端应用名称 spring.application.name=spring-cloud-eureka-client ##客户端端口 server.port=8080 ##管理端口安全失效 management.security.enabled=false

4.启动时会报连接拒绝错误

检验Eureka客户端
发现与Eureka服务器端出现相同异常

需要再次调整Eureka客户端

 ##客户端应用名称 spring.application.name=spring-cloud-eureka-client ##客户端端口 server.port=8080 ##管理端口安全失效 management.security.enabled=false ##Spring Cloud Eureka客户端 注册到服务器 eureka.client.serviceUrl.defaultZone=http://localhost:9090/eureka 

配置完成后出现下面截图情况表示连接成功
_

原文链接:https://yq.aliyun.com/articles/694185
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章