【微服务笔记】服务发现

2019-09-26  本文已影响0人  程序员Anthony

背景介绍

服务中心又称注册中心,管理各种服务功能包括服务的注册、发现、熔断、负载、降级等。
通过服务中心来获取服务你不需要关注你调用的项目IP地址,由几台服务器组成,每次直接去服务中心获取可以使用的服务去调用既可。

由于各种服务都注册到了服务中心,就有了去做很多高级功能条件。比如几台服务提供相同服务来做均衡负载;监控服务器调用成功率来做熔断,移除服务列表中的故障点;监控服务调用时间来对不同的服务器设置不同的权重等等。

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 的服务,主要在 AWS 云中使用, 定位服务来进行中间层服务器的负载均衡和故障转移。

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现。Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server,并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。Spring Cloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。

Eureka由两个组件组成:Eureka服务器和Eureka客户端。Eureka服务器用作服务注册服务器。Eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。

上图简要描述了Eureka的基本架构,由3个角色组成:

1、Eureka Server

提供服务注册和发现
2、Service Provider

服务提供方
将自身服务注册到Eureka,从而使服务消费方能够找到
3、Service Consumer

服务消费方
从Eureka获取注册服务列表,从而能够消费服务

案例实践

Eureka Server

spring cloud已经帮我实现了服务注册中心,我们只需要很简单的几个步骤就可以完成。
1、pom中添加依赖

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

2、添加启动代码中添加@EnableEurekaServer注解

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

3、配置文件

在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,在application.yml 添加以下配置:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false  # not register to Eureka Server
    fetch-registry: false # not get register info from Eureka Server
    service-url:  # Eureka Server register url for communication of clients and servers
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Eureka Client

1、pom中添加依赖

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

2、添加启动代码中添加@EnableEurekaServer注解

@EnableDiscoveryClient
@SpringBootApplication
public class AntPayWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(AntPayWebApplication.class, args);
    }

}

3、配置文件

server:
  port: 8765
spring:
  application:
    name: ant-pay

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:  # Eureka Server注册中心的地址,用于client与server进行交流
      defaultZone: http://localhost:8761/eureka/

查看Eureka服务注册中心

搭建好上述的两个的Eureka应用后,依次启动Server和Client。

访问地址Eureka Server的主页http://localhost:8761,可以看到以下界面:

与服务注册中心交换信息

DiscoveryClient来源于spring-cloud-client-discovery,是SpringCloud中定义的用来服务发现的顶级接口,在SpringCloud的各类服务发现组件中(如Netflix+Eureka或者Consul)都有相应的实现。它提供从服务注册中心中根据serviceId的获取到对应的服务实例信息的能力。当一个服务实例拥有DiscoveryClient的具体实现时,就可以从Eureka Server中发现其他的服务实例。在Eureka Client中注入DiscoveryClient,并从Eureka Server 获取服务实例的信息,这里添加一个ServiceInstanceRestController

@RestController
public class ServiceInstanceRestController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping("/service-instances/{applicationName}")
    public List<ServiceInstance> serviceInstancesByApplicationName(
            @PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }
}

启动应用后,访问地址http://localhost:8765/service-instances/ant-pay,获取应用名为ant-pay的(服务本身)服务实例的元数据,结果如下:

Eureka中的标准元数据有主机名、IP地址、端口号、状态页url和健康检查url等,这些元数据都会保存在Eureka Server的注册信息表中,Eureka Client通过根据服务名读取这些元数据来发现和调用其他的服务实例。

对应的代码已经提交到github 项目AntMicroService
下面是对应的tag,可以对应查看

参考文章

服务注册与发现组件 Eureka 应用实

服务注册与发现组件 Eureka 客户端实现原理解析

Github Netflix eureka wiki

springcloud(二):注册中心Eureka

上一篇下一篇

猜你喜欢

热点阅读