首先,根据要调用的服务及接口:

import com.sample.pass.distrition.model.ResultBody;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "cd-server")
public interface CdClient {
  @GetMapping("/crmdict/province")
  ResultBody province();

  @GetMapping("/crmdict/city")
  ResultBody city(@RequestParam("provinceId") Integer provinceId);

  @GetMapping("/crmdict/area")
  ResultBody area(@RequestParam("cityId") Integer cityId);

}

然后,编写自己的需要暴露的接口:

@RestController
@RequestMapping(value="/currency")
public class CurrencyController {
   
   @Autowired
   private CdClient cdClient;

   @RequestMapping(value="/findAll",method = RequestMethod.GET)
   public ResultBody findAll() {
      ResultBody obj = cdClient.findAll();
      List<Currency> list = (List<Currency>)obj.getData();
      if (list.size()<=0) {
         return new ResultBody(ResCodeEnum.NULL_DATA.getCode(),"数据为空!");
      }
      return new ResultBody(ResCodeEnum.SUCCESS.getCode(),"success",list);
   }
   
}

通过这两步,我们已经能实现服务间的接口调用,数据传输等操作了。

补充:

若暴露接口的服务没有将model共享,那么我们还需要在自己的项目中建一个一摸一样的model,这样才能调到数据。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