微服务从零到一 服务消费(Feign)

 微服务不仅仅是一种具体的事物,更是一种思维方式,一种程序语言解决现实问题的辨证思想

                                                              服务消费(Feign)

Spring Cloud Feign

Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

下面,我们通过一个例子来展现Feign如何方便的声明对provider服务的定义和调用。

1:修改pom.xml文件,新增如下jar包依赖。

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

2:修改应用主类。通过@EnableFeignClients注解开启扫描Spring Cloud Feign客户端的功能:

package com.lyh.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringCloudApplication
@EnableFeignClients
public class ConsumerApplication {

   

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

}

3:创建一个Feign的客户端接口定义。使用@FeignClient注解来指定这个接口所要调用的服务名称,接口中定义的各个函数使用Spring MVC的注解就可以来绑定服务提供方的REST接口,比如下面就是绑定consumer服务的/queryNameById接口的例子:

package com.lyh.consumer.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value="feign-provider")
@Service
public interface ComputeClient {
    @RequestMapping(method = RequestMethod.GET, value = "/queryNameById")
    String queryNameById(@RequestParam(value = "id") Long id);
}

3:修改Controller。通过定义的feign客户端来调用服务提供方的接口

package com.lyh.consumer.controller;


import com.lyh.consumer.service.ComputeClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ComputeController {

    private final Logger logger = LoggerFactory.getLogger(ComputeController.class);

    @Autowired
    ComputeClient computeClient;

 
    @Autowired
    RestTemplate restTemplate;


   

    /**
     * 通过定义的feign客户端来调用服务提供方的接口
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/queryNameById")
    String queryNameById(@RequestParam(value = "id") Long id){

        return computeClient.queryNameById(id);
    }

}

通过Spring Cloud Feign来实现服务调用的方式更加简单了,通过@FeignClient定义的接口来统一的生命我们需要依赖的微服务接口。而在具体使用的时候就跟调用本地方法一点的进行调用即可。由于Feign是基于Ribbon实现的,所以它自带了客户端负载均衡功能,也可以通过Ribbon的IRule进行策略扩展。另外,Feign还整合的Hystrix来实现服务的容错保护,在Dalston版本中,Feign的Hystrix默认是关闭的。待后文介绍Hystrix带领大家入门之后,我们再结合介绍Feign中的Hystrix以及配置方式。

在完成了上面你的代码编写之后,然后访问http://localhost:1003/queryNameById?id=2 ,最终也能达到负载均衡的目的。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值