SpringCloud--Eureka

Eureka服务注册与发现

  • Eureka基础知识
  • 服务治理
  • 服务注册
  • Eureka两组建件

注册中心-Eureka

介绍
又称服务中心,管理各种服务功能包括服务的注册、发现、熔断、负载、降级等。

==什么是注册与发现==

  • 在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息,比如 服务地址通讯地址等以别名等方式注册到注册中心上,另一方(消费者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后在实现本地RPC调用RPC远程调用框架核心设计思想:自阿雨注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(==服务治理概念==)。在任何rpc远程框架中,都会有一个注册中心(存放接口地址)

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

SpringCloud--Eureka


它主要包括两个组件:Eureka Server 和 Eureka Client

  • Eureka Server:提供服务注册和发现的能力,各个微服务节点通过配置启动后,会在Eureka Server 中进行注册,这样EurekaServer中服务注册表中将会存储所有可用节点的信息,在ui界面可以看到(通常就是微服务中的注册中心)
  • Eureka Client:一个Java客户端,用于简化与 Eureka Server 的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在启动后,将会向Eureka Server发送心跳(默认周期30秒),如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个节点移除(默认90秒)(通常就是微服务中的客户端和服务端)

单机版的eureka

1.新建Maven工程 POM 添加 eureka-server

<!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 一般通用配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.编写yml 文件

#微服务建议一定要写服务端口号和微服务名称
server:
  #端口号
  port: 7001

eureka:
  instance:
    hostname: localhost
  client:
    #false表示不向注册中心注册自己(想注册也可以,不过没必要)
    register-with-eureka: false
    #false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与eurekaServer交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.编写启动类

package com.yxl.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer //表示此项目是eureka的服务注册中心
@SpringBootApplication
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class, args);
    }
}

4.测试

启动项目,在浏览器输入http://localhost:7001/

SpringCloud--Eureka


将EurekaClient端 注册进EurekaServer成为服务提供者provider

1.新建Maven工程 引入POM

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

2。新建yml 添加

eureka:
  client:
    #true表示向注册中心注册自己,默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

3.在主配置类上加上@EnableEurekaClient注解,表示这个项目是eureka的客户端。

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

4.启动项目,然后刷新页面,成功注册进注册中心。
SpringCloud--Eureka
SpringCloud--Eureka

上一篇:Ribbon负载均衡与原理解析


下一篇:猎鹰网络安全工具新增对站点 SSL 证书安全检测功能