Spring Cloud Netflix(一)

Spring Cloud Netflix(一)

介绍

Spring Cloud Netflix 是由 Netflix 开源的,并且由 Spring 项目集成到 Spring Cloud 中的,主要用于构建大型分布式项目。

Spring Cloud Netflix 通过自动配置来绑定到 Spring 项目中,使用注解便可以快速启用相应的功能。

Spring Cloud Netflix 主要提供以下功能

  • Eureka:服务发现
  • Hystrix:断路器
  • Zuul:智能路由
  • Ribbon:客户端的负载均衡

Eureka

Eureka 主要提供服务注册、服务发现功能,这是微服务架构中的核心功能之一。

Eureka 主要分为 ServerClient

Client 是应用端,是向外提供的服务。

Server 是服务端,即注册中心,存储了所有已注册 Client 的元数据信息。例如:主机、端口、健康指标、首页等其他信息。

Client 会发送心跳给 Server,用于表明服务正常可用。如果 Server 不能定时接受到 Client 的心跳信息,便会将 ClientInstance 移除。但是如果在两次心跳之间服务挂掉,那么 Server 中的 Client 不会移出,有一定的信息延迟。

1. 搭建 Eureka-Server

1.1. 依赖

Eureka-Server 的依赖是 spring-cloud-starter-netflix-eureka-server

pom.xml

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

1.2. 主类

@EnableEurekaServer 注解是用来启动 Eureka 服务的。

EurekaServerApplication.java

package com.example;

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

/**
 * @author mw118
 * @version 1.0
 * @date 2021/1/8 22:26
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

1.3. 配置

server.port 用于表明 Eureka 服务的访问接口。

eureka.client.register-with-eureka 表示不在其他 Eureka 服务上注册,因为当前只有一个节点。

eureka.client.fetch-registry 表示是否从其他 Eureka 服务上获取注册表信息。

application.properties

server.port=8762

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

1.4. 启动并访问

访问 localhost:8761 ,便会展示 Spring Eureka 的网页信息。

其中红色方框内便会展示服务的实例信息,此时还没有服务注册到该节点上。

Eureka首页

2. 搭建 Eureka-Client

2.1. 依赖

Eureka-Client 的依赖是 spring-cloud-starter-netflix-eureka-client

pom.xml

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

2.2. 主类

@EnableDiscoveryClient 是客户端进行服务注册的。

EurekaClientApplication.java

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author mw118
 * @version 1.0
 * @date 2021/1/8 22:29
 */

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
    
    

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

2.3. 配置

spring.application.name 指明当前应用名称,会使用该名称的大写作为该应用的名称

eureka.client.serviceUrl.defaultZone 用于定位 Eureka 注册中心

application.properties

spring.application.name=eureka-client

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

2.4.启动

启动项目,并刷新 localhost:8761页面。此时在红色方框中,便会展示刚才启动的客户端服务。

显示的重要信息有:

  • Application:实例名称,默认使用客户端的环境变量 spring.application.name
  • Availability Zones:实例数量,如果部署了多个相同名称的服务,便会展示对应的数量
  • Status:展示了当前的实例状态(UP:表示为可用),以及访问的 URLURL 会指向实例的 /actuator/info

Eureka首页

2.5. 测试客户端关闭的场景

将刚刚启动的 Eureka-Client 关闭,并等待1分钟后,刷新 localhost:9761 的页面。

在图中,尽管 Eureka-Client 服务已经关闭了,但是在注册中心的列表中还存在该实例,并且状态为 UP

原因参考图中第一个红色方框中的内容,这是由于 Eureka 的自我保护机制,如果更新的心跳次数小于预期阈值,Eurka 服务不会将实例移除,防止因为网络引起的故障。

错误提示

资源

代码

参考及引用

猜你喜欢

转载自blog.csdn.net/qq_38685141/article/details/112385545