SpringCloud组件openfeign的使用

SpringCloud组件openfeign的使用

注意:本次实现必须使用到nacos的注册中心

gitee:springcloud组件openfeign使用
个人建议:配置的方式学习openfeign组件,这样更加容易看懂

目录结构

在这里插入图片描述

maven依赖

父工程(springcloud-openfeign)依赖

<parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.13.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.13.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR6</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.4.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

子模块【openfeign客户端】

(client-openfeign-annotation【注解方式】,client-openfeign-config【配置方式】)依赖

    <dependencies>
        <!-- web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- nacos注册中心依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

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

子模块【openfeign服务端】

(product-openfeign,stock-openfeign)

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

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
</dependencies>

openfeign服务端

product-openfeign服务

目录结构

在这里插入图片描述

maven依赖

子模块【openfeign服务端】

配置文件(application.yaml)
server:
  port: 8230  #模拟时需要进行通过idea配置之后开启多个服务

spring:
  application:
    name: product-openfeign
  cloud:
    nacos:
      discovery:
        namespace: 2ce93c25-dd8f-4972-8350-73833228e108
      username: nacos
      password: nacos
      server-addr: 192.168.153.199:8848
controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 黔程似景
 * @description
 * @date 2022/9/29 6:28
 * @blame 黔程似景
 **/
@RestController
public class ProductController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/product")
    public String product() throws InterruptedException {
        return "服务: " + port;
    }

    /**
     * 请求feign超时
     */
    @GetMapping("/timeout")
    public String timeout() throws InterruptedException {
        Thread.sleep(10000);
        return "服务: " + port;
    }

}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 黔程似景
 * @description 启动类
 * @date 2022/9/29 6:27
 * @blame 黔程似景
 **/
@SpringBootApplication
public class ProductOpenFeignApplication {

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

stock-openfeign服务

目录结构

在这里插入图片描述

maven依赖

子模块【openfeign服务端】

配置文件(application.yaml)
server:
  port: 8220

spring:
  application:
    name: stock-openfeign
  cloud:
    nacos:
      discovery:
        namespace: 2ce93c25-dd8f-4972-8350-73833228e108
      username: nacos
      password: nacos
      server-addr: 192.168.153.199:8848
controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 黔程似景
 * @description stock服务提供接口
 * @date 2022/9/27 6:57
 * @blame 黔程似景
 **/
@RestController
@RequestMapping("/stock")
public class StockController {

    @Value("${server.port}")
    private String port;

    /**
     * 库存修改
     */
    @GetMapping("/goods")
    public String goods() {
        System.out.println("商品库存修改");
        return "服务:" + port ;
    }

    @GetMapping("/{id}")
    public String findOrderByUserId(@PathVariable("id") String id) {
        System.out.println("根据用户Id查询商品");
        return "服务:" + port + "  id为:" + id;
    }

}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 黔程似景
 * @description feign调用提供端
 * @date 2022/9/27 6:56
 * @blame 黔程似景
 **/
@SpringBootApplication
public class StockOpenFeignApplication {

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

openfeign客户端

client-openfeign-annotation【注解注入方式】

目录结构

在这里插入图片描述

maven依赖

子模块【openfeign客户端】

配置文件(application.yaml)
server:
  port: 8210

spring:
  application:
    name: client-openfeign-annotation
  cloud:
    nacos:
      discovery:
        namespace: 2ce93c25-dd8f-4972-8350-73833228e108
      username: nacos
      password: nacos
      server-addr: 192.168.153.199:8848

# ribbon 默认为懒加载模式,即用的时候才会去加载,需要设置启动加载配置如下
ribbon:
  eager-load:
    # 开启ribbon饥饿加载
    enabled: true
    # 配置mall‐user使用ribbon饥饿加载,多个使用逗号分隔
    clients: stock-openfeign,product-openfeign

# 使用配置的形式进行设置负载均衡策略
stock-openfeign:
  ribbon:
    # 轮询策略
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
product-openfeign:
  ribbon:
    # 轮询策略
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

# 服务日志配置(因为默认spring日志是info,会阻挡feign日志输出)
logging:
  level:
    com.qq.openfeign.client: debug
拦截器(interceptor)
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 黔程似景
 * @description
 * @date 2022/9/29 6:58
 * @blame 黔程似景
 **/
public class CustomFeignInterceptor implements RequestInterceptor {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void apply(RequestTemplate requestTemplate) {
        logger.info("---------------feign拦截器使用------------");

