java - 如何在本地主机上运行的两个 Spring Boot 应用程序上调用端点
问题描述
我在 localhost 上运行两个 Spring Boot 应用程序。它们在不同的端口上运行。我们称它们为 SBA1(Spring Boot App)和 SBA2。我需要使用 SBA2 中的一个端点。我已经直接在 SBA2 的 swagger UI 上对其进行了测试,并且我知道它的工作原理。但是当我尝试使用 SBA1 中的所述端点时,我似乎无法调用它。这是我到目前为止所尝试的,
这是调用调用 SBA2 端点的类的服务:
@Service
public class HierarchyServiceImpl implements HierarchyService {
@Autowired
private PolicyRepository service;
//this is the class that calls SBA2's end point
@Autowired
private RuleEngineApi api;
@Override
public Policy calculateCollection(Collection collection) {
Policy policy = service.getPolicyData(collection.getPolicyNumber());
CollectionMapper mapper = new CollectionMapper();
Rule facts = new Rule();
facts.setFacts(mapper.mapCollections(collection, policy));
Rule rule = api.analyzeRules(facts);
return policy;
}
}
这是调用 SBA2 端点的类:
@FeignClient(name = "rule-engine-service", url = "http://localhost:8080")
public interface RuleEngineApi {
@PostMapping(value = "/v1/rule/analyzer", consumes = "application/json")
public Rule analyzeRules(Rule rule);
}
这种方法的问题是应用程序无法找到RuleEngineApi
该类的 bean。当我尝试运行 SBA1 时,它会这样说:
required a bean of type '<path of class>.RuleEngineApi' that could not be found.\r\n\r\n\r\nAction:\r\n\r\nConsider defining a bean of type '<path of class>.RuleEngineApi' in your configuration.
我试过类似的东西:
RuleEngineApi api = null;
Rule rule = api.analyzeRules(facts);
但当然,那返回了一个NullPointerException
. 我只想强调 SBA2 运行良好。当它们(SBA1 和 SBA2)都在本地运行时,我只是无法调用它的端点。任何帮助将非常感激
解决方案
您需要配置 @EnableFeignClient 注释,您还可以为您的客户所在的位置设置基本包。像这样的东西
@EnableFeignClients(basePackages = {"my.external.feign.client.package", "my.local.package"})
推荐阅读
- php - 动态更改 json 键名
- javascript - AWS lambda:“模块‘exports’上缺少处理程序‘handle’”
- python - 散景没有用 SQL 数据解释正确的比例
- r - R ggplot2 - 仅为 x 轴的一部分添加功能区
- c# - 将多个模型组合成 1 个 JSON WebAPI 响应
- excel - 通过 Excel 的 eDrawings API
- java - java比较器按多个字段排序
- amazon-web-services - API 网关的“导出为 Swagger”和“导出为 Swagger + Postman”缺少“参数”对象
- node.js - 错误:没有 HttpClient 的提供者!角度错误信息
- angular - Ng2-select 不适用于 Angular 6