feign调用,如何通过指定的服务名来调用,或者通过指定的url调用,方便调用本地的服务,或者调用指定的测试环境的服务

1. feign client 通过服务名来调用

 InnerOssEndpointClient 类是  feign client方法声明的类
 name 就是服务名,这里默认是通过服务名来调用。服务名在哪呢,在注册的nacos注册中心能看到

1.1 调用方的代码

关键看 @FeignClient注解的代码,name 指定了调用的服务名。服务名为 file-system

// 这是调用方的代码

@Service
public class OssEndpointServiceImpl implements OssEndpointService {

    @Resource
    private InnerOssEndpointClient ossEndpoint;

    @Override
    public R<AnnexVo> putFile(MultipartFile file, String sourceCode) {

        CjtDataSyncCurrentUtil.initUserFilter(tenantId);
        return ossEndpoint.putFile(getToken(),file);
    }


}


// InnerOssEndpointClient  feign client方法声明的类
// name 就是服务名, 这里默认是通过服务名来调用。服务名在哪呢,在注册的nacos注册中心能看到
@FeignClient(name = "file-system",contextId = "ossEndpoint")
public interface InnerOssEndpoint {
    @PostMapping(value = "/oss/endpoint/put-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    CjtResult<AnnexVo> putFile(@RequestHeader("Authorization") String token, @RequestPart(value = "file") MultipartFile file);

}

1.2 被调用方的代码

这里就是一个普通的 restful api ,接收调用请求

// 这里就是一个普通的 restful api 

@RestController
@AllArgsConstructor
@RequestMapping("/oss/endpoint")
@Api(value = "对象存储端点", tags = "对象存储端点")
public class OssEndpoint {

    /**
	 * 上传单个文件
	 *
	 * @param file 文件
	 * @return ObjectStat
	 */
	@SneakyThrows
	@PostMapping("/put-file")
	public R<Annex> putFile(@RequestParam MultipartFile file) {
		return R.data(getAnnexByUpload(file));
	}




}

nacos 上,这里就能看到注册了哪些服务,上面的服务名就是和 nacos这里的服务名要一致才能调通!! 

2. feign client 通过url来调用

上面是通过服务名来调用了,那假如我想调用,我本地的服务,没有注册到nacos的服务。那怎么调用呢

2.1 这是调用方的代码

关键看 @FeignClient注解的代码, 加了一个url,url指定的 本地的地址

@FeignClient(name = "file-system",contextId = "ossEndpoint", url = "http://localhost:8080/blade-system")
public interface InnerOssEndpoint

// 这是调用方的代码

@Service
public class OssEndpointServiceImpl implements OssEndpointService {

    @Resource
    private InnerOssEndpointClient ossEndpoint;

    @Override
    public R<AnnexVo> putFile(MultipartFile file, String sourceCode) {

        CjtDataSyncCurrentUtil.initUserFilter(tenantId);
        return ossEndpoint.putFile(getToken(),file);
    }


}


// InnerOssEndpointClient  feign client方法声明的类
// name 就是服务名,同是也指定了url,那么这里就以url为准。只要写了url就根据url来调用
 
@FeignClient(name = "file-system",contextId = "ossEndpoint", url = "http://localhost:8080/blade-system")
public interface InnerOssEndpoint {
    @PostMapping(value = "/oss/endpoint/put-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    CjtResult<AnnexVo> putFile(@RequestHeader("Authorization") String token, @RequestPart(value = "file") MultipartFile file);

}
2.2 被调用方代码和 上面的1.2 是一样的。就不贴了

3. feign client 通过url来调用,特定的环境

3.1 调用方的代码、这里和上面的 2.1 大同小异

关键看 @FeignClient注解的代码, 加了一个url,url指定的 是一个绝对地址,这样的话,就在本地就可以调用测试环境, 或者生产环境的服务了。方便debug,排查一些问题。

@FeignClient(name = "file-system",contextId = "ossEndpoint", url = "http://www.baidu.com/a/blade-system")
public interface InnerOssEndpoint

// 这是调用方的代码

@Service
public class OssEndpointServiceImpl implements OssEndpointService {

    @Resource
    private InnerOssEndpointClient ossEndpoint;

    @Override
    public R<AnnexVo> putFile(MultipartFile file, String sourceCode) {

        CjtDataSyncCurrentUtil.initUserFilter(tenantId);
        return ossEndpoint.putFile(getToken(),file);
    }


}


// InnerOssEndpointClient  feign client方法声明的类
// name 就是服务名,同是也指定了url,那么这里就以url为准。只要写了url就根据url来调用
 
@FeignClient(name = "file-system",contextId = "ossEndpoint", url = "http://www.baidu.com/a/blade-system")
public interface InnerOssEndpoint {
    @PostMapping(value = "/oss/endpoint/put-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    CjtResult<AnnexVo> putFile(@RequestHeader("Authorization") String token, @RequestPart(value = "file") MultipartFile file);

}
3.2 被调用的代码,和上面的1.2 一样
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值