![](/img/trans.png)
[英]Configuring ConnectionFactory for RabbitMQ in Spring Boot AMQP
[英]Dynamically setting host for Spring AMQP and RabbitMQ on Spring Boot
我有一個問題,我不知道如何動態設置主機並在不同主機上進行 RPC 操作
這是情況
我有多個 RabbitMQ 在不同的服務器和網絡(即 192.168.1.0/24、192.168.2.0/24)上運行。
行為將是我有一個 IP 地址列表,我將使用它執行 RPC。 因此,對於 ip 地址列表中的每個條目,我想執行convertSendAndReceive
並處理回復等。
嘗試了文檔中的一些代碼,但似乎即使無效地址(沒有運行有效 RabbitMQ 的地址,或者網絡上不存在事件,例如 1.1.1.1 的地址)也無法由有效的 RabbitMQ 接收(例如在 192.168.1.1 上運行)
注意:我可以在正確的地址上成功執行 RPC 調用,但是,我也可以在我不認為的無效地址上成功執行 RPC 調用
有人對此有任何想法嗎?
這是我的來源
TaskSchedulerConfiguration.java
@Configuration
@EnableScheduling
public class TaskSchedulerConfiguration {
@Autowired
private IpAddressRepo ipAddressRepo;
@Autowired
private RemoteProcedureService remote;
@Scheduled(fixedDelayString = "5000", initialDelay = 2000)
public void scheduledTask() {
ipAddressRepo.findAll().stream()
.forEach(ipaddress -> {
boolean status = false;
try {
remote.setIpAddress(ipaddress);
remote.doSomeRPC();
} catch (Exception e) {
logger.debug("Unable to Connect to licenser server: {}", license.getIpaddress());
logger.debug(e.getMessage(), e);
}
});
}
}
RemoteProcedureService.java
@Service
public class RemoteProcedureService {
@Autowired
private RabbitTemplate template;
@Autowired
private DirectExchange exchange;
public boolean doSomeRPC() throws JsonProcessingException {
//I passed this.factory.getHost() so that i will know if only the valid ip address will be received by the other side
//at this point, other side receives invalid ipaddress which supposedly will not be receive by the oher side
boolean response = (Boolean) template.convertSendAndReceive(exchange.getName(), "rpc", this.factory.getHost());
return response;
}
public void setIpAddress(String host) {
factory.setHost(host);
factory.setCloseTimeout(prop.getRabbitMQCloseConnectTimeout());
factory.setPort(prop.getRabbitMQPort());
factory.setUsername(prop.getRabbitMQUsername());
factory.setPassword(prop.getRabbitMQPassword());
template.setConnectionFactory(factory);
}
}
AmqpConfiguration.java
@Configuration
public class AmqpConfiguration {
public static final String topicExchangeName = "testExchange";
public static final String queueName = "rpc";
@Autowired
private LicenseVisualizationProperties prop;
//Commented this out since this will only be assigne once
//i need to achieve to set it dynamically in order to send to different hosts
//so put it in RemoteProcedureService.java, but it never worked
// @Bean
// public ConnectionFactory connectionFactory() {
// CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
// connectionFactory.setCloseTimeout(prop.getRabbitMQCloseConnectTimeout());
// connectionFactory.setPort(prop.getRabbitMQPort());
// connectionFactory.setUsername(prop.getRabbitMQUsername());
// connectionFactory.setPassword(prop.getRabbitMQPassword());
// return connectionFactory;
// }
@Bean
public DirectExchange exhange() {
return new DirectExchange(topicExchangeName);
}
}
更新 1
似乎,在循環期間,當在 CachingConnectionFactory 中設置有效的 ip 時,在CachingConnectionFactory
尋址循環之后,無論有效還是無效,都會被 CachingConnectionFactory 中的第一個有效CachingConnectionFactory
集合接收
更新 2
我發現一旦它可以成功建立連接,它就不會創建新的連接。 你如何強制RabbitTemplate
建立一個新的連接?
這是一個相當奇怪的用例,性能不會很好; 你最好有一個連接工廠和模板池。
但是,要回答您的問題:
調用resetConnection()
關閉連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.