美文网首页随笔-生活工作点滴
SpringCloud 远程调用Feign

SpringCloud 远程调用Feign

作者: 黄靠谱 | 来源:发表于2019-07-05 11:07 被阅读0次

    参考

    http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html

    概述

    Feign是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。
    Feign通过处理注解,将请求模板化,当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。

    Feign = RestTemplate + Spring全家桶(Eureka + Ribbon + 熔断 + Zipkin)

    • RestTemplate + Eureka 加持了 注册中心的关环,使得调用变得非常简洁,简洁到只需要指明 application.name + methodName
    • Eukeka +Ribbon 使得负载均衡功能可以实现(默认轮询)
    • 熔断机制,实现失败返回和重试等机制。类似于Dubbo的重试策略
    • Zipkin 链路监控,实现类似Dubbo 监控平台的功能

    所以对于服务提供者而言是无感知的,依然正常的对外开放Http接口。

    功能点

    1. 基础的远程调用
    2. 负载均衡策略
    3. 熔断策略

    服务消费端

    • 启动类使用@EnableFeignClients 注解
    • 远程调用接口使用@FeignClient 指定,服务提供方的 application.name
    • @RequestMapping指定要调用的接口方法
    1. @EnableFeignClients+ @FeignClient ,consumer启动类的注解,表示该消费者会发起远程调用
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    }
    
    @RestController
    public class ConsumerController {
        @Autowired
        HelloRemote HelloRemote;
        @RequestMapping("/hello/{name}")
        public String index(@PathVariable("name") String name) {
            return HelloRemote.hello(name);
        }
    }
    
    @FeignClient(name= "spring-cloud-producer")
    public interface HelloRemote {
        @RequestMapping(value = "/hello")
        public String hello(@RequestParam(value = "name") String name);
    }
    

    依赖jar包,版本号 和springcloud的不一样

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

    相关文章

      网友评论

        本文标题:SpringCloud 远程调用Feign

        本文链接:https://www.haomeiwen.com/subject/rseshctx.html