Spring boot 我可以动态创建一个外部客户机还是创建一个具有不同名称的实例
我已经定义了一个REST接口,该接口使用不同的Spring boot 我可以动态创建一个外部客户机还是创建一个具有不同名称的实例,spring-boot,spring-cloud-netflix,spring-cloud-feign,netflix-ribbon,feign,Spring Boot,Spring Cloud Netflix,Spring Cloud Feign,Netflix Ribbon,Feign,我已经定义了一个REST接口,该接口使用不同的Spring.Application.name(在我的业务中,Spring.Application.name不能是相同的) 我怎样才能只定义一个假客户机,并访问所有SpringBootApplication REST服务 SpringBootApplication A(spring.application.name=A)和B(spring.application.name=)具有以下RestService: @RestController @Requ
Spring.Application.name
(在我的业务中,Spring.Application.name
不能是相同的)
我怎样才能只定义一个假客户机,并访问所有SpringBootApplication REST服务
SpringBootApplication A(spring.application.name=A)和B(spring.application.name=)具有以下RestService:
@RestController
@RequestMapping(value = "/${spring.application.name}")
public class FeignRestService {
@Autowired
Environment env;
@RequestMapping(path = "/feign")
public String feign() {
return env.getProperty("server.port");
}
}
另一个SpringBootC应用程序:
@FeignClient(name="SpringApplication A or B")
public interface FeignClientService {
@RequestMapping(path = "/feign")
public String feign();
}
在SpringBootC应用程序中,我想使用一个假客户端服务来访问a和B。你有什么想法吗?你可能已经知道了,但这可能会帮助任何正在寻找相同问题答案的人。您需要为每个使用服务的服务客户端配置外部客户端
不可能使用同一个外部客户端调用不同的服务,因为外部客户端绑定到您定义的服务 是的,您可以创建一个外部客户机,并根据需要重复使用它来调用Eureka目录中的不同命名服务(您用spring cloud netflix标记了这个问题)。下面是一个如何操作的示例:
@Component
public class DynamicFeignClient {
interface MyCall {
@RequestMapping(value = "/rest-service", method = GET)
void callService();
}
FeignClientBuilder feignClientBuilder;
public DynamicFeignClient(@Autowired ApplicationContext appContext) {
this.feignClientBuilder = new FeignClientBuilder(appContext);
}
/*
* Dynamically call a service registered in the directory.
*/
public void doCall(String serviceId) {
// create a feign client
MyCall fc =
this.feignClientBuilder.forType(MyCall.class, serviceId).build();
// make the call
fc.callService();
}
}
调整调用接口以满足您的需求,然后您可以在需要使用它的bean中插入并使用DynamicFeignClient
实例
几个月来,我们一直在生产中使用这种方法来查询几十种不同的服务,以获取版本信息和其他有用的运行时数据