springcloud 服务消费及熔断

目录

1. 服务消费方式

1.1 RestTemplate

1.2 feign

2. 服务熔断(降级)

2.1 在微服务架构中服务熔断的必要性

2.2 hystrix


1. 服务消费方式

1.1 RestTemplate

传统情况下在java代码里访问restful服务,一般使用Apache的HttpClient。不过此种方法使用起来太过繁琐。spring提供了一种简单便捷的模板类来进行操作,这就是RestTemplate

示例:

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApp {


    //启用负载均衡
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            System.out.println("服务消费者 .......... ");
            return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
        }
    }


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

}

1.2 feign

feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service。Spring Cloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端。

1)导入包

    <!-- 声明式服务调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2)SpringbootApplication启动类加上@EnableFeignClients

3)定义服务接口

4)使用Feign接口

package com.zking.nacos.controller;

import com.zking.nacos.service.ProviedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ProviedService proviedService;

    @RequestMapping(value = "/echou/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) {
        System.out.println("服务消费者 .......... ");
        return restTemplate.getForObject("http://service-proved/echo/" + str, String.class);
    }


    @RequestMapping(value = "/echoFeign/{str}", method = RequestMethod.GET)
    public String echoFeign(@PathVariable String str) {
        System.out.println("服务消费者echo .......... ");
        return proviedService.echo(str);
    }
}

2. 服务熔断(降级)

2.1 在微服务架构中服务熔断的必要性

在互联网系统中,当下游服务因访问压力过大而响应变慢或失败,上游服务为了保护系统整体的可用性,可以暂时切断对下游服务的调用。这种牺牲局部,保全整体的措施就叫做熔断


熔断机制相当于生活中的比较接近的例子就是电容丝,在用电量过载时会发生熔断,以避免灾难性的后果。在微服务架构中往往会包含大量的微服务,且这些微服务之间会发生互相调用,如果此时某个微服务发生故障,则可能产生连锁反应,在一些资料中将这种现象称为微服务雪崩。为了避免这种情况的发生就有了微服务调用的熔断机制,这种机制可以对发生问题的服务及时进行隔离,避免微服务雪崩的出现。

健康的微服务集群:

出现故障:

系统雪崩:

注: 在进行微服务的设计时,不要出现循环依赖。

2.2 hystrix

在springclound中熔断器的的组件是Hystrix,是Netflix套件的一部分。其作用是在微服务相互调用中出现故障时,对故障进行隔离,使得故障不至于蔓延到中分布式系统。

2.3 hystrix的使用

1)导入需要的依赖包

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

Feign已经集成了对Hytrixde支持,所以不用专门导入Hytrix依赖。如果不是在Feign中使用熔断器,则需要导hystrix依赖,例如下面要讲到的ribbon。

2)配置文件

 开启熔断功能
feign.hystrix.enabled=true

3)编写熔断事件发生时的处理程序 

package com.zking.nacos.service;

import org.springframework.stereotype.Component;

@Component
public class ProviedServiceFallback implements ProviedService{
    @Override
    public String echo(String string) {
        return "provied fail ,,,,错误:"+string;
    }
}
  1. 在feign接口中配置熔断

package com.zking.nacos.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Service("proviedService")
@FeignClient(name="service-proved",fallback = ProviedServiceFallback.class)
public interface ProviedService {
    @GetMapping(value = "/echo/{string}")
     String echo(@PathVariable("string") String string);
}

然后把 service-proved模块关闭进行测试

猜你喜欢

转载自blog.csdn.net/qq_62898618/article/details/128268713
今日推荐