Feign应用
one piece 38 0

Maven依赖

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

API服务 接口类:value值为对应微服务服务名

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

@FeignClient(value = "a-server", path = TestApi.mapping, contextId = TestApi.mapping)
public interface TestApi {

   static String mapping="api/test";


   @RequestMapping(value="/getName",method = RequestMethod.POST)
   public String getName();
}

Server服务 接口调用实现,接口请求路径需与api中接口路劲一致

package com.a.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @PostMapping("/getName")
    public String getName(){
        return "one piece";
    }

}
评论区