SpringCloud使用Feign实现远程调用

Nacos实现配置管理的:SpringCloud使用Nacos实现配置管理_生骨大头菜的博客-CSDN博客

之前使用 RestTemplate进行远程调用,但是RestTemplate存在一下问题:

  • 代码可读性差,编程体验不统一
  • 参数复杂URL难以维护

1、在order-service中引入依赖

        <!--feign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2、在order-service的启动类中添加注解开启Feign功能

package com.demo;

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


@EnableFeignClients
@SpringBootApplication
public class OrderApplication {

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

}

3、编写Feign客户端

package com.demo.clients;

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


@FeignClient("userservice")
public interface UserClient {

    @GetMapping("/user/{id}")
    String findById(@PathVariable String id);

}

4、controller中使用Feign客户端

package com.demo.controller;

import com.demo.clients.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @Author: 冯健
 * @Date: 2022/3/30
 * @Description:
 * @Modified By:
 */
@RestController
public class OrderController {

    @Autowired
    UserClient userClient;

    @GetMapping("/order/{id}")
    public String order(@PathVariable String id) {
        // 这里访问user-service的微服务
        String user = userClient.findById(id);

        return "订单:" + id + "=====" + user;
    }

}

5、重启服务并且测试

6、性能优化

Feign低层客户端默认使用URLConnection,不支持连接池,可以改为Apache HttpClient或者OKHttp,添加HttpClient的支持,引入依赖:

        <!--feign的HttpClient支持-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
        </dependency>

编辑application.yml,配置连接池与日志,日志级别最好用basic或none

feign:
  client:
    config:
      default:  # default全局的配置
        loggerLevel: BASIC  # 日志级别,BASIC就是最基本的请求和响应信息
  httpclient:
    enabled: true # 开启feign对HttpClient的支持
    max-connections: 200  # 最大的连接数
    max-connections-per-route: 50 # 每个路径的最大连接数
logging:
  level:
    com.demo: debug

重启order-service并测试:

7、Feign的最佳实践,创建一个Feign功能的Module

1.新建一个Module

 

 

 

 2.添加feign依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--feign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!--feign的HttpClient支持-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
        </dependency>
    </dependencies>

3.添加相关client接口

package com.demo.feign.clients;

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


@FeignClient("userservice")
public interface UserClient {

    @GetMapping("/user/{id}")
    String findById(@PathVariable String id);

}

4.无需创建application.yml文件,feign日志等相关配置需要写在order-service,否则不生效

5.在order-service引入feign-api

        <!--引入feign的统一api-->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>feign-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

6.删除order-service中关于feign的引用,并且引用feign-api的相关类

package com.demo.controller;

import com.demo.feign.clients.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class OrderController {

    @Autowired
    UserClient userClient;

    @GetMapping("/order/{id}")
    public String order(@PathVariable String id) {
        // 这里访问user-service的微服务
        String user = userClient.findById(id);

        return "订单:" + id + "=====" + user;
    }

}

 7.重启,报错了

 原因:当定义的FeignClient不在SpringBootApplication的扫描包范围时,这些FeignClient无法使用。有两种方式解决:

方式一:指定FeignClient所在包

@EnableFeignClients(basePackages = "com.demo.feign.clients")

方式二:指定FeignClient字节码

@EnableFeignClients(clients = {UserClient.class})

使用第二种:

package com.demo;

import com.demo.feign.clients.UserClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;


@EnableFeignClients(clients = UserClient.class)
@SpringBootApplication
public class OrderApplication {

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

}

重新启动order-service,并且访问

 

 

注意事项:feign的相关配置需要写在order-service中,而不是feign-api中

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud使用Feign是一种声明式的Web服务客户端,它简化了使用HTTP客户端进行远程服务调用的过程。Feign内部集成了Ribbon负载均衡器和Hystrix断路器,可以实现服务的负载均衡和容错处理。 要使用Feign,首先需要在项目中添加相应的依赖。在Maven项目中,可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 然后,在启动类上加上@EnableFeignClients注解来启用Feign客户端: ```java @SpringBootApplication @EnableFeignClients public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 接下来,定义一个Feign客户端接口,使用@FeignClient注解指定要调用的服务名,并定义接口中的方法与远程服务的API接口相对应。例如: ```java @FeignClient(name = "your-service") public interface YourServiceClient { @GetMapping("/api/something") String getSomething(); } ``` 在需要调用远程服务的地方,可以通过注入此Feign客户端接口来进行调用: ```java @RestController public class YourController { private final YourServiceClient yourServiceClient; public YourController(YourServiceClient yourServiceClient) { this.yourServiceClient = yourServiceClient; } @GetMapping("/something") public String getSomething() { return yourServiceClient.getSomething(); } } ``` 以上是使用Feign进行远程服务调用的基本步骤,通过Feign可以方便地实现服务之间的通信。同时,Feign还支持对请求参数、请求头、请求体等进行配置和传递,以满足各种复杂的场景需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值