eureka介绍
Eureka是Netflix开发的服务发现框架
,SpringCloud将它集成在自己的子项目 spring-cloud-netflix中,实现SpringCloud的服务发现功能。
Eureka包含两个组件: Eureka Server
和Eureka 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, "基础我微服务宕机,打开熔断器");
}
}
有的事就是这样,说不清楚,越活越糊涂,永远也不知道规则是什么,而潜规则又不是每个人都明白的。
鬼吹灯
天下霸唱