Spring Cloud服务发现/注册

简介: 服务发现/注册服务发现(Service Discovery)在计算机网络中,一种自发现设备或者服务的技术,通过服务发现协议(Service Discovery Protocol)实现。常见协议java:jini(Apache River)REST:HATEOASWeb Services:UDDI(Universal Description Discovery and lntegration)服务注册(Service Registration)在计算机网络中,为了更好地治理多个设备或者服务,这些设备或者服务主动或者被动注册到管理中心,以便服务被发现和消费。

服务发现/注册

服务发现(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

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

目录
相关文章
|
4天前
|
消息中间件 负载均衡 Java
【Spring Cloud 初探幽】
【Spring Cloud 初探幽】
13 1
|
6天前
|
Java 开发者 微服务
Spring Cloud原理详解
【5月更文挑战第4天】Spring Cloud是Spring生态系统中的微服务框架,包含配置管理、服务发现、断路器、API网关等工具,简化分布式系统开发。核心组件如Eureka(服务发现)、Config Server(配置中心)、Ribbon(负载均衡)、Hystrix(断路器)、Zuul(API网关)等。本文讨论了Spring Cloud的基本概念、核心组件、常见问题及解决策略,并提供代码示例,帮助开发者更好地理解和实践微服务架构。此外,还涵盖了服务通信方式、安全性、性能优化、自动化部署、服务网格和无服务器架构的融合等话题,揭示了微服务架构的未来趋势。
31 6
|
10天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
10天前
|
负载均衡 Java 开发者
Spring Cloud:一文读懂其原理与架构
Spring Cloud 是一套微服务解决方案,它整合了Netflix公司的多个开源框架,简化了分布式系统开发。Spring Cloud 提供了服务注册与发现、配置中心、消息总线、负载均衡、熔断机制等工具,让开发者可以快速地构建一些常见的微服务架构。
|
12天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
27 1
|
12天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
15 2
|
12天前
|
监控 Java Sentinel
Spring Cloud Sentinel:概念与实战应用
【4月更文挑战第28天】在分布式微服务架构中,确保系统的稳定性和可靠性至关重要。Spring Cloud Sentinel 为微服务提供流量控制、熔断降级和系统负载保护,有效预防服务雪崩。本篇博客深入探讨 Spring Cloud Sentinel 的核心概念,并通过实际案例展示其在项目中的应用。
23 0
|
12天前
|
Cloud Native Java Nacos
Spring Cloud Nacos:概念与实战应用
【4月更文挑战第28天】Spring Cloud Nacos 是一个基于 Spring Cloud 构建的服务发现和配置管理工具,适用于微服务架构。Nacos 提供了动态服务发现、服务配置、服务元数据及流量管理等功能,帮助开发者构建云原生应用。
20 0
|
15天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流