spring cloud alibaba远程调用OpenFeign

1、目录结构
新建两个子模块
spring cloud alibaba远程调用OpenFeign_第1张图片
spring cloud alibaba远程调用OpenFeign_第2张图片

2、安装依赖


        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

注:在2021和boot 2.6.13版本中需要加入依赖如下


            org.springframework.cloud
            spring-cloud-starter-bootstrap
        

其中对于新版的消费端,需要加入如下依赖


            org.springframework.cloud
            spring-cloud-loadbalancer
        

3、bootstrap.yml新增配置项

provider:
  name: openfeign-provider-sample
#增加第sentinel的支持
feign:
  sentinel:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 5000 # 建立连接所用的时间 单位:毫秒
        readTimeout: 5000  #建立连接后从服务器读取到资源所用的时间 单位:毫秒
#日志配置
logging:
  level:
    com.example.openfeign.service: debug  # 打印com.springcloudalibaba.openfeign.service包的日志 debug级别

4、启动类添加注解@EnableFeignClients
5、调用放新建service接口

import com.example.openfeign.fallback.OpenFeignTestServiceFallback;
import com.springcloudalibaba.common.Params;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

// 表示调用服务名为 openfeign-provider-sample fallback 异常、限流等的自定义处理逻辑在OpenFeignTestServiceFallback类中
@FeignClient(value = "${provider.name}",fallback = OpenFeignTestServiceFallback.class)
public interface OpenFeignTestService {
    @GetMapping("/openFeignProviderTest")
    String openFeignProviderTest();

    @GetMapping("/sampleParamProviderTest")
    String sampleParamProviderTest(@RequestParam("name") String name, @RequestParam("id") Integer id);



    @PostMapping("/springQueryMapProviderTest")
    String springQueryMapProviderTest(@SpringQueryMap Params params);

    @PostMapping("/springBodyProviderTest")
    String springBodyProviderTest(@RequestBody Params params);

    @GetMapping("/pathParamProviderTest/{name}/{id}")
    String pathParamProviderTest(@PathVariable("name") String name, @PathVariable("id") Integer id);
}

其中@FeignClient的value是被调用方的服务名称,fallback 为限流或异常执行的函数
6、新建OpenFeignTestServiceFallback异常或限流类

import com.example.openfeign.service.OpenFeignTestService;
import com.springcloudalibaba.common.Params;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@Component
public class OpenFeignTestServiceFallback implements OpenFeignTestService {

    @Override
    public String openFeignProviderTest(){
        return "兜底方法";
    }

    @Override
    public String sampleParamProviderTest(@RequestParam("name") String name, @RequestParam("id") Integer id){
        return "简单传参失败";
    }

    @Override
    public String springQueryMapProviderTest(Params params){
        return "兜底错误数据";
    }

    @Override
    public String springBodyProviderTest(@RequestBody Params params){
        return "body兜底";
    }

    @Override
    public String pathParamProviderTest(String name,Integer id){
        return "路径方式path兜底";
    }
}

7、新建OpenFeignLoggerConfiguration类配置日志

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

@Configuration
public class OpenFeignLoggerConfiguration {
    @Bean
    public Logger.Level openFeignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

8、新建控制器测试类

import com.example.openfeign.service.OpenFeignTestService;
import com.springcloudalibaba.common.Params;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class OpenFeignTestController {
    @Resource
    private OpenFeignTestService openFeignTestService;

    @GetMapping("/openFeignTest")
    public String openFeignTest(){
        return openFeignTestService.openFeignProviderTest();
    }


    @GetMapping("/sampleParamsQueryTest")
    public String sampleParamsQueryTest(){
        return openFeignTestService.sampleParamProviderTest("张明",1234);
    }

    @GetMapping("/pathParamsQueryTest")
    public String pathParamsQueryTest(){
        return openFeignTestService.pathParamProviderTest("张明path",6789);
    }

    @GetMapping("/springQueryMapTest")
    public String springQueryMapTest(){
        Params params = new Params();
        params.setId(12);
        params.setName("测试数据");
        return openFeignTestService.springQueryMapProviderTest(params);
    }


    @GetMapping("/springBodyTest")
    public String springBodyTest(){
        Params params = new Params();
        params.setId(78);
        params.setName("小明明");
        return openFeignTestService.springBodyProviderTest(params);
    }

}

你可能感兴趣的:(spring,java,spring,cloud)