Feign调用

  1. 添加feign的jar包
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 编写Feign的接口服务,用于微服务的提供者和消费者
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("XXX")
public interface IFeignService {
    @GetMapping("feign_get")
    String get(@RequestParam("name") String name);

    @PostMapping("feign_post")
    User post(@RequestBody User user);
}
  1. 在微服务的提供者一方,实现这个接口服务
import com.qf.feign.IFeignService;
import com.qf.feign.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController implements IFeignService {
    @Value("${server.port}")
    private String port;

    @Override
    public String get(String name) {
        return String.format("你好, %s, 调用的是GET方法, 端口为:%s", name, port);
    }

    @Override
    public User post(User user) {
        user.setName(user.getName().concat("   POST方法  ").concat(port));
        return user;
    }
}

  1. 在微服务的消费者一方,开启Feign;并实现这个接口服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;

@Service
@FeignClient(name = "EUREKA-CLIENT-PROVIDER")
public interface FeignService extends IFeignService {
}

接下来就直接注入这个FeignService并调用即可:

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Autowired
    private final FeignUserService feignUserService;

    @GetMapping("get")
    public String feignGet(@RequestParam(value = "name") String name) {
        return feignUserService.feignGet(name);
    }

    @GetMapping("post")
    public String feignPost(@RequestParam(value = "name") String name) {
        return feignUserService.feignPost(name);
    }

    @GetMapping("getEntity")
    public User feignGetEntity(User user) {
        return feignUserService.feignGetEntity(user);
    }

    @GetMapping("postEntity")
    public User feignPostEntity(User user) {
        return feignUserService.feignPostEntity(user);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值