Create client connection to a specified org? #fabric-sdk-go #fabric-questions


Yueming Xu
 

When I use the following code to create a fabric client, it randomly chooses a peer of any org defined in the network config file.  Is it possible to change the code to connect to a peer of a specified org, even though the network config file defines multiple orgs?
```
    sdk, err := fabsdk.New(networkConfigProvider(config.NetworkConfig, config.EntityMatchers))
    if err != nil {
        return nil, errors.Wrapf(err, "Failed to create new SDK")
    }

    opts := []fabsdk.ContextOption{fabsdk.WithUser(config.UserName)}
    if config.OrgName != "" {
        opts = append(opts, fabsdk.WithOrg(config.OrgName))
    }
    client, err := channel.New(sdk.ChannelContext(config.ChannelID, opts...))
```


Xixue Jia
 

Hi Yueming,

You may use `channel.WithTargetEndpoints` to specify the peer you want to connect to. see -> https://github.com/hyperledger/fabric-sdk-go/blob/master/pkg/client/channel/api.go#L66

e.g.
client.Query(channel.Request{ChaincodeID: cc.name, Fcn: cc.args[0],
Args: argsByte, TransientMap: cc.transientMap}, channel.WithTargetEndpoints(peers...))

On Mon, Jul 22, 2019 at 11:13 AM Yueming Xu <yxucolo@...> wrote:
When I use the following code to create a fabric client, it randomly chooses a peer of any org defined in the network config file.  Is it possible to change the code to connect to a peer of a specified org, even though the network config file defines multiple orgs?
```
    sdk, err := fabsdk.New(networkConfigProvider(config.NetworkConfig, config.EntityMatchers))
    if err != nil {
        return nil, errors.Wrapf(err, "Failed to create new SDK")
    }

    opts := []fabsdk.ContextOption{fabsdk.WithUser(config.UserName)}
    if config.OrgName != "" {
        opts = append(opts, fabsdk.WithOrg(config.OrgName))
    }
    client, err := channel.New(sdk.ChannelContext(config.ChannelID, opts...))
```