专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Spring Cloud认知学习(三):声明式调用Feign的使用

目录

  • Feign
    • 使用示例
      • 1.导入依赖:
      • 2.新建Feign Interface
      • 3.创建服务消费者
      • 4.测试
    • 补充:

上一篇介绍微服务构建起来后,使用Ribbon来解决多个服务的负载均衡问题。 Spring Cloud认知学习(二):Ribbon使用

这一篇介绍一个用于声明式调用服务的组件Fegin,主要用于解决前面的服务调用与restTemplate耦合紧密的问题。


Feign

Feign用于声明式调用服务
在上面的服务调用中,我们始终还是没有摆脱restTemplate,我们调用别的服务始终要使用restTemplate来发起。想想我们以前是怎么开发的(三层架构,controller调用service,service调用dao),controller调用service,feign就是为这种面向接口化编程需求而产生的。为什么说他能面向接口化编程呢?我们下面来演示。
90_1.png

使用示例

代码参考:Feign简单使用实验
考虑到服务可能有多个消费者,所以我们把共有的代码写到spring-cloud-common-data中,这样所有的消费者都可以通过继承这个依赖包来获取Feign修饰的接口。

1.导入依赖:

spring-cloud-common-data中导入依赖:

<dependencies>
        <!--增加feign依赖 start-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--旧版本的-->
            <!--<artifactId>spring-cloud-starter-feign</artifactId>-->
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--增加feign依赖 end-->
    </dependencies>

2.新建Feign Interface

spring-cloud-common-data创建一个interface:

package com.progor.study.service;

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

import java.util.Map;

// 由于这种服务的服务消费者可能比较多,放到共有依赖中。
@FeignClient(value = "MESSAGESERIVE")
public interface MessageService {
    // 这里使用RequestMapping将服务提供者的方法与本地Service方法建立映射
    @RequestMapping(value = "/msg/list", method = RequestMethod.GET)
    Map<String, String> list();
}

@FeignClient用来配置信息,可以配置当前interface对应哪个服务。搭配@RequestMapping的效果就是调用对应服务的对应API接口。上面代码的效果就是从eureka中获取名为MESSAGESERIVE的服务的服务示例,调用服务实例的/msg/list路由方法。

3.创建服务消费者

3、1 在服务消费者spring-cloud-user-consumer-80中创建一个MessageController2,注入MessageService:

package com.progor.study.Controller;

import com.progor.study.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

// 这个控制器用来处理使用fegin的情况

@RestController
public class MessageController2 {

    // 注入MessageService
    @Autowired
    private MessageService messageService; 

    @GetMapping("/msg2/list")
    public Map<String, String> list() {
        return messageService.list();
    }

}

3.2、在spring-cloud-user-consumer-80启用Feign:
由于在spring-cloud-common-data中导入了fegin依赖,所以这里不需要再导入了。

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "USERSERIVE", configuration = MyConfig.class)
@EnableFeignClients // 使用feign
public class UserConsumer80Application {
    public static void main(String[] args) {
        SpringApplication.run(UserConsumer80Application.class, args);
    }
}

4.测试

4、1:启动spring-cloud-eureka-server-7001spring-cloud-message-service-8004spring-cloud-message-service-8005

4、2:访问http://localhost/msg2/list,这个URL调用的是MessageController2的API,而MessageController2里面调用的是被Fegin封装的MessageService的方法。
如果能访问成功,那说明了是我们使用Feign成功了。
从上面可以看出,使用了Feign之后,我们可以像以前一样调用Service来调用业务逻辑了。

补充:

  • Feign默认是有负载均衡的,看一下spring-cloud-starter-openfeign依赖包,你会发现它有导入依赖spring-cloud-starter-netflix-ribbon
  • 更多内容包括其工作原理,将会单章讲解,咕咕咕。

文章永久链接:https://tech.souyunku.com/19085

未经允许不得转载:搜云库技术团队 » Spring Cloud认知学习(三):声明式调用Feign的使用

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们