Interface Gateway

All Superinterfaces:
AutoCloseable

public interface Gateway extends AutoCloseable
The Gateway provides the connection point for an application to access the Fabric network as a specific user. It is instantiated from a Builder instance that is created using 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

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.

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    The Gateway Builder interface defines the options that can be configured prior to connection.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the gateway connection and all associated resources, including removing listeners attached to networks and contracts created by the gateway.
    Creates a gateway builder which is used to configure the gateway options prior to connecting to the Fabric network.
    org.hyperledger.fabric.sdk.HFClient
    Get the Client used by the gateway connection.
    Get the identity associated with the gateway connection.
    getNetwork(String networkName)
    Returns an object representing a network.
  • Method Details

    • getNetwork

      Network getNetwork(String networkName)
      Returns an object representing a network.
      Parameters:
      networkName - The name of the network (channel name)
      Returns:
      Network
      Throws:
      GatewayRuntimeException - if a configuration or infrastructure error causes a failure.
    • getIdentity

      Identity getIdentity()
      Get the identity associated with the gateway connection.
      Returns:
      The identity used by this Gateway.
    • createBuilder

      static Gateway.Builder createBuilder()
      Creates a gateway builder which is used to configure the gateway options prior to connecting to the Fabric network.
      Returns:
      A gateway connection.
    • getClient

      org.hyperledger.fabric.sdk.HFClient getClient()
      Get the Client used by the gateway connection.
      Returns:
      The Client used by this Gateway.
    • close

      void close()
      Close the gateway connection and all associated resources, including removing listeners attached to networks and contracts created by the gateway.
      Specified by:
      close in interface AutoCloseable