SpringCloud系列(一)服务注册

229 阅读3分钟

这是我参与更文挑战的第 10 天,活动详情查看: 更文挑战

一、Eureka

1、概述

  • 服务治理:传统 grp 远程调用中,服务与服务之间关系复杂,管理比较复杂,所以使用服务治理管理服务之间的依赖关系,可以实现服务调用、负载均衡、容错、服务发现与注册。
  • 服务注册:Eureka 是 C/S 设计架构,注册中心作为服务端,生产者和消费者作为客户端,并和服务端保持心跳连接,在服务器启动时,会把注册中心的信息以别名的方式注册到注册中心,消费者或生产者以该别名去注册中心获取到实际的服务通信地址,然后实现本地的 rpc 远程调用。

两个组件

  • Eureka Server:提供服务注册
  • Eureka Client:通过注册中心进行访问,在应用启动后,将会向 Server 发送心跳。如果 Server 多个心跳周期没有收到某个节点的心跳,Server 就会把这个节点移除(默认 90 秒)

2、环境搭建

注册中心Server端

依赖

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

配置

server:
  port: 7001
eureka:
  instance:
    # eureka注册中心实例名称
    hostname: localhost
  client:
    # 不注册自己
    register-with-eureka: false
    # 不用去检索服务
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Server主启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class, args);
    }
}

注册中心Client端

依赖

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

Client配置

server:
  port: 8001

spring:
  application:
    # 在注册中心标识当前服务的名称
    name: cloud-payment-service


eureka:
  instance:
    # 修改主机名
    prefer-ip-address: true
    instance-id: payment8001
  client:
    # 注册到注册中心
    register-with-eureka: true
    # 从server抓取自己的注册信息
    fetch-registry: true
    service-url:
      # 注册的server的地址
      defaultZone: http://localhost:7001/eureka

Client主启动

@EnableEurekaClient
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class, args);
    }
}

3、注册中心集群

为了避免注册中心的宕机引发的一系列问题,把注册中心改为集群模式。并且开启负载均衡模式。

Server端配置中设置多个注册中心相互守望相互注册

eureka:
  instance:
    # eureka注册中心实例名称
    hostname: localhost
  client:
    # 不注册自己
    register-with-eureka: false
    # 不用去检索服务
    fetch-registry: false
    service-url:
      #搭建注册中心的集群,多个注册中心互相守望,其他注册中心也注册当前的注册中心
      defaultZone: http://xxx-PC:7002/eureka/

client端配置同时注册到多台注册中心上

client:
  # 注册到注册中心
  register-with-eureka: true
  # 从server抓取自己的注册信息
  fetch-registry: true
  service-url:
    # 注册的server的地址
    defaultZone: http://localhost:7001/eureka,http://xxx-PC:7001/eureka

配置Configuration类设置远程调用

@SpringBootConfiguration
public class ApplicationConfig {
    @Bean
    @LoadBalanced //开启负载均衡
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

多个消费者通过注册中心就可以调用其注册中心上的其他提供服务的模块,并且具有负载均衡的功能。

二、Zookeeper

导入依赖,注意排除依赖冲突

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </exclusion>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>

        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.9</version>
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

配置

server:
  port: 8080


spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      connect-string: localhost:2181

创建生产者和消费者,同时注册到zookeeper,主启动类加@EnableDiscoveryClient 注解。使用RestTemplate远程调用。实现通过consumer调用provider。

三、Consul

  • 安装并启动Consul
  • 默认访问地址http://localhost:8500/

依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

配置

server:
  port: 8080
spring:
  application:
    name: cloud-cousumer-payment
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

四、三个注册中心的区别

  • CAP理论(针对数据):
    • C:强一致性
    • A:可用性
    • P:分区容错性
  • Eureka 为 AP 模式,如果服务暂时不可用,在段时间没有发送心跳,Eureka也不会删除这个服务,而是暂时保留。
  • Consul\zookeeper 为 CP 模式,强调数据的一致性,当服务不可用时会立即从注册中心删除服务。