github.com/true-sqn/fabric@v2.1.1+incompatible/docs/source/developapps/connectionprofile.md (about)

     1  # Connection Profile
     2  
     3  **Audience**: Architects, application and smart contract developers
     4  
     5  A connection profile describes a set of components, including peers, orderers
     6  and certificate authorities in a Hyperledger Fabric blockchain network. It also
     7  contains channel and organization information relating to these components. A
     8  connection profile is primarily used by an application to configure a
     9  [gateway](./gateway.html) that handles all network interactions, allowing it
    10  to focus on business logic. A connection profile is normally created by an
    11  administrator who understands the network topology.
    12  
    13  In this topic, we're going to cover:
    14  
    15  * [Why connection profiles are important](#scenario)
    16  * [How applications use a connection profile](#usage)
    17  * [How to define a connection profile](#structure)
    18  
    19  ## Scenario
    20  
    21  A connection profile is used to configure a gateway. Gateways are important for
    22  [many reasons](./gateway.html), the primary being to simplify an application's
    23  interaction with a network channel.
    24  
    25  ![profile.scenario](./develop.diagram.30.png) *Two applications, issue and buy,
    26   use gateways 1&2 configured with connection profiles 1&2. Each profile
    27   describes a different subset of MagnetoCorp and DigiBank network components.
    28   Each connection profile must contain sufficient information for a gateway to
    29   interact with the network on behalf of the issue and buy applications. See the
    30   text for a detailed explanation.*
    31  
    32  A connection profile contains a description of a network view, expressed in a
    33  technical syntax, which can either be JSON or YAML. In this topic, we use the
    34  YAML representation, as it's easier for you to read. Static gateways need more
    35  information than dynamic gateways because the latter can use [service
    36  discovery](../discovery-overview.html) to dynamically augment the information in
    37  a connection profile.
    38  
    39  A connection profile should not be an exhaustive description of a network
    40  channel; it just needs to contain enough information sufficient for a gateway
    41  that's using it. In the network above, connection profile 1 needs to contain at
    42  least the endorsing organizations and peers for the `issue` transaction, as well
    43  as identifying the peers that will notify the gateway when the transaction has
    44  been committed to the ledger.
    45  
    46  It's easiest to think of a connection profile as describing a *view* of the
    47  network. It could be a comprehensive view, but that's unrealistic for a few
    48  reasons:
    49  
    50  * Peers, orderers, certificate authorities, channels, and organizations are
    51    added and removed according to demand.
    52  
    53  * Components can start and stop, or fail unexpectedly (e.g. power outage).
    54  
    55  * A gateway doesn't need a view of the whole network, only what's necessary to
    56    successfully handle transaction submission or event notification for example.
    57  
    58  * Service Discovery can augment the information in a connection profile.
    59    Specifically, dynamic gateways can be configured with minimal Fabric topology
    60    information; the rest can be discovered.
    61  
    62  A static connection profile is normally created by an administrator who
    63  understands the network topology in detail. That's because a static profile can
    64  contain quite a lot of information, and an administrator needs to capture this
    65  in the corresponding connection profile. In contrast, dynamic profiles minimize
    66  the amount of definition required and therefore can be a better choice for
    67  developers who want to get going quickly, or administrators who want to create a
    68  more responsive gateway. Connection profiles are created in either the YAML or
    69  JSON format using an editor of choice.
    70  
    71  ## Usage
    72  
    73  We'll see how to define a connection profile in a moment; let's first see how it
    74  is used by a sample MagnetoCorp `issue` application:
    75  
    76  ```javascript
    77  const yaml = require('js-yaml');
    78  const { Gateway } = require('fabric-network');
    79  
    80  const connectionProfile = yaml.safeLoad(fs.readFileSync('../gateway/paperNet.yaml', 'utf8'));
    81  
    82  const gateway = new Gateway();
    83  
    84  await gateway.connect(connectionProfile, connectionOptions);
    85  ```
    86  
    87  After loading some required classes, see how the `paperNet.yaml` gateway file is
    88  loaded from the file system, converted to a JSON object using the
    89  `yaml.safeLoad()` method, and used to configure a gateway using its `connect()`
    90  method.
    91  
    92  By configuring a gateway with this connection profile, the issue application is
    93  providing the gateway with the relevant network topology it should use to
    94  process transactions. That's because the connection profile contains sufficient
    95  information about the PaperNet channels, organizations, peers, orderers and CAs
    96  to ensure transactions can be successfully processed.
    97  
    98  It's good practice for a connection profile to define more than one peer for any
    99  given organization -- it prevents a single point of failure. This practice also
   100  applies to dynamic gateways; to provide more than one starting point for service
   101  discovery.
   102  
   103  A DigiBank `buy` application would typically configure its gateway with a
   104  similar connection profile, but with some important differences. Some elements
   105  will be the same, such as the channel; some elements will overlap, such as the
   106  endorsing peers. Other elements will be completely different, such as
   107  notification peers or certificate authorities for example.
   108  
   109  The `connectionOptions` passed to a gateway complement the connection profile.
   110  They allow an application to declare how it would like the gateway to use the
   111  connection profile. They are interpreted by the SDK to control interaction
   112  patterns with network components, for example to select which identity to
   113  connect with, or which peers to use for event notifications. Read
   114  [about](./connectionoptions.html) the list of available connection options and
   115  when to use them.
   116  
   117  ## Structure
   118  
   119  To help you understand the structure of a connection profile, we're going to
   120  step through an example for the network shown [above](#scenario). Its connection
   121  profile is based on the PaperNet commercial paper sample, and
   122  [stored](https://github.com/hyperledger/fabric-samples/blob/{BRANCH}/commercial-paper/organization/magnetocorp/gateway/networkConnection.yaml)
   123  in the GitHub repository. For convenience, we've reproduced it [below](#sample).
   124  You will find it helpful to display it in another browser window as you now read
   125  about it:
   126  
   127  * Line 9: `name: "papernet.magnetocorp.profile.sample"`
   128  
   129    This is the name of the connection profile. Try to use DNS style names; they
   130    are a very easy way to convey meaning.
   131  
   132  
   133  * Line 16: `x-type: "hlfv1"`
   134  
   135    Users can add their own `x-` properties that are "application-specific" --
   136    just like with HTTP headers. They are provided primarily for future use.
   137  
   138  
   139  * Line 20: `description: "Sample connection profile for documentation topic"`
   140  
   141    A short description of the connection profile. Try to make this helpful for
   142    the reader who might be seeing this for the first time!
   143  
   144  
   145  * Line 25: `version: "1.0"`
   146  
   147    The schema version for this connection profile.  Currently only version 1.0 is
   148    supported, and it is not envisioned that this schema will change frequently.
   149  
   150  
   151  * Line 32: `channels:`
   152  
   153    This is the first really important line. `channels:` identifies that what
   154    follows are *all* the channels that this connection profile describes. However,
   155    it is good practice to keep different channels in different connection
   156    profiles, especially if they are used independently of each other.
   157  
   158  
   159  * Line 36: `papernet:`
   160  
   161    Details of `papernet`, the first channel in this connection profile, will
   162    follow.
   163  
   164  
   165  * Line 41: `orderers:`
   166  
   167    Details of all the orderers for `papernet` follow. You can see in line 45 that
   168    the orderer for this channel is `orderer1.magnetocorp.example.com`. This is
   169    just a logical name; later in the connection profile (lines 134 - 147), there
   170    will be details of how to connect to this orderer. Notice that
   171    `orderer2.digibank.example.com` is not in this list; it makes sense that
   172    applications use their own organization's orderers, rather than those from a
   173    different organization.
   174  
   175  
   176  * Line 49: `peers:`
   177  
   178    Details of all the peers for `papernet` will follow.
   179  
   180    You can see three peers listed from MagnetoCorp:
   181    `peer1.magnetocorp.example.com`, `peer2.magnetocorp.example.com` and
   182    `peer3.magnetocorp.example.com`. It's not necessary to list all the peers in
   183    MagnetoCorp, as has been done here. You can see only one peer listed from
   184    DigiBank: `peer9.digibank.example.com`; including this peer starts to imply
   185    that the endorsement policy requires MagnetoCorp and DigiBank to endorse
   186    transactions, as we'll now confirm. It's good practice to have multiple peers
   187    to avoid single points of failure.
   188  
   189    Underneath each peer you can see four non-exclusive roles: **endorsingPeer**,
   190    **chaincodeQuery**, **ledgerQuery** and **eventSource**. See how `peer1` and
   191    `peer2` can perform all roles as they host `papercontract`. Contrast to
   192    `peer3`, which can only be used for notifications, or ledger queries that
   193    access the blockchain component of the ledger rather than the world state, and
   194    hence do not need to have smart contracts installed. Notice how `peer9` should
   195    not be used for anything other than endorsement, because those roles are
   196    better served by MagnetoCorp peers.
   197  
   198    Again, see how the peers are described according to their logical names and
   199    their roles. Later in the profile, we'll see the physical information for
   200    these peers.
   201  
   202  
   203  * Line 97: `organizations:`
   204  
   205    Details of all the organizations will follow, for all channels.  Note that
   206    these organizations are for all channels, even though `papernet` is currently
   207    the only one listed.  That's because organizations can be in multiple
   208    channels, and channels can have multiple organizations. Moreover, some
   209    application operations relate to organizations rather than channels. For
   210    example, an application can request notification from one or all peers within
   211    its organization, or all organizations within the network -- using [connection
   212    options](./connectoptions.html).  For this, there needs to be an organization
   213    to peer mapping, and this section provides it.
   214  
   215  * Line 101: `MagnetoCorp:`
   216  
   217    All peers that are considered part of MagnetoCorp are listed: `peer1`,
   218    `peer2` and `peer3`. Likewise for Certificate Authorities. Again, note the
   219    logical name usages, the same as the `channels:` section; physical information
   220    will follow later in the profile.
   221  
   222  
   223  * Line 121: `DigiBank:`
   224  
   225    Only `peer9` is listed as part of DigiBank, and no Certificate Authorities.
   226    That's because these other peers and the DigiBank CA are not relevant for
   227    users of this connection profile.
   228  
   229  
   230  * Line 134: `orderers:`
   231  
   232    The physical information for orderers is now listed. As this connection
   233    profile only mentioned one orderer for `papernet`, you see
   234    `orderer1.magnetocorp.example.com` details listed. These include its IP
   235    address and port, and gRPC options that can override the defaults used when
   236    communicating with the orderer, if necessary. As with `peers:`, for high
   237    availability, specifying more than one orderer is a good idea.
   238  
   239  
   240  * Line 152: `peers:`
   241  
   242    The physical information for all previous peers is now listed.  This
   243    connection profile has three peers for MagnetoCorp: `peer1`, `peer2`, and
   244    `peer3`; for DigiBank, a single peer `peer9` has its information listed. For
   245    each peer, as with orderers, their IP address and port is listed, together
   246    with gRPC options that can override the defaults used when communicating with
   247    a particular peer, if necessary.
   248  
   249  
   250  * Line 194: `certificateAuthorities:`
   251  
   252    The physical information for certificate authorities is now listed.  The
   253    connection profile has a single CA listed for MagnetoCorp, `ca1-magnetocorp`,
   254    and its physical information follows. As well as IP details, the registrar
   255    information allows this CA to be used for Certificate Signing Requests (CSR).
   256    These are used to request new certificates for locally generated
   257    public/private key pairs.
   258  
   259  Now you've understood a connection profile for MagnetoCorp, you might like to
   260  look at a
   261  [corresponding](https://github.com/hyperledger/fabric-samples/blob/{BRANCH}/commercial-paper/organization/magnetocorp/gateway/networkConnection.yaml)
   262  profile for DigiBank. Locate where the profile is the same as MagnetoCorp's, see
   263  where it's similar, and finally where it's different. Think about why these
   264  differences make sense for DigiBank applications.
   265  
   266  That's everything you need to know about connection profiles. In summary, a
   267  connection profile defines sufficient channels, organizations, peers, orderers
   268  and certificate authorities for an application to configure a gateway. The
   269  gateway allows the application to focus on business logic rather than the
   270  details of the network topology.
   271  
   272  ## Sample
   273  
   274  This file is reproduced inline from the GitHub commercial paper
   275  [sample](https://github.com/hyperledger/fabric-samples/blob/{BRANCH}/commercial-paper/organization/magnetocorp/gateway/networkConnection.yaml).
   276  
   277  ```yaml
   278  1: ---
   279  2: #
   280  3: # [Required]. A connection profile contains information about a set of network
   281  4: # components. It is typically used to configure gateway, allowing applications
   282  5: # interact with a network channel without worrying about the underlying
   283  6: # topology. A connection profile is normally created by an administrator who
   284  7: # understands this topology.
   285  8: #
   286  9: name: "papernet.magnetocorp.profile.sample"
   287  10: #
   288  11: # [Optional]. Analogous to HTTP, properties with an "x-" prefix are deemed
   289  12: # "application-specific", and ignored by the gateway. For example, property
   290  13: # "x-type" with value "hlfv1" was originally used to identify a connection
   291  14: # profile for Fabric 1.x rather than 0.x.
   292  15: #
   293  16: x-type: "hlfv1"
   294  17: #
   295  18: # [Required]. A short description of the connection profile
   296  19: #
   297  20: description: "Sample connection profile for documentation topic"
   298  21: #
   299  22: # [Required]. Connection profile schema version. Used by the gateway to
   300  23: # interpret these data.
   301  24: #
   302  25: version: "1.0"
   303  26: #
   304  27: # [Optional]. A logical description of each network channel; its peer and
   305  28: # orderer names and their roles within the channel. The physical details of
   306  29: # these components (e.g. peer IP addresses) will be specified later in the
   307  30: # profile; we focus first on the logical, and then the physical.
   308  31: #
   309  32: channels:
   310  33:   #
   311  34:   # [Optional]. papernet is the only channel in this connection profile
   312  35:   #
   313  36:   papernet:
   314  37:     #
   315  38:     # [Optional]. Channel orderers for PaperNet. Details of how to connect to
   316  39:     # them is specified later, under the physical "orderers:" section
   317  40:     #
   318  41:     orderers:
   319  42:     #
   320  43:     # [Required]. Orderer logical name
   321  44:     #
   322  45:       - orderer1.magnetocorp.example.com
   323  46:     #
   324  47:     # [Optional]. Peers and their roles
   325  48:     #
   326  49:     peers:
   327  50:     #
   328  51:     # [Required]. Peer logical name
   329  52:     #
   330  53:       peer1.magnetocorp.example.com:
   331  54:         #
   332  55:         # [Optional]. Is this an endorsing peer? (It must have chaincode
   333  56:         # installed.) Default: true
   334  57:         #
   335  58:         endorsingPeer: true
   336  59:         #
   337  60:         # [Optional]. Is this peer used for query? (It must have chaincode
   338  61:         # installed.) Default: true
   339  62:         #
   340  63:         chaincodeQuery: true
   341  64:         #
   342  65:         # [Optional]. Is this peer used for non-chaincode queries? All peers
   343  66:         # support these types of queries, which include queryBlock(),
   344  67:         # queryTransaction(), etc. Default: true
   345  68:         #
   346  69:         ledgerQuery: true
   347  70:         #
   348  71:         # [Optional]. Is this peer used as an event hub? All peers can produce
   349  72:         # events. Default: true
   350  73:         #
   351  74:         eventSource: true
   352  75:       #
   353  76:       peer2.magnetocorp.example.com:
   354  77:         endorsingPeer: true
   355  78:         chaincodeQuery: true
   356  79:         ledgerQuery: true
   357  80:         eventSource: true
   358  81:       #
   359  82:       peer3.magnetocorp.example.com:
   360  83:         endorsingPeer: false
   361  84:         chaincodeQuery: false
   362  85:         ledgerQuery: true
   363  86:         eventSource: true
   364  87:       #
   365  88:       peer9.digibank.example.com:
   366  89:         endorsingPeer: true
   367  90:         chaincodeQuery: false
   368  91:         ledgerQuery: false
   369  92:         eventSource: false
   370  93: #
   371  94: # [Required]. List of organizations for all channels. At least one organization
   372  95: # is required.
   373  96: #
   374  97: organizations:
   375  98:    #
   376  99:    # [Required]. Organizational information for MagnetoCorp
   377  100:   #
   378  101:   MagnetoCorp:
   379  102:     #
   380  103:     # [Required]. The MSPID used to identify MagnetoCorp
   381  104:     #
   382  105:     mspid: MagnetoCorpMSP
   383  106:     #
   384  107:     # [Required]. The MagnetoCorp peers
   385  108:     #
   386  109:     peers:
   387  110:       - peer1.magnetocorp.example.com
   388  111:       - peer2.magnetocorp.example.com
   389  112:       - peer3.magnetocorp.example.com
   390  113:     #
   391  114:     # [Optional]. Fabric-CA Certificate Authorities.
   392  115:     #
   393  116:     certificateAuthorities:
   394  117:       - ca-magnetocorp
   395  118:   #
   396  119:   # [Optional]. Organizational information for DigiBank
   397  120:   #
   398  121:   DigiBank:
   399  122:     #
   400  123:     # [Required]. The MSPID used to identify DigiBank
   401  124:     #
   402  125:     mspid: DigiBankMSP
   403  126:     #
   404  127:     # [Required]. The DigiBank peers
   405  128:     #
   406  129:     peers:
   407  130:       - peer9.digibank.example.com
   408  131: #
   409  132: # [Optional]. Orderer physical information, by orderer name
   410  133: #
   411  134: orderers:
   412  135:   #
   413  136:   # [Required]. Name of MagnetoCorp orderer
   414  137:   #
   415  138:   orderer1.magnetocorp.example.com:
   416  139:     #
   417  140:     # [Required]. This orderer's IP address
   418  141:     #
   419  142:     url: grpc://localhost:7050
   420  143:     #
   421  144:     # [Optional]. gRPC connection properties used for communication
   422  145:     #
   423  146:     grpcOptions:
   424  147:       ssl-target-name-override: orderer1.magnetocorp.example.com
   425  148: #
   426  149: # [Required]. Peer physical information, by peer name. At least one peer is
   427  150: # required.
   428  151: #
   429  152: peers:
   430  153:   #
   431  154:   # [Required]. First MagetoCorp peer physical properties
   432  155:   #
   433  156:   peer1.magnetocorp.example.com:
   434  157:     #
   435  158:     # [Required]. Peer's IP address
   436  159:     #
   437  160:     url: grpc://localhost:7151
   438  161:     #
   439  162:     # [Optional]. gRPC connection properties used for communication
   440  163:     #
   441  164:     grpcOptions:
   442  165:       ssl-target-name-override: peer1.magnetocorp.example.com
   443  166:       request-timeout: 120001
   444  167:   #
   445  168:   # [Optional]. Other MagnetoCorp peers
   446  169:   #
   447  170:   peer2.magnetocorp.example.com:
   448  171:     url: grpc://localhost:7251
   449  172:     grpcOptions:
   450  173:       ssl-target-name-override: peer2.magnetocorp.example.com
   451  174:       request-timeout: 120001
   452  175:   #
   453  176:   peer3.magnetocorp.example.com:
   454  177:     url: grpc://localhost:7351
   455  178:     grpcOptions:
   456  179:       ssl-target-name-override: peer3.magnetocorp.example.com
   457  180:       request-timeout: 120001
   458  181:   #
   459  182:   # [Required]. Digibank peer physical properties
   460  183:   #
   461  184:   peer9.digibank.example.com:
   462  185:     url: grpc://localhost:7951
   463  186:     grpcOptions:
   464  187:       ssl-target-name-override: peer9.digibank.example.com
   465  188:       request-timeout: 120001
   466  189: #
   467  190: # [Optional]. Fabric-CA Certificate Authority physical information, by name.
   468  191: # This information can be used to (e.g.) enroll new users. Communication is via
   469  192: # REST, hence options relate to HTTP rather than gRPC.
   470  193: #
   471  194: certificateAuthorities:
   472  195:   #
   473  196:   # [Required]. MagnetoCorp CA
   474  197:   #
   475  198:   ca1-magnetocorp:
   476  199:     #
   477  200:     # [Required]. CA IP address
   478  201:     #
   479  202:     url: http://localhost:7054
   480  203:     #
   481  204:     # [Optioanl]. HTTP connection properties used for communication
   482  205:     #
   483  206:     httpOptions:
   484  207:       verify: false
   485  208:     #
   486  209:     # [Optional]. Fabric-CA supports Certificate Signing Requests (CSRs). A
   487  210:     # registrar is needed to enroll new users.
   488  211:     #
   489  212:     registrar:
   490  213:       - enrollId: admin
   491  214:         enrollSecret: adminpw
   492  215:     #
   493  216:     # [Optional]. The name of the CA.
   494  217:     #
   495  218:     caName: ca-magnetocorp
   496  ```
   497  
   498  <!--- Licensed under Creative Commons Attribution 4.0 International License
   499  https://creativecommons.org/licenses/by/4.0/ -->