        String url = requestTemplate.url();
        if("/1".equals(url)){
            logger.info("url地址:" + url);
            requestTemplate.uri("/9");
        }

    }
}
配置类(config)
import feign.Contract;
import feign.Logger;
import feign.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 黔程似景
 * @description OpenFeign相关配置
 * @date 2022/9/28 6:52
 * @blame 黔程似景
 **/
@Configuration
public class OpenFeignConfig {

    /**
     * 配置打印feign调用接口的日志
     */
    @Bean
    public Logger.Level feignLoggerLevel(){
        //full配置比较特殊,需要将spring默认的日志级别info改为debug,否则看不到效果
        return Logger.Level.FULL;
    }

    /**
     * 修改契约配置,支持Feign原生的注解,老版本是不支持springmvc注解的,此处仅作演示,项目中用到的是fegin的注解时,需要切换
     */
    @Bean
    public Contract feignContract(){
        return new Contract.Default();
    }

    /**
     * 设置请求feign超时时间
     */
    @Bean
    public Request.Options options() {
        return new Request.Options(5000, 1000);
    }

    /**
     * 进行注入拦截器使用
     */
    @Bean
    public CustomFeignInterceptor feignAuthRequestInterceptor(){
        return new CustomFeignInterceptor();
    }

}
openfeign调用类(service)

ProductFeignService(product服务)

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

/**
 * @author 黔程似景
 * @description feign调用product服务
 * @date 2022/9/28 22:49
 * @blame 黔程似景
 *
 * @FeignClient(name = "product-openfeign" ,path = "/xx" )
 *      1.  name : 服务名
 *      2.  path : 服务下某个controller类上面的注解@RequestMapping("/xx")的value参数
 *
 *
 **/
@FeignClient(name = "product-openfeign" )
public interface ProductFeignService {

    @GetMapping("/product")
    @RequestLine("GET /product")    //早期feign不支持springMvc注解
    String product();

    @GetMapping("/timeout")
    @RequestLine("GET /timeout")    //早期feign不支持springMvc注解
    String timeout();

}

StockFeignService(stock服务)

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

/**
 * @author 黔程似景
 * @description feign调用stock服务
 * @date 2022/9/28 22:49
 * @blame 黔程似景
 *
 * @FeignClient(name = "product-openfeign" ,path = "/stock" )
 *      1.  name : 服务名
 *      2.  path : 服务下某个controller类上面的注解@RequestMapping("/stock")的value参数
 *
 **/
@FeignClient(name = "stock-openfeign" , path = "/stock")
public interface StockFeignService {

    #@GetMapping("/goods")
    @RequestLine("GET /goods")
    String goods();

    #@GetMapping("/{id}")
    @RequestLine("GET /{id}")   # feign早期版本不支持springmvc注解,@Param("id")=@PathVariable("id")
    String findOrderByUserId(@Param("id") String id);

}
controller
import com.qq.openfeign.client.service.ProductFeignService;
import com.qq.openfeign.client.service.StockFeignService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 黔程似景
 * @description openfeign调用客户端接口
 * @date 2022/9/27 7:08
 * @blame 黔程似景
 **/
@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private StockFeignService stockFeignService;
    @Autowired
    private ProductFeignService productFeignService;

    @GetMapping("/demo")
    public String demo() {
        System.out.println("----------------------进行负载均衡调用-----------------");
        return "stock" + stockFeignService.goods() + "\n product" + productFeignService.product();

    }

    /**
     * 拦截器使用
     */
    @GetMapping("/interceptor/{id}")
    public String interceptor(@PathVariable("id") String id){
        System.out.println("----------------------进行负载均衡调用-----------------");
        return stockFeignService.findOrderByUserId(id);
    }

    /**
     * 请求feign超时
     */
    @GetMapping("/timeout")
    public String timeout(){
        return productFeignService.timeout();
    }
}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author 黔程似景
 * @description 启动类
 * @date 2022/9/28 22:46
 * @blame 黔程似景
 **/
@SpringBootApplication
@EnableFeignClients
public class ClientOpenFeignApplication {

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

}
启动测试

注意:启动2个stock-openfeign服务,2个product-openfeign服务,外加当前类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

client-openfeign-config【配置方式实现】

目录结构

在这里插入图片描述

maven依赖

子模块【openfeign客户端】

配置文件(application.yaml)
server:
  port: 8211

spring:
  application:
    name: client-openfeign-config
  cloud:
    nacos:
      discovery:
        namespace: 2ce93c25-dd8f-4972-8350-73833228e108
      username: nacos
      password: nacos
      server-addr: 192.168.153.199:8848

# ribbon 默认为懒加载模式,即用的时候才会去加载,需要设置启动加载配置如下
ribbon:
  eager-load:
    # 开启ribbon饥饿加载
    enabled: true
    # 配置mall‐user使用ribbon饥饿加载,多个使用逗号分隔
    clients: stock-openfeign,product-openfeign
# 使用配置的形式进行设定负载均衡策略
stock-openfeign:
  ribbon:
    # 轮询
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule

# 使用配置的形式进行设定负载均衡策略
product-openfeign:
  ribbon:
    # 轮询
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

logging:
  level:
    com.qq.openfeign.client: debug

# feign日志级别配置
feign:
  client:
    config:
      stock-openfeign:  # 负载均衡的服务名
        loggerLevel: FULL
        contract: feign.Contract.Default  # 配置方式使用原生注解
        connectTimeout: 5000              # 配置方式设置feign请求连接时间
        readTimeout: 3000                 # 配置方式设置feign请求读取时间  # 请求处理超时时间,默认5s
        requestInterceptors[0]: com.qq.openfeign.client.interceptor.CustomFeignInterceptor
      product-openfeign:
        loggerLevel: FULL
        #contract: feign.Contract.Default # 配置方式使用原生注解
        connectTimeout: 5000              # 配置方式设置feign请求连接时间
        readTimeout: 3000                 # 配置方式设置feign请求读取时间
拦截器(interceptor)
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 黔程似景
 * @description
 * @date 2022/9/29 6:58
 * @blame 黔程似景
 **/
public class CustomFeignInterceptor implements RequestInterceptor {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void apply(RequestTemplate requestTemplate) {
        logger.info("---------------feign拦截器使用------------");

        String url = requestTemplate.url();
        if("/1".equals(url)){
            logger.info("url地址:" + url);
            requestTemplate.uri("/9");
        }

    }
}
openfeign调用类(service)

ProductFeignService(product服务)

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

/**
 * @author 黔程似景
 * @description feign调用product服务
 * @date 2022/9/28 22:49
 * @blame 黔程似景
 *
 * @FeignClient(name = "product-openfeign" ,path = "/xx" )
 *      1.  name : 服务名
 *      2.  path : 服务下某个controller类上面的注解@RequestMapping("/xx")的value参数
 *
 *
 **/
@FeignClient(name = "product-openfeign" )
public interface ProductFeignService {

    @GetMapping("/product")
    String product();

    @GetMapping("/timeout")
    String timeout();

}

StockFeignService(stock服务)

import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;

/**
 * @author 黔程似景
 * @description feign调用stock服务
 * @date 2022/9/28 22:49
 * @blame 黔程似景
 *
 * @FeignClient(name = "product-openfeign" ,path = "/stock" )
 *      1.  name : 服务名
 *      2.  path : 服务下某个controller类上面的注解@RequestMapping("/stock")的value参数
 **/
@FeignClient(name = "stock-openfeign" , path = "/stock")
public interface StockFeignService {

    @RequestLine("GET /goods")
    String goods();

    @RequestLine("GET /{id}")
    String findOrderByUserId(@Param("id") String id);

}
controller
import com.qq.openfeign.client.service.ProductFeignService;
import com.qq.openfeign.client.service.StockFeignService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 黔程似景
 * @description openfeign调用客户端
 * @date 2022/9/27 7:08
 * @blame 黔程似景
 **/
@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private StockFeignService stockFeignService;
    @Autowired
    private ProductFeignService productFeignService;

    @GetMapping("/demo")
    public String demo() {
        System.out.println("进行负载均衡调用-----------------");

        return "stock" + stockFeignService.goods() + "\n product" + productFeignService.product();
    }

    /**
     * 拦截器使用
     */
    @GetMapping("/interceptor/{id}")
    public String interceptor(@PathVariable("id") String id){
        System.out.println("----------------------进行负载均衡调用-----------------");
        return stockFeignService.findOrderByUserId(id);
    }

    /**
     * 请求feign超时
     */
    @GetMapping("/timeout")
    public String timeout(){
        return productFeignService.timeout();
    }
}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author 黔程似景
 * @description 启动类
 * @date 2022/9/28 22:46
 * @blame 黔程似景
 **/
@SpringBootApplication
@EnableFeignClients
public class ClientOpenFeignApplication {

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

}
启动测试

注意:启动2个stock-openfeign服务,2个product-openfeign服务,外加当前类
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【2021年,将Spring全家桶系列课程进行Review,修复顺序等错误。进入2022年,将Spring的课程进行整理,整理为案例精讲的系列课程,并新增高级的Spring Security等内容,通过手把手一步步教你从零开始学会应用Spring,课件将逐步进行上传,敬请期待】 本课程是Spring案例精讲课程的第四部分Spring Cloud,Spring案例精讲课程以真实场景、项目实战为导向,循序渐进,深入浅出的讲解Java网络编程,助力您在技术工作中更进一步。 本课程聚焦Spring Cloud的核心知识点:注册中心、服务提供者与消费者、服务的调用OpenFeign、Hystrix监控、服务网关gateway、消息驱动的微服务Spring Cloud Stream、分布式集群、分布式配置中心的案例介绍, 快速掌握Spring Cloud的核心知识,快速上手,为学习及工作做好充足的准备。 由于本课程聚焦于案例,即直接上手操作,对于Spring的原理等不会做过多介绍,希望了解原理等内容的需要通过其他视频或者书籍去了解,建议按照该案例课程一步步做下来,之后再去进一步回顾原理,这样能够促进大家对原理有更好的理解。【通过Spring全家桶,我们保证你能收获到以下几点】 1、掌握Spring全家桶主要部分的开发、实现2、可以使用Spring MVC、Spring Boot、Spring Cloud及Spring Data进行大部分的Spring开发3、初步了解使用微服务、了解使用Spring进行微服务的设计实现4、奠定扎实的Spring技术,具备了一定的独立开发的能力  【实力讲师】 毕业于清华大学软件学院软件工程专业,曾在Accenture、IBM等知名外企任管理及架构职位,近15年的JavaEE经验,近8年的Spring经验,一直致力于架构、设计、开发及管理工作,在电商、零售、制造业等有丰富的项目实施经验  【本课程适用人群】如果你是一定不要错过!  适合于有JavaEE基础的,如:JSP、JSTL、Java基础等的学习者没有基础的学习者跟着课程可以学习,但是需要补充相关基础知识后,才能很好的参与到相关的工作中。 【Spring全家桶课程共包含如下几门】 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值