springcloud应用通过openfeign实现应用间通信

springcloud应用如果注册在同一个注册中心,比如nacos,那个应用间相互调用可以使用openfeign来相互通信。

  • 首先引入openfeign包
build.gradle:

implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.0.3'

新建调用接口


TbkFeignClient.class

package com.dsiab.manageService.schedule;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

//@FeignClient(name = "promote-service",url = "http://101.33.248.9:8095")
@FeignClient(name = "remote-service")
public interface TbkFeignClient {
    @GetMapping("/getList")
    String getList(
            @RequestParam("pageNo") int pageNo,
            @RequestParam("pageSize") int pageSize
    );
}

其中feignclient参数为要调用的服务名称,前提是已经注册在同一个注册中心。

getList为接口方法,getmapping的值为远程服务的接口地址。


  • 调用时传入对应参数即可
@Autowired
private TbkFeignClient tbkFeignClient;


tbkFeignClient.getList(1, 100);





本文章由javascript技术分享原创和收集

发表评论 (审核通过后显示评论):

昵称:
邮箱:
内容: