Spring Cloud alibaba系列之Feign

JAVA项目中如何实现接口调用?

Httpclient

Httpclient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持Http协议的客户端编程工具包,并且它支持HTTP协议最新版本和建议。HttpClient相比传统JDK自带的URL Connection,提升了易用性和灵活性,是客户端发送HTTP请求变得容易,提高了开发的效率。

Okhttp

一个处理网络请求的开源项目,是安卓端最火的轻量级框架,由Square公司贡献,用于代替HttpUrlConnection和Apache HttpClient。OkHttp拥有简洁的API、高校的性能,并支持多种协议(HTTP/2和SPDY)

HttpURLConnection

HttpURLConnection是Java的标准类,它继承自URLConnection,可用于向指定网站发送GET请求、POST请求。HttpURLConection使用比较复杂,不想HttpClient那样容易使用。

RestTemplate WbClient

RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程HTTP服务的方法,能够大大提高客户端的编写效率。
上面介绍的是最常见的几种调用接口的方法,我们下面介绍的方法比上面的更简单、方便,它就是Feign。

什么是Feign

Feign是Nexflix开发的声明式、模板化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。Feign可帮助我们更加便捷、优雅地调用HTTP API。

#####优势
Feign可以做到,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。它像Dubbo一样,consumer直接调用接口方法调用provider,而不是要通过常规的HttpClient构造请求解析返回数据。它解决了让开发者调用远程接口就跟调用本地方法一样,无需关注与远程的交互细节,更无需关注分布式环境开发。(简单地说就是A服务的Service接口去调用别的B服务的Controller接口)

Spring Cloud Alibaba快速整合Opne Feign
复制order-nacos项目,修改项目名为order-openfeign,修改其它配置。引入依赖

<!--openfeign远程调用-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

新建Feign接口

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

/*
name指定远程调用的接口所对应的服务名
path指定远程调用的接口所对应的类的@RequestMapping
 */
@FeignClient(name = "stock-service", path = "/stock")
public interface StockFeignService {
    @RequestMapping("/reduct")
    String reduct();
}

编写订单Controller

import com.somnus.feign.StockFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    StockFeignService stockFeignService;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功!");
        String reduct = stockFeignService.reduct();
        return "Hello Feign" + reduct;
    }
}

此时stockFeignService下面有一条红色的波浪线,需要在启动类中配置上开启Feign的注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

最终项目结构

访问订单接口
在这里插入图片描述

日志配置
1.新建product-nacos子项目,controller

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Value("${server.port}")
    String port;

    @RequestMapping("/{id}")
    public String get(@PathVariable Integer id) {
        System.out.println("查询商品" + id);
        return "查询商品" + id + ":" + port;
    }
}
application.yml

server:
  port: 8007
spring:
  application:
    name: product-nacos
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        namespace: public

在上文中创建的order-openfeign项目中新建配置类

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author somnus
 * @create 2022-06-21
 */
// 此处配置Configuration注解会全局生效 如果想指定对一个的微服务生效 就不能配置
@Configuration
public class FeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

新建feign接口

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

/*
name指定远程调用的接口所对应的服务名
path指定远程调用的接口所对应的类的@RequestMapping
 */
@FeignClient(name = "product-nacos", path = "/product")
public interface ProductFeignService {
    @RequestMapping("/{id}")
    String get(@PathVariable("id") Integer id);
}

更新Controller

import com.somnus.feign.ProductFeignService;
import com.somnus.feign.StockFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    StockFeignService stockFeignService;

    @Autowired
    ProductFeignService productFeignService;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功!");
        String reduct = stockFeignService.reduct();
        String s = productFeignService.get(1);
        return "Hello Feign" + reduct + s;
    }
}

application.yml添加配置

springboot默认的日志级别时info feign默认的是debug 所以日志级别不输出

logging:
  level:
    com.mrZhao.feign: debug

契约配置
  Spring Cloud在Feign的基础上做了扩展,使用Spring MVC的疏解来完成Feign的功能。原生的Feign是不支持Spring MVC注解的,如果你想在Spring Cloud中使用原生的注解方式来定义客户端也是可以的,通过配置契约来改变这个配置,Spring Cloud中默认的是SpringMvcContract。
  Spring Cloud 1早期版本就是用的原生Feign,随着netflix的停更替换成了Open feign。

修改契约配置,支持Feign原生的注解

/**
  *  修改契约配置,支持feign原生的注解
  *  @return
  */
@Bean
public Contract feignContract() {
  return new Cotract.Default();
}
$\color{#FF0000}{注意:修改契约配置后,stock-service不再支持springmvc的注解,需要使用Feign原生的注解}

OrderFeignService中配置使用Feign原生的注解
@FeignClient(name = "stock-service", path = "/stock", configuration = FeignConfig.class)
public interface StockFeignService {
    @RequestLine("GET /reduct")
    String reduct();
}

补充:也可以通过yml方式配置契约

feign日志局部配置

feign:
  client:
    config:
      stock-service:
        loggerLevel: BASIC
        # 设置未默认的契约(还原成原生注解)
        contract: feign.Contract.Default

超时时间设置
通过 Options 可以配置连接超时时间和读取超时时间,Options 得第一个参数是连接的超时时间(ms),默认值是2s;第二个是请求处理的超时时间(ms),默认是5s。
全局配置

import feign.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 此处配置Configuration注解会全局生效 如果想指定对一个的微服务生效 就不能加
@Configuration
public class FeignConfig {
    @Bean
    public Request.Options options() {
        return new Request.Options(5000, 10000);
    }
}

yml中配置

feign日志局部配置

feign:
  client:
    config:
      stock-service:
        loggerLevel: BASIC
        # 连接超时时间,默认是2s
        connectTimeout: 5000
        # 请求处理超时时间,默认是5s
        readTimeout: 3000
Spring CloudSpring Cloud Alibaba都是基于Spring Framework的开源框架,用于构建分布式系统和微服务架构。它们都提供了一系列的组件和工具来简化微服务开发和管理。 Spring Cloud是一个由Pivotal团队维护的项目,它集成了Netflix开源的一些组件,如Eureka作为注册中心、Ribbon作为负载均衡器、Feign作为服务调用工具等。同时,Spring Cloud还提供了其他功能,如Config Server用于动态管理配置、Gateway用于构建API网关等。 而Spring Cloud Alibaba则是阿里巴巴开源的项目,它在Spring Cloud的基础上进行了扩展和定制,增加了一些阿里巴巴自己的组件和工具。比如,它使用Nacos作为注册中心和配置中心,使用Sentinel作为熔断降级工具。 总的来说,Spring CloudSpring Cloud Alibaba都是用于构建微服务架构的框架,它们的区别在于Spring Cloud集成了Netflix组件,而Spring Cloud Alibaba集成了阿里巴巴的一些组件。根据具体的需求和技术栈选择使用哪个框架可以更好地满足开发和管理微服务的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring Cloud AlibabaSpring Cloud的区别](https://blog.csdn.net/weixin_43888891/article/details/126653270)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值