当前位置: 移动技术网 > IT编程>开发语言>Java > 玩转SpringCloud 二.服务消费者(1)ribbon+restTemplate

玩转SpringCloud 二.服务消费者(1)ribbon+restTemplate

2018年09月11日  | 移动技术网IT编程  | 我要评论

 此文章基于:

玩转springcloud 一.服务的注册与发现(eureka)

玩转springcloud 二.服务消费者(1)ribbon+resttemplate

转springcloud 二.服务消费者(2)feign

 

三.断路器(hystrix

 

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(rpc),在spring cloud可以用来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应

 

为了解决这个问题,业界提出了断路器模型。

 

 

一、断路器简介

netflix开源了hystrix组件,实现了断路器模式,springcloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的,如下图:

 

 

较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值hystric 是5秒20次) 断路器将会被打开。

 

断路打开后,可用避免连锁故障fallback方法可以直接返回一个固定值。

 

二、准备工作

 

启动demo1 工程;启动demo2工程,它的端口为8763

 

 

三、ribbon使用断路器

基于 玩转springcloud 二.服务消费者(1)ribbon+resttemplate

 项目架构:

 

改造demo3 工程的代码,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依赖:

<dependency>
   <groupid>org.springframework.cloud</groupid>
   <artifactid>spring-cloud-starter-netflix-hystrix</artifactid>
</dependency>

在程序的启动类serviceribbonapplication 加@enablehystrix注解开启hystrix:

@enableeurekaclient
@enablediscoveryclient
@springbootapplication
@enablehystrix
public class demo3application {

   public static void main(string[] args) {
      springapplication.run(demo3application.class, args);
   }


   @bean
   @loadbalanced
   resttemplate resttemplate() {
      return new resttemplate();
   }
}

 注解解析:

@enablehystrix

1.开启熔断机制

 

 

改造helloservice类,在hiservice方法上加上@hystrixcommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackmethod熔断方法,熔断方法直接返回了一个字符串,字符串为”hi,”+name+”,sorry,error!”

@service
public class helloservice {

    @autowired
    resttemplate resttemplate;

    @hystrixcommand(fallbackmethod = "hierror")
    public string hiservice(string name) {
        return resttemplate.getforobject("http://service-hi/hi?name="+name,string.class);
    }

    public string hierror(string name) {
        return "hi,"+name+",sorry,error!";
    }
}

注解解析:

@hystrixcommand

1. 指定断路后执行的方法,通过调用服务,实现fallback方法

2. 实现在spring cloud中使用hystrix组件:

 

 

启动:service-ribbon 工程,当我们访问,浏览器显示:

 

此时关闭 service-hi 工程,当我们再访问,浏览器会显示:

 

 

这就说明当 demo2工程不可用的时候,demo3调用 demo2api接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞

 

四、feign中使用断路器

基于 玩转springcloud 二.服务消费者(2)feign

 项目架构:

 

基于demo4工程进行改造

feign是自带断路器的,在d版本的spring cloud之后,它没有默认打开。需要在配置文件中配置打开它,在yml最后一行加入:

 

feign.hystrix.enabled: true

 

基于demo4工程进行改造,只需要在feignclient的schedualservicehi接口的注解中加上fallback的指定类就行了:

@feignclient(value = "service-hi",fallback = schedualservicehihystric.class)
public interface schedualservicehi {
    @requestmapping(value = "/hi",method = requestmethod.get)
    string sayhifromclientone(@requestparam(value = "name") string name);
}

schedualservicehihystric需要实现schedualservicehi 接口,并注入到ioc容器中,

@component
public class schedualservicehihystric implements schedualservicehi {
    @override
    public string sayhifromclientone(string name) {
        return "sorry "+name;
    }
}

注解解析:

 

@component

 

1. 把普通pojo实例化到spring容器中

 

2. 泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@controller、@services等的时候),我们就可以使用@component来标注这个类。

 

 

可能出现的错误:

 

那是因为

这里必须写实现类.class

 

启动四demo4工程,浏览器打开,注意此时demo2工程已经启动,网页显示:

 

 

关闭demo2工程,重新访问

这证明断路器起到作用了。

 

 

 

 

                          未完,待续。。。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网