使用Feign来实现微服务间的调用
在SOFAST框架中,微服务间的rpc使用Feign来实现。
微服务间的相互调用需要开发Feign接口。
Feign接口开发
下面演示biz-hello项目根据Title查询Content的feign接口的开发过程
1.创建对应微服务的sdk
使用代码生成器生成 Feign Client SDK
SOFAST为Feign接口提供了代码生成器IDEA插件 SoFast Feign Client Generator,可一键生成 Feign Client SDK。
安装SoFast Feign Client Generator插件后可以直接在HelloController点击鼠标右键选择-Generate -Feign Client
包名选择第一步创建的包(com.sofast.cloud.sdk.hello),可根据需求勾选SDK属性、熔断返回值、选择生成的方法,点击ok在工程中可以看见代码已经生成。
BizHelloConstants中定义 url常量
/*
* Copyright (c) 2019-2029. 恩梯梯数据(中国)信息技术有限公司. All Rights Reserved.
*/
package com.sofast.cloud.sdk.hello.constants;
import com.sofast.cloud.common.constants.Constants;
/**
* biz-hello服务的Feign接口常量定义
*
* @author NCIT
* @date 2022/02/10 15:10:32
*/
public interface BizHelloConstants {
String FEIGN_BIZ_HELLO_SERVICE_ID = "biz-hello";
/**
* 根据标题查询内容
*/
String FEIGN_HELLO_GETCONTENTBYTITLE = Constants.FEIGN_PREFIX + "/hello/getContentBytitle";
}
HelloFallback中定义操作API接口的断路器
/*
* Copyright (c) 2019-2029. 恩梯梯数据(中国)信息技术有限公司. All Rights Reserved.
*/
package com.sofast.cloud.sdk.hello.fallback;
import com.sofast.cloud.sdk.hello.feign.IHelloFeign;
import com.sofast.cloud.common.domain.vo.R;
import com.sofast.cloud.common.constants.MsgConstants;
import com.sofast.cloud.common.domain.vo.R;
/**
* 操作API接口的断路器
*
* @author NCIT
* @date 2022/02/10 15:10:32
*/
public class HelloFallback implements IHelloFeign {
/**
* 根据标题查询内容
* @param title 标题
* @return R.ng 熔断提示
*/
@Override
public R<String> getContentBytitle(String title) {
return R.ng(MsgConstants.ERR_E099);
}
}
IHelloFeign中定义操作API的Feign接口 需要注意
代码生成器生成的@FeignClient注解中没有指定熔断后的操作,需要手动添加fallback或fallbackFactory参数
/*
* Copyright (c) 2019-2029. 恩梯梯数据(中国)信息技术有限公司. All Rights Reserved.
*/
package com.sofast.cloud.sdk.hello.feign;
import org.springframework.cloud.openfeign.FeignClient;
import com.sofast.cloud.sdk.hello.constants.BizHelloConstants;
import com.sofast.cloud.common.domain.vo.R;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 操作API的Feign接口
*
* @author NCIT
* @date 2022/02/10 15:10:32
*/
@FeignClient(contextId = "iHelloFeign", value = BizHelloConstants.FEIGN_BIZ_HELLO_SERVICE_ID,fallback = HelloFallback.class)
public interface IHelloFeign {
/**
* 根据标题查询内容
* @param title 标题
* @return content 内容
*/
@GetMapping(value = BizHelloConstants.FEIGN_HELLO_GETCONTENTBYTITLE)
R<String> getContentBytitle(String title);
}
Feign接口实现
sdk开发好后,微服务需要依赖该sdk,并实现接口逻辑:
依赖sdk
<dependency>
<groupId>com.sofast.cloud</groupId>
<artifactId>biz-hello-sdk</artifactId>
</dependency>
在HelloProvider中 实现接口逻辑 ,提供被其他微服务调用方法
package com.sofast.cloud.biz.hello.feign;
import org.springframework.web.bind.annotation.RestController;
import com.sofast.cloud.sdk.hello.feign.IHelloFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import com.sofast.cloud.common.domain.vo.R;
import com.sofast.cloud.biz.hello.service.IHelloService;
/**
* 操作API的Feign接口提供者trim
*
* @author NCIT
* @date 2022/02/10 15:10:32
*/
@RestController
@Api(tags = "操作API")
public class HelloProvider implements IHelloFeign {
@Autowired
private IHelloService iHelloService;
/**
* 根据标题查询内容
* @param title 标题
* @return content 内容
*/
@ApiOperation(value = "根据标题查询内容")
@Override
public R<String> getContentBytitle(String title) {
String content = iHelloService.getContentByTitle( title);
return R.data(content);
}
}
Feign接口使用
下面使用biz-hello服务调用so-fast-upms服务中用户信息API
1.在biz-hello项目pom中加入so-fast-upms-sdk依赖
<dependency>
<groupId>com.sofast.cloud</groupId>
<artifactId>so-fast-upms-sdk</artifactId>
</dependency>
直接注入IUserFeign接口就可以使用接口提供的方法
@Autowired
IUserFeign iUserFeign;