Eureka、Feign、Hystrix

eureka介绍

Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目 spring-cloud-netflix中,实现SpringCloud的服务发现功能。

Eureka包含两个组件: Eureka ServerEureka Client
Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注 册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点 的信息可以在界面中直观的看到。

Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也 就别一个内置的、使用轮询(round-robin)负载算法的负载均衡器。
在应用启动后,将会 向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有 接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90 秒)

Eureka Server集群之间通过复制的方式完成数据的同步,

Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务 的API。

综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活 性和可伸缩性。

如果在Eureka Server的首页看到以下这段提示,则说明Eureka已经进入了保护模式:

Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,如果出 现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳 定导致),

Eureka Server会将当前的实例注册信息(客户端)保护起来,同时提示这个警告。

保护 模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保 护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的 数据(也就是不会注销任何微服务)。

Eureka服务端开发

在父工程中引入依赖定义springCloud版本

<dependencyManagement> 
	<dependencies> 
		<dependency> 
			<groupId>org.springframework.cloud</groupId> 
			<artifactId>spring‐cloud‐dependencies</artifactId> 
			<version>Finchley.M9</version> 
			<type>pom</type> 
			<scope>import</scope> 
		</dependency> 
	</dependencies> 
</dependencyManagement>

tensquare_eureka模块pom.xml引入eureka-server

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

application.yml

server:
  port: 6868
eureka:
  client:
    #是否把自己服务注册到eureka服务中,默认值是true
    #这里是搭建eureka服务,不需要自己注册到自己里面,设置为false,所以Eureka客户端这个配置和下面那个配置不需要配置,因为默认都是true
    register-with-eureka: false
    #是否从eureka中获取去注册信息,自己就是eureka服务,不需要获取注册信息
    fetch-registry: false
    #设置eureka服务的地址
    service-url:
      defaultZone: http://127.0.0.1:${server.port}/eureka/

开启eureka

package com.tensquare.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

Eureka客户端开发

依赖

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

application.yml

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:6868/eureka/
  instance:
    #获取服务相关的ip地址
    prefer-ip-address: true

开启eureka客户端

在spring的启动类添加@EnableEurekaClient

Feign

依赖

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

Feign开启服务调用

package com.tensquare.qa;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import util.IdWorker;

@SpringBootApplication
@MapperScan("com.tensquare.qa.mapper")
@EnableEurekaClient
// 开启发现微服务客户端
@EnableDiscoveryClient
// 开启调用微服务客户端
@EnableFeignClients
public class QaApplication {

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

    @Bean
    public IdWorker createIdWorker(){
        return new IdWorker(1,1);
    }
}

调用服务

package com.tensquare.qa.client;

import entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

// 调用的微服务名,在配置文件中配置
@FeignClient("tensquare-base")
public interface LabelClient {

    // 需要调用哪个服务的接口,直接复制过来就可以
    // 针对外界来说是接口,针对服务来说,其实是调用其他服务的controller

    // 1.请求路径需要保证完整,也就是把controller类上的那个路径加过来
    // 2.@PathVariable中的值必须写
    @GetMapping("label/{labelId}")
    Result findById(@PathVariable("labelId") String labelId);
}

为什么要使用熔断器

在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障, 进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应。服务雪崩效应是一种 因“服务提供者”的不可用导致“服务消费者”的不可用,并将不可用逐渐放大的过程。

如下图所示:A作为服务提供者,B为A的服务消费者,C和D是B的服务消费者。A 不可用引起了B的不可用,并将不可用像滚雪球一样放大到C和D时,雪崩效应就形成 了

在这里插入图片描述

Hystrix 能使你的系统在出现依赖服务失效的时候,通过隔离系统所依赖的服务(隔离挂掉的服务),防 止服务级联失败,同时提供失败回退机制(服务挂掉又好了),更优雅地应对失效,并使你的系统能更快地 从异常中恢复。

熔断器模式


半开:会测试之前失败的服务是否可用,可用关闭熔断器,不可用打开熔断器。

熔断器使用

Feign 本身支持Hystrix,不需要额外引入依赖。

application.yml
// 使用feign自带的熔断器
feign: 
	hystrix: 
		enabled: true

开启熔断器

package com.tensquare.qa.client;

import com.tensquare.qa.client.impl.LabelClientImpl;
import entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//声明该类是使用Feign的客户端,调用其他的微服务
//注解里面的value属性就是需要调用的微服务的名字
//fallback属性就是熔断器打开后使用的方案
@FeignClient(value = "tensquare-base", fallback = LabelClientImpl.class)
public interface LabelClient {

    //在问答微服务调用基础微服务,实现根据标签id查询标签功能
    //需要调用哪个微服务的接口,直接把这个微服务的接口方法复制过来
    //还需要修改两个地方
    //1. 保证value属性必须是完整的访问路径
    //2. 保证@PathVariable属性必须是对应的占位符的名字,不能省略

    //GET /label/{labelId} 根据ID查询
    @RequestMapping(value = "label/{labelId}", method = RequestMethod.GET)
    //@ResponseBody
    public Result findById(@PathVariable("labelId") String labelId);
}

熔断器备用方案


package com.tensquare.qa.client.impl;

import com.tensquare.qa.client.LabelClient;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Component;

@Component
public class LabelClientImpl implements LabelClient {

    @Override
    public Result findById(String labelId) {
        //编写备用方案,基础微服务调用失败,使用的方案
        return new Result(false, StatusCode.ERROR, "基础我微服务宕机,打开熔断器");
    }
}

有的事就是这样,说不清楚,越活越糊涂,永远也不知道规则是什么,而潜规则又不是每个人都明白的。

鬼吹灯
天下霸唱

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的Spring MVC项目,集成了EurekaFeign,您可以参考: 1. 首先,需要在pom.xml中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 在启动类上添加@EnableEurekaClient注解启用Eureka客户端 ```java @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 创建一个Feign客户端接口,用于定义需要调用的服务API ```java @FeignClient("service-provider") public interface ServiceProviderClient { @GetMapping("/hello") String hello(); } ``` 4. 在控制器中注入ServiceProviderClient,并调用其hello方法 ```java @RestController public class HelloController { @Autowired private ServiceProviderClient serviceProviderClient; @GetMapping("/hello") public String hello() { return serviceProviderClient.hello(); } } ``` 5. 在application.yml中配置EurekaFeign ```yaml spring: application: name: service-consumer eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 ``` 6. 启动Eureka服务器和服务提供者,然后启动服务消费者,访问http://localhost:8080/hello即可看到服务提供者返回的结果。 希望这个例子能够帮助到您!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值