项目学习第八天Hystrix,Zuul,SpringCloudConfig

项目学习第八天Hystrix,Zuul,SpringCloudConfig

熔断器Hystrix

微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应。服务雪崩效应是一种因“服务提供者”的不可用导致“服务消费者”的不可用,并将不可用逐渐放大的过程。
Hystrix是Spring Cloud提供的一种带有熔断机制的框架,出现远程调用失败的时候提供一种机制来保证程序的正常运行而不会卡死在某一次调用。
Feign 支持Hystrix,修改对应模块的application.yml ,开启hystrix。

feign:
  hystrix:
   enabled: true

创建熔断实现类,实现自接口

@Component
public class LabelClientImpl implements LabelClient {
@Override
  public Result findById(String id) {
   return new Result(false, StatusCode.ERROR,"启动熔断器");
 }
}

修改接口的注解

@FeignClient(value="tensquare‐base",fallback = LabelClientImpl.class)
微服务网关Zuul

ZUUL是Netflix开源的微服务网关,它可以和Eureka、Ribbon、Hystrix等组件配合使用,它主要用作反向代理、Filter扩展、动态加载、动态路由、压力测试、弹性扩展、审查监控、安全检查等。
 (1)创建子模块manager,pom.xml引入eureka-client 和zuul的依赖

<dependencies>
  <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‐netflix‐zuul</artifactId>
</dependency>
</dependencies>

创建application.yml

server:
  port: 9011
spring:
  application:
    name: tensquare-manager
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:6868/eureka/
  instance:
    prefer-ip-address: true
jwt:
  config:
    key: itcast
zuul:
  routes:
    tensquare-base:
      path: /base/**
      serviceId: tensquare-base
    tensquare-user:
      path: /user/** #配置请求URL的请求规则  
      serviceId: tensquare-user #指定Eureka注册中心中的服务id
    tensquare-qa:
      path: /qa/** #配置请求URL的请求规则  
      serviceId: tensquare-qa #指定Eureka注册中心中的服务id

编写启动类

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

集中配置组件SpringCloudConfig

在分布式系统中,由于服务数量多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。
首先将配置文件放到GitHub上。
在这里插入图片描述
创建工程模块 配置中心微服务config ,pom.xml引入依赖

<dependencies>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring‐cloud‐config‐server</artifactId>
  </dependency>
</dependencies>

创建启动类ConfigServerApplication

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

编写配置文件application.yml,***为自己的git地址。

spring:
 application:
  name: tensquare‐config
 cloud:
  config:
  server:
 git:
   uri: https://git*****.git
server:
port: 12000

配置客户端

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring‐cloud‐starter‐config</artifactId>
</dependency>

添加bootstrap.yml ,删除application.yml

spring:
 cloud:
  config:
   name: base
   profile: dev
   label: master
   uri: http://127.0.0.1:12000

面试问题总结

为什么要使用断路器Hystrix?

在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC)。为了保证其高可用,单个服务有必须集群部署。由于网络原因或者自身的原因,服务并不能保证服务的100%可用,如果单个服务出现问题,调用这个服务就会出现网络延迟,此时若有大量的网络涌入,会形成任务累计,导致服务瘫痪,甚至导致服务“雪崩”。为了解决这个问题出现了短路器,相当于现实生活中的保险丝。

什么是服务雪崩?

多个服务之间调用的时候,假设服务A调用服务B和服务C,服务B和服务C又调用其他的服务,这就是所谓的“扇出”。如果“扇出”的链路上某个服务调用响应时间过长或者不可用,对服务A的调用就会占用越来越多的系统资源,
进而引起系统崩溃,所谓的 “ 雪崩效应 ”。、

Zuul和Nginx的区别?

相同点:Zuul和Nginx都可以实现负载均衡、反向代理(隐藏真实ip地址),过滤请求,实现网关的效果
不同点:
Nginx–c语言开发
Zuul–java语言开发
Zuul负载均衡实现:采用ribbon+eureka实现本地负载均衡
Nginx负载均衡实现:采用服务器实现负载均衡
Nginx相比zuul功能会更加强大,因为Nginx整合一些脚本语言(Nginx+lua)
Nginx适合于服务器端负载均衡
Zuul适合微服务中实现网

什么是SpringCloudConfig?

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
使用:
1、添加pom依赖
2、配置文件添加相关配置
3、启动类添加注解@EnableConfigServer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值