首页 > 解决方案 > 如何在本地主机上运行的两个 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)都在本地运行时,我只是无法调用它的端点。任何帮助将非常感激

标签: javaspring

解决方案


您需要配置 @EnableFeignClient 注释,您还可以为您的客户所在的位置设置基本包。像这样的东西

@EnableFeignClients(basePackages = {"my.external.feign.client.package", "my.local.package"})


推荐阅读