public interface Gateway extends AutoCloseable
createBuilder() and configured using a common
connection profile and a Wallet identity. It can then be connected to a fabric network using the
Gateway.Builder.connect() method. Once connected, it can then access individual Network instances (channels)
using the getNetwork method which in turn can access the Contract installed on a
network and submit transactions to the ledger.
Gateway instances should be reused for multiple transaction invocations and only closed once connection to the Fabric network is no longer required.
Gateway.Builder builder = Gateway.createBuilder()
.identity(wallet, "user1")
.networkConfig(networkConfigFile);
try (Gateway gateway = builder.connect()) {
Network network = gateway.getNetwork("mychannel");
// Interactions with the network
}
Service discovery allows the client to dynamically discover nodes (peers and orderers) within a network that are
not defined in the connection profile, and is enabled using Gateway.Builder.discovery(boolean). With
service discovery enabled, the client will receive the public network address of each node. If these network
addresses can be reached directly by the client, no additional configuration is required.
In some cases nodes cannot be accessed directly by the client on their public network address. For example, when nodes are hosted in Docker containers, and the client is running on the local machine outside of the Docker network. In this case service discovery will report the network address of nodes within the Docker network but the client will need to access nodes using the localhost address. Setting the following environment variable will force service discovery to report the localhost address for all nodes.
org.hyperledger.fabric.sdk.service_discovery.as_localhost=true
An alternative approach is to modify the local hosts file for the client machine to resolve the network address of nodes running in a Docker network to the localhost address.
Note that the port numbers of nodes obtained through service discovery remain unchanged so ports forwarded from the local machine's network must match the port numbers of nodes within the Docker network.
| Modifier and Type | Interface and Description |
|---|---|
static interface |
Gateway.Builder
The Gateway Builder interface defines the options that can be configured
prior to connection.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close the gateway connection and all associated resources, including removing listeners attached to networks and
contracts created by the gateway.
|
static Gateway.Builder |
createBuilder()
Creates a gateway builder which is used to configure the gateway options
prior to connecting to the Fabric network.
|
org.hyperledger.fabric.sdk.HFClient |
getClient()
Get the Client used by the gateway connection.
|
Identity |
getIdentity()
Get the identity associated with the gateway connection.
|
Network |
getNetwork(String networkName)
Returns an object representing a network.
|
Network getNetwork(String networkName)
networkName - The name of the network (channel name)NetworkGatewayRuntimeException - if a configuration or infrastructure error causes a failure.Identity getIdentity()
static Gateway.Builder createBuilder()
org.hyperledger.fabric.sdk.HFClient getClient()
void close()
close in interface AutoCloseableCopyright © 2023. All rights reserved.