中国网站备案,酒店网站html,坪山做网站,网络营销项目策划书范文在微服务中#xff0c;如何使用feign在不同微服务中进行远程调用
在微服务中#xff0c;如何使用feign在不同微服务中进行远程调用
步骤#xff1a;
第一步#xff1a;
引入feign依赖 dependencygroupIdorg.springframework.cloud/groupId…在微服务中如何使用feign在不同微服务中进行远程调用
在微服务中如何使用feign在不同微服务中进行远程调用
步骤
第一步
引入feign依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency这里没有指定版本号是因为…中的spring-cloud指定了版本号如下 dependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement第二步在启动类中添加EnableFeignClients注解
EnableFeignClients //开启远程调用
SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}第三步编写远程调用接口。现search服务需要调用product服务下/product/attr/info/{attrId}路径下的方法该被调用的方法为 RequestMapping(/product/attr/info/{attrId})public R info(PathVariable(attrId) Long attrId){AttrRespVo respVo attrService.getAttrInfo(attrId);return R.ok().put(attr, respVo);}那么在search服务中编写的接口如下我们需要保证接口中的方法与product中被调用的方法方法名可以不同但是他们的参数、返回值类型和请求路径要一致。接口方法发送get请求.
FeignClient(name product,url http://localhost:10000)
public interface ProductFeignService {
GetMapping(/product/attr/info/{attrId})public R attrInfo(PathVariable(attrId) Long attrId);}第四步在业务中远程调用如下
Autowired
ProductFeignService productFeignService;// 远程调用
Long attrId1;
R r productFeignService.attrInfo(attrId);