springcloud 使用feign

本文介绍了如何使用Spring Cloud Feign调用web-test服务的API接口,包括`web-test`的接口定义和`web-hello`中的Feign客户端配置。重点展示了FeignClient的使用和如何在测试中调用远程接口。
摘要由CSDN通过智能技术生成

一,被调用方 web-test

spring:
  application:
    name: web-test

二,web-test准备接口

package com.tenyears.webTest.controller;

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

@RestController
@RequestMapping("api/test")
public class TestController {


    @RequestMapping(value = "test1", method = RequestMethod.POST)
    public String test1() {
        return "hello";
    }

}

三,调用方web-hello

POM

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

四,Feign

@FeignClient(value="服务名")

package com.tenyears.webAD.feign;

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

/**
 * @description :
 * @auther Tyler
 * @date 2021/6/15
 */

@FeignClient(value = "web-test")
public interface WebTestFeign {
    @RequestMapping(value = "/api/test/test1", method = RequestMethod.POST)
    String test1();
}

Main方法添加 @EnableFeignClients

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

    }
}

五,测试接口

package com.tenyears.webAD.controller;

import com.tenyears.webAD.feign.WebTestFeign;
import com.tenyears.webAD.service.service.AdScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Tyler
 * @date 2019/4/16
 */

@RestController
@RequestMapping("api/test")
public class TestController {
    Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    WebTestFeign webTestFeign;


    @RequestMapping(value = "test1", method = RequestMethod.POST)
    public String test1() {
        String str=webTestFeign.test1();
        return str;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值