public async Task TestClusterConnectionWithAuthAsync() { var resolver = new NatsConnectionResolver(); resolver.Configure(ConfigParams.FromTuples( "connections.0.protocol", "nats", "connections.0.host", "server1", "connections.0.port", 4222, "connections.1.protocol", "nats", "connections.1.host", "server2", "connections.1.port", 4222, "connections.2.protocol", "nats", "connections.2.host", "server3", "connections.2.port", 4222, "credential.username", "test", "credential.password", "pass123", "credential.token", "ABC" )); var connection = await resolver.ResolveAsync(null); Assert.Equal("nats://server1:4222,nats://server2:4222,nats://server3:4222", connection.GetAsString("uri")); Assert.Equal("test", connection.GetAsString("username")); Assert.Equal("pass123", connection.GetAsString("password")); Assert.Equal("ABC", connection.GetAsString("token")); }
public async Task TestSingleConnectionAsync() { var resolver = new NatsConnectionResolver(); resolver.Configure(ConfigParams.FromTuples( "connection.protocol", "nats", "connection.host", "localhost", "connection.port", 4222 )); var connection = await resolver.ResolveAsync(null); Assert.Equal("nats://localhost:4222", connection.GetAsString("uri")); Assert.Null(connection.GetAsString("username")); Assert.Null(connection.GetAsString("password")); Assert.Null(connection.GetAsString("token")); }
/// <summary> /// Opens the component. /// </summary> /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param> public async Task OpenAsync(string correlationId) { var options = await _connectionResolver.ResolveAsync(correlationId); var opts = ConnectionFactory.GetDefaultOptions(); opts.AllowReconnect = _retryConnect; opts.MaxReconnect = _maxReconnect; opts.ReconnectWait = _reconnectTimeout; var uri = options.GetAsString("uri"); opts.Servers = uri.Split(','); var username = options.GetAsString("username"); if (!string.IsNullOrEmpty(username)) { opts.User = username; var password = options.GetAsString("password"); opts.Password = password; } var token = options.GetAsString("token"); if (!string.IsNullOrEmpty(token)) { opts.Token = token; } try { var connection = new ConnectionFactory().CreateConnection(opts); _connection = connection; } catch (Exception ex) { _logger.Error(correlationId, ex, "Failed to connect to NATS broker at " + uri); throw ex; } _logger.Debug(correlationId, "Connected to NATS broker at " + uri); }