我正在尝试使用RPC连接管理code来启用节点故障转移后的自动重试。但是我注意到,在节点重新启动后,客户端无法与Corda节点重新连接。下面是我的代码:
class ConnectToCordaRPC(val nodeHostAndPort: NetworkHostAndPort, val
username: String, val password: String) {
lateinit var rpcConnection: CordaRPCConnection
private set
lateinit var proxy: CordaRPCOps
private set
val logger = loggerFor<ConnectToCordaRPC>()
private fun establishConnectionWithRetry(): CordaRPCConnection? {
val retryInterval = 5.seconds
do {
val connection = try {
logger.info("Connecting to: $nodeHostAndPort")
val cordaRPCClientConfiguration = CordaRPCClientConfiguration(retryInterval)
val client = CordaRPCClient(
nodeHostAndPort,
cordaRPCClientConfiguration
)
val _connection = client.start(username, password)
// Check connection is truly operational before returning it.
val nodeInfo = _connection.proxy.nodeInfo()
require(nodeInfo.legalIdentitiesAndCerts.isNotEmpty())
_connection
} catch(secEx: ActiveMQSecurityException) {
// Happens when incorrect credentials provided - no point to retry connecting.
throw secEx
}
catch(ex: RPCException) {
// Deliberately not logging full stack trace as it will be full of internal stacktraces.
logger.info("Exception upon establishing connection: " + ex.message)
null
}
if(connection != null) {
logger.info("Connection successfully established with: $nodeHostAndPort")
return connection
}
// Could not connect this time round - pause before giving another try.
Thread.sleep(retryInterval.toMillis())
} while (connection == null)
return null
}
fun performRpcReconnect() {
rpcConnection = establishConnectionWithRetry()!!
proxy = rpcConnection.proxy
val (snapshot, updates) = proxy.vaultTrack(IOUState::class.java)
//snapshot.states.forEach {}
updates.toBlocking().subscribe { update ->
update.produced.forEach { processState(it.state.data) }
}
val (stateMachineInfos, stateMachineUpdatesRaw) = proxy.stateMachinesFeed()
val retryableStateMachineUpdatesSubscription: AtomicReference<Subscription?> = AtomicReference(null)
val subscription: Subscription = stateMachineUpdatesRaw
.startWith(stateMachineInfos.map { StateMachineUpdate.Added(it) })
.subscribe({logger.info(it.id.toString())
/* Client code here */ }, {
logger.info("*********closing the connection*********")
// Terminate subscription such that nothing gets past this point to downstream Observables.
retryableStateMachineUpdatesSubscription.get()?.unsubscribe()
// It is good idea to close connection to properly mark the end of it. During re-connect we will create a new
// client and a new connection, so no going back to this one. Also the server might be down, so we are
// force closing the connection to avoid propagation of notification to the server side.
rpcConnection.forceClose()
// Perform re-connect.
performRpcReconnect()
})
retryableStateMachineUpdatesSubscription.set(subscription)
}
private fun processState(state: ContractState) {
logger.info("state is "+ state.toString())
}
}
}
class EventListenerRPC {
fun main(args: Array<String>) {
require(System.getenv("CONFIG_RPC_HOST") != null) { "CONFIG_RPC_HOST env var was not set." }
require(System.getenv("CONFIG_RPC_PORT") != null) { "CONFIG_RPC_PORT env var was not set." }
require(System.getenv("CONFIG_RPC_USERNAME") != null) { "CONFIG_RPC_USERNAME env var was not set." }
require(System.getenv("CONFIG_RPC_PASSWORD") != null) { "CONFIG_RPC_PASSWORD env var was not set." }
val nodeIpAndPort = "${System.getenv(CORDA_VARS.CORDA_NODE_HOST)}:${System.getenv(CORDA_VARS.CORDA_NODE_RPC_PORT)}"
val nodeAddress = NetworkHostAndPort.parse(nodeIpAndPort)
val nodeUsername = System.getenv(CORDA_VARS.CORDA_USER_NAME)
val nodePassword = System.getenv(CORDA_VARS.CORDA_USER_PASSWORD)
val connectToCordaRPC = ConnectToCordaRPC(nodeAddress, nodeUsername, nodePassword)
connectToCordaRPC.performRpcReconnect()
}
}
下面是客户端控制台上的日志:
16:19:21.455 [Thread-4 (ActiveMQ-client-global-threads)] WARN org.apache.activemq.artemis.core.client - AMQ212037: Connection failure has been detected: AMQ119015: The connection was disconnected because of server shutdown [code=DISCONNECTED]
16:19:21.486 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying reconnection attempt 0/-1
16:19:21.486 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying to connect with connectorFactory = org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory@f09733f, connectorConfig=TransportConfiguration(name=913dad64-f4bb-11e8-844e-5cea1d41295b, factory=org-apache-activemq-artemis-core-remoting-impl-netty-NettyConnectorFactory) ?host=192-168-249-61&port=10003&protocols=CORE,AMQP&useGlobalWorkerPool=true&RemotingThreads=-1
16:19:21.488 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Connector + NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] using nio
16:19:21.488 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Started Netty Connector version 4.1.27.Final
16:19:21.488 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Remote destination: /192.168.249.61:10003
16:19:22.729 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Connector towards NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] failed
16:19:27.732 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying reconnection attempt 1/-1
16:19:27.733 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying to connect with connectorFactory = org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory@f09733f, connectorConfig=TransportConfiguration(name=913dad64-f4bb-11e8-844e-5cea1d41295b, factory=org-apache-activemq-artemis-core-remoting-impl-netty-NettyConnectorFactory) ?host=192-168-249-61&port=10003&protocols=CORE,AMQP&useGlobalWorkerPool=true&RemotingThreads=-1
16:19:27.736 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Connector + NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] using nio
16:19:27.736 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Started Netty Connector version 4.1.27.Final
16:19:27.736 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Remote destination: /192.168.249.61:10003
16:19:28.927 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Connector towards NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] failed
16:19:33.932 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying reconnection attempt 2/-1
16:19:33.932 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying to connect with connectorFactory = org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory@f09733f, connectorConfig=TransportConfiguration(name=913dad64-f4bb-11e8-844e-5cea1d41295b, factory=org-apache-activemq-artemis-core-remoting-impl-netty-NettyConnectorFactory) ?host=192-168-249-61&port=10003&protocols=CORE,AMQP&useGlobalWorkerPool=true&RemotingThreads=-1
16:19:33.933 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Connector + NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] using nio
16:19:33.933 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Started Netty Connector version 4.1.27.Final
16:19:33.933 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Remote destination: /192.168.249.61:10003
16:19:35.128 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Connector towards NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] failed
16:19:40.132 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying reconnection attempt 3/-1
16:19:40.133 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying to connect with connectorFactory = org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory@f09733f, connectorConfig=TransportConfiguration(name=913dad64-f4bb-11e8-844e-5cea1d41295b, factory=org-apache-activemq-artemis-core-remoting-impl-netty-NettyConnectorFactory) ?host=192-168-249-61&port=10003&protocols=CORE,AMQP&useGlobalWorkerPool=true&RemotingThreads=-1
16:19:40.136 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Connector + NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] using nio
16:19:40.136 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Started Netty Connector version 4.1.27.Final
16:19:40.136 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Remote destination: /192.168.249.61:10003
16:19:41.428 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Connector towards NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] failed
16:19:46.450 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying reconnection attempt 4/-1
16:19:46.456 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Trying to connect with connectorFactory = org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory@f09733f, connectorConfig=TransportConfiguration(name=913dad64-f4bb-11e8-844e-5cea1d41295b, factory=org-apache-activemq-artemis-core-remoting-impl-netty-NettyConnectorFactory) ?host=192-168-249-61&port=10003&protocols=CORE,AMQP&useGlobalWorkerPool=true&RemotingThreads=-1
16:19:46.458 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Connector + NettyConnector [host=192.168.249.61, port=10003, httpEnabled=false, httpUpgradeEnabled=false, useServlet=false, servletPath=/messaging/ActiveMQServlet, sslEnabled=false, useNio=true] using nio
16:19:46.458 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Started Netty Connector version 4.1.27.Final
16:19:46.458 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector - Remote destination: /192.168.249.61:10003
16:19:46.620 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl - Reconnection successful
16:19:46.673 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client - AMQ214028: Couldnt reattach session {0}, performing as a failover operation now and recreating objects
16:19:46.791 [Thread-4 (ActiveMQ-client-global-threads)] DEBUG org.apache.activemq.artemis.core.client - AMQ214028: Couldnt reattach session {0}, performing as a failover operation now and recreating objects
这里可能是什么问题?是因为阻止代码订阅了新的状态更新?
答案 0 :(得分:1)
这是Enterprise RPC客户端和节点吗?如果是这样,您可以提出支持请求吗?我不确定开源RPC客户端是否具有HA /故障转移支持。