Interface Gateway

  • All Superinterfaces:
    java.lang.AutoCloseable

    public interface Gateway
    extends java.lang.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:
    Developing Fabric Applications - Gateway
    • Nested Class Summary

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

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method 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.
      Identity getIdentity()
      Get the identity associated with the gateway connection.
      Network getNetwork​(java.lang.String networkName)
      Returns an object representing a network.
    • Method Detail

      • getNetwork

        Network getNetwork​(java.lang.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.
      • 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 java.lang.AutoCloseable