欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenFeign服务接口调用

程序员文章站 2022-06-13 11:46:37
...

  概述


    是什么

OpenFeign服务接口调用
      Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上申明注解
    OpenFeign服务接口调用
    能干嘛

OpenFeign服务接口调用
    Fegin+OpenFeign两者区别

OpenFeign服务接口调用


  OpenFeign使用步骤


    接口+注解
      微服务调用接口aaa@qq.com
    新建wms-sentinel-service8401
      Feign在消费端使用
    POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>wms</artifactId>
        <groupId>com.hk.wms</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>wms-sentinel-service8401</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <!-- SpringCloud ailibaba nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到-->
    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
    </dependency>
    <!-- SpringCloud ailibaba sentinel-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--监控-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
</project>

 

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


    YML

server:
  port: 8401

spring:
  application:
    name: wms-sentinal-service
  cloud:
    nacos:
      discovery:
        #Nacos服务注册中心地址
        server-addr: 192.168.1.131:8848
    sentinel:
      transport:
        #配置Sentin dashboard地址
        dashboard: localhost:8080      #8080端口将会监控8401
        # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口
        port: 8719
      datasource:
        ds1:
          nacos:
            server-addr: 192.168.1.131:8848
            dataId: wms-sentinal-service
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow
service-url:
  nacos-user-service: http://wms-provider
config-url:
  config-service: http://wms-config-3377

management:
  endpoints:
    web:
      exposure:
        include: '*'
#**sentinel对feign的支持
feign:
  sentinel:
    enabled: true


    主启动
      @EnableFeignClients

package com.hk.wms.wmssentinelservice8401;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class WmsSentinelService8401Application {

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

}


    业务类
      业务逻辑接口 + @FeignClient配置调用provider服务
      新建FeignService 接口并新增注解@FeignClient
        @FeignClient value的值是寻找的微服务在注册中心中的名字

package com.hk.wms.wmssentinelservice8401.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(value="wms-provider",fallback = FeignFallbackService.class)
public interface FeignService {
    @GetMapping(value="/getStr/{id}")
    public String getString(@PathVariable("id") int id);
}

注意:@FeignClient有纳入容器管理的功能,不需要加@Component
      控制层Controller

package com.hk.wms.wmssentinelservice8401.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hk.wms.wmssentinelservice8401.myhandler.CustomerBlockHandler;
import com.hk.wms.wmssentinelservice8401.service.FeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@RestController
@Slf4j
public class FlowLimitController {
    @Resource
    FeignService feignService;

    @GetMapping("/testE/{id}")
    @SentinelResource(value="testE",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "customBlockHandler2",fallback = "handlerFallBack")
    public String getStrController(@PathVariable("id")int id){
        String str=feignService.getString(id);
        if(id<0){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常...");
        }
        if(str==null){
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }
        return str;
    }


}


      Feign自带负载均衡配置项
    小总结

OpenFeign服务接口调用


  OpenFeign超时控制


    超时设置,故意设置超时演示出错情况
      服务提供故意写暂停程序

 @GetMapping("/getStr2/{id}")
    public String getStr2(@PathVariable("id")int id){
        try {
            //暂停3秒线程
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return id+":"+serverPort;

    }


      服务消费方添加超时方法FeignService

package com.hk.wms.wmssentinelservice8401.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(value="wms-provider",fallback = FeignFallbackService.class)
public interface FeignService {
    @GetMapping(value="/getStr/{id}")
    public String getString(@PathVariable("id") int id);


    @GetMapping(value="/getStr2/{id}")
    public String getString2(@PathVariable("id") int id);
}


      服务消费方80添加超时方法OrderFeignController

package com.hk.wms.wmssentinelservice8401.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hk.wms.wmssentinelservice8401.myhandler.CustomerBlockHandler;
import com.hk.wms.wmssentinelservice8401.service.FeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@RestController
@Slf4j
public class FlowLimitController {
    @Resource
    FeignService feignService;

    @GetMapping("/testE/{id}")
    @SentinelResource(value="testE",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "customBlockHandler2",fallback = "handlerFallBack")
    public String getStrController(@PathVariable("id")int id){
        String str=feignService.getString(id);
        if(id<0){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常...");
        }
        if(str==null){
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }
        return str;
    }
    @GetMapping("/testE2/{id}")
    @SentinelResource(value="testE",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "customBlockHandler2",fallback = "handlerFallBack")
    public String getStrController2(@PathVariable("id")int id){
        String str=feignService.getString2(id);
        if(id<0){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常...");
        }
        if(str==null){
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }
        return str;
    }

}

当服务出错时fallback可以降级服务,自己写返回错误信息
    
    OpenFeign默认等待1秒钟,超过后报错


    是什么
      OpenFeign默认支持Ribbon
    YML文件里需要开启OpenFeign客户端超时控制

server:
  port: 80

#这里只把feign做客户端用,不注册进eureka
eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: false
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
  #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的实际
  ReadTimeout: 5000
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

 


  OpenFeign日志打印功能


    是什么

Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节说白了就是对Feign接口调用情况进行监控和输出

    日志级别

OpenFeign服务接口调用
YML文件里需要开启日志的Feign客户端

server:
  port: 8401

spring:
  application:
    name: wms-sentinal-service
  cloud:
    nacos:
      discovery:
        #Nacos服务注册中心地址
        server-addr: 192.168.1.131:8848
    sentinel:
      transport:
        #配置Sentin dashboard地址
        dashboard: localhost:8080      #8080端口将会监控8401
        # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口
        port: 8719
      datasource:
        ds1:
          nacos:
            server-addr: 192.168.1.131:8848
            dataId: wms-sentinal-service
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow
service-url:
  nacos-user-service: http://wms-provider
config-url:
  config-service: http://wms-config-3377

management:
  endpoints:
    web:
      exposure:
        include: '*'
#**sentinel对feign的支持
feign:
  sentinel:
    enabled: true
ribbon:
  #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的实际
  ReadTimeout: 5000
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000
logging:
  level:
    #feign日志以什么级别监控那个接口
    com.hk.wms.wmssentinelservice8401.service: debug

   

logging:
  level:
    #feign日志以什么级别监控那个接口
    com.hk.wms.wmssentinelservice8401.service: debug

配置日志bean

package com.hk.wms.wmssentinelservice8401.config;

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



@Configuration
public class FeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

 


    后台日志查看

2020-05-08 18:07:25.486 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] <--- HTTP/1.1 200 (612ms)
2020-05-08 18:07:25.486 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] connection: keep-alive
2020-05-08 18:07:25.486 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] content-length: 6
2020-05-08 18:07:25.486 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] content-type: text/plain;charset=UTF-8
2020-05-08 18:07:25.486 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] date: Fri, 08 May 2020 10:07:25 GMT
2020-05-08 18:07:25.487 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] keep-alive: timeout=60
2020-05-08 18:07:25.487 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] 
2020-05-08 18:07:25.487 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] 2:9002
2020-05-08 18:07:25.487 DEBUG 6184 --- [nio-8401-exec-1] c.h.w.w.service.FeignService             : [FeignService#getString2] <--- END HTTP (6-byte body)
2020-05-08 18:07:26.380  INFO 6184 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: wms-provider.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2020-05-08 18:07:27.750 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] ---> GET http://wms-provider/getStr2/2 HTTP/1.1
2020-05-08 18:07:27.750 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] ---> END HTTP (0-byte body)
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] <--- HTTP/1.1 200 (3008ms)
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] connection: keep-alive
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] content-length: 6
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] content-type: text/plain;charset=UTF-8
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] date: Fri, 08 May 2020 10:07:30 GMT
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] keep-alive: timeout=60
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] 
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] 2:9003
2020-05-08 18:07:30.760 DEBUG 6184 --- [nio-8401-exec-2] c.h.w.w.service.FeignService             : [FeignService#getString2] <--- END HTTP (6-byte body)

 

相关标签: springcloud