• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

fabric-sdk-goclient开发

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.代码结构图

2.prescription-client-config.yaml文件

  1 name: "chaoyang-prescription-network"
  2 description: "chao yang hospital prescription network"
  3 version: 1.0.0
  4 client:
  5   organization: cyorg.bjgoodwill.com
  6   logging:
  7     level: info
  8   cryptoconfig:
  9     path: ${GOPATH}/src/github.com/prescriptionserver/client/go/crypto-config
 10     #path: /opt/app/fabric/cli/user/cyorg.bjgoodwill.com/Admin-peer0cyorg.bjgoodwill..com
 11   credentialStore:
 12     path: /tmp/state-store
 13     cryptoStore:
 14       path: /tmp/msp
 15   BCCSP:
 16     security:
 17      enabled: true
 18      default:
 19       provider: "SW"
 20      hashAlgorithm: "SHA2"
 21      softVerify: true
 22      level: 256
 23   tlsCerts:
 24     systemCertPool: false
 25     client:
 26       key:
 27         path:
 28         #path: /opt/app/fabric/cli/user/cyorg.bjgoodwill.com/Admin-peer0.cyorg.bjgoodwill.com/tls/client.key
 29       cert:
 30         path:
 31         #path: /opt/app/fabric/cli/user/cyorg.bjgoodwill.com/Admin-peer0.cyorg.bjgoodwill.com/tls/client.crt
 32 channels:
 33   prescriptionchannel:
 34     peers:
 35       peer0.cyorg.bjgoodwill.com:
 36         endorsingPeer: true
 37         chaincodeQuery: true
 38         ledgerQuery: true
 39         eventSource: true
 40     policies:
 41       queryChannelConfig:
 42         minResponses: 1
 43         maxTargets: 1
 44         retryOpts:
 45           attempts: 5
 46           initialBackoff: 500ms
 47           maxBackoff: 5s
 48           backoffFactor: 2.0
 49           
 50   prescriptionmultiorgchannel:
 51 
 52     peers:
 53       peer0.cyorg.bjgoodwill.com:
 54         endorsingPeer: true
 55         chaincodeQuery: true
 56         ledgerQuery: true
 57         eventSource: true
 58 
 59       peer0.jhorg.bjgoodwill.com:
 60         endorsingPeer: true
 61         chaincodeQuery: true
 62         ledgerQuery: true
 63         eventSource: true
 64 
 65       peer0.wjworg.bjgoodwill.com:
 66         endorsingPeer: true
 67         chaincodeQuery: true
 68         ledgerQuery: true
 69         eventSource: true
 70 
 71     # [Optional]. The application can use these options to perform channel operations like retrieving channel
 72     # config etc.
 73     policies:
 74       #[Optional] options for retrieving channel configuration blocks
 75       queryChannelConfig:
 76         #[Optional] min number of success responses (from targets/peers)
 77         minResponses: 1
 78         #[Optional] channel config will be retrieved for these number of random targets
 79         maxTargets: 1
 80         #[Optional] retry options for query config block
 81         retryOpts:
 82           #[Optional] number of retry attempts
 83           attempts: 5
 84           #[Optional] the back off interval for the first retry attempt
 85           initialBackoff: 500ms
 86           #[Optional] the maximum back off interval for any retry attempt
 87           maxBackoff: 5s
 88           #[Optional] he factor by which the initial back off period is exponentially incremented
 89           backoffFactor: 2.0
 90 
 91 organizations:
 92   cyorg.bjgoodwill.com:
 93     mspid: CYOrgMSP
 94     cryptoPath: peerOrganizations/cyorg.bjgoodwill.com/users/[email protected]/msp/
 95     #cryptoPath: msp/
 96     peers:
 97       - peer0.cyorg.bjgoodwill.com
 98     certificateAuthorities:
 99       - ca.cyorg.bjgoodwill.com
100   
101   jhorg.bjgoodwill.com:
102     mspid: JHOrgMSP
103     # This org's MSP store (absolute path or relative to client.cryptoconfig)
104     cryptoPath:  peerOrganizations/jhorg.bjgoodwill.com/users/[email protected]/msp
105     peers:
106       - peer0.jhorg.bjgoodwill.com
107     certificateAuthorities:
108       - ca.jhorg.bjgoodwill.com
109   
110   wjworg.bjgoodwill.com:
111     mspid: WJWOrgMSP
112     # This org's MSP store (absolute path or relative to client.cryptoconfig)
113     cryptoPath:  peerOrganizations/wjworg.bjgoodwill.com/users/[email protected]/msp
114     peers:
115       - peer0.wjworg.bjgoodwill.com
116     certificateAuthorities:
117       - ca.wjworg.bjgoodwill.com 
118      # Orderer Org name
119   ordererorg:
120       # Membership Service Provider ID for this organization
121       mspID: "PrescriptionOrdererMSP"
122 
123       # Needed to load users crypto keys and certs for this org (absolute path or relative to global crypto path, DEV mode)
124       cryptoPath: ordererOrganizations/bjgoodwill.com/users/[email protected]/msp
125 
126 orderers:
127   orderer.bjgoodwill.com:
128     url: grpc://192.168.229.136:7050
129     grpcOptions:
130       ssl-target-name-override: orderer.bjgoodwill.com
131       keep-alive-time: 0s
132       keep-alive-timeout: 20s
133       keep-alive-permit: false
134       fail-fast: false
135       allow-insecure: false
136     tlsCACerts:
137       path: ${GOPATH}/src/github.com/prescriptionserver/client/go/crypto-config/ordererOrganizations/bjgoodwill.com/tlsca/tlsca.bjgoodwill.com-cert.pem
138       #path: /opt/app/fabric/cli/user/member1.example.com/Admin-peer0.member1.example.com/tlsca.member1.example.com-cert.pem
139 peers:
140   peer0.cyorg.bjgoodwill.com:
141     url: grpc://192.168.229.138:7051
142     eventUrl: grpc://192.168.229.138:7053
143     grpcOptions:
144       ssl-target-name-override: peer0.cyorg.bjgoodwill.com
145       keep-alive-time: 0s
146       keep-alive-timeout: 20s
147       keep-alive-permit: false
148       fail-fast: false
149       allow-insecure: false
150     tlsCACerts:
151       path: ${GOPATH}/src/github.com/prescriptionserver/client/go/crypto-config/peerOrganizations/cyorg.bjgoodwill.com/tlsca/tlsca.cyorg.bjgoodwill.com-cert.pem
152       #path: /opt/app/fabric/cli/user/cyorg.bjgoodwill.com/Admin-peer0.cyorg.bjgoodwill.com/tls/ca.crt
153  
154   peer0.jhorg.bjgoodwill.com:
155     url: peer0.jhorg.bjgoodwill.com:7051
156     eventUrl: peer0.jhorg.bjgoodwill.com:7053
157     grpcOptions:
158       ssl-target-name-override: peer0.jhorg.bjgoodwill.com
159       keep-alive-time: 0s
160       keep-alive-timeout: 20s
161       keep-alive-permit: false
162       fail-fast: false
163       allow-insecure: false
164     tlsCACerts:
165       path: ${GOPATH}/src/github.com/prescriptionserver/client/go/crypto-config/peerOrganizations/jhorg.bjgoodwill.com/tlsca/tlsca.jhorg.bjgoodwill.com-cert.pem
166       #path: /opt/app/fabric/cli/user/member1.example.com/Admin-peer0.member1.example.com/tls/ca.crt
167       
168   peer0.wjworg.bjgoodwill.com:
169     url: peer0.wjworg.bjgoodwill.com:7051
170     eventUrl: peer0.wjworg.bjgoodwill.com:7053
171     grpcOptions:
172       ssl-target-name-override: peer0.wjworg.bjgoodwill.com
173       keep-alive-time: 0s
174       keep-alive-timeout: 20s
175       keep-alive-permit: false
176       fail-fast: false
177       allow-insecure: false
178     tlsCACerts:
179       path: ${GOPATH}/src/github.com/prescriptionserver/client/go/crypto-config/peerOrganizations/wjworg.bjgoodwill.com/tlsca/tlsca.wjworg.bjgoodwill.com-cert.pem
180       #path: /opt/app/fabric/cli/user/member1.example.com/Admin-peer0.member1.example.com/tls/ca.crt
181 certificateAuthorities:
182   ca.fbi.citizens.com:
183     url: https://192.168.229.138:7054
184     tlsCACerts:
185       # Comma-Separated list of paths
186       path: ${GOPATH}/src/github.com/prescriptionserver/client/go/crypto-config/peerOrganizations/cyorg.bjgoodwill.com/tlsca/tlsca.cyorg.bjgoodwill.com-cert.pem
187       # Client key and cert for SSL handshake with Fabric CA
188       client:
189         key:
190           path: ${GOPATH}/src/github.com/prescriptionserver/client/go/crypto-config/peerOrganizations/cyorg.bjgoodwill.com/users/[email protected]/tls/client.key
191         cert:
192           path: ${GOPATH}/src/github.com/prescriptionserver/client/go/crypto-config/peerOrganizations/cyorg.bjgoodwill.com/users/[email protected]/tls/client.crt
193 
194     # Fabric-CA supports dynamic user enrollment via REST APIs. A "root" user, a.k.a registrar, is
195     # needed to enroll and invoke new users.
196     registrar:
197       enrollId: admin
198       enrollSecret: adminpw
199     # [Optional] The optional name of the CA.
200     caName: ca.cyorg.bjgoodwill.com
201 
202 entityMatchers:
203   orderer:
204     - pattern: (\w*)orderer.bjgoodwill.com(\w*)
205       urlSubstitutionExp: grpc://orderer.bjgoodwill.com:7050
206       sslTargetOverrideUrlSubstitutionExp: orderer.bjgoodwill.com
207       mappedHost: orderer.bjgoodwill.com
View Code

3.prescriptionclient.yaml文件

 1 prescriptionclient:
 2   fabricsdk:
 3     configfile: ./client/go/config/prescription-client-config.yaml
 4   resmgmt:
 5     orgname: cyorg.bjgoodwill.com
 6     orgadmin: Admin
 7   channel:
 8     channelconfig: /src/github.com/prescriptionserver/client/go/channel-artifacts/prescription.tx
 9     channelid: prescriptionchannel
10     username: User1
11   msp:
12     orgname: cyorg.bjgoodwill.com
13     mspid: CYOrgMSP
14   filepath:
15     configfilepath: ./client/go/config/prescriptionclient.yaml
View Code

4.config.go代码

 1 package util
 2 
 3 import (
 4     "encoding/json"
 5     "io/ioutil"
 6     "github.com/prescriptionserver/client/go/global"
 7     "log"
 8 )
 9 
10 var prescriptionclientconfig []byte
11 
12 type PrescriptionClientConfig struct {
13     PrescriptionClient PrescriptionClient `yaml:"prescriptionclient"`
14 }
15 type PrescriptionClient struct {
16     FabricSDK FabricSDK `yaml:"fabricsdk"`
17     ResMgmt ResMgmt `yaml:"resmgmt"`
18     Channel Channel `yaml:"channel"`
19     Msp Msp `yaml:"msp"`
20     FilePath FilePath `yaml:"filepath"`
21 }
22 type FabricSDK struct {
23     ConfigFile string `yaml:"configfile"`
24 }
25 type Channel struct {
26     ChannelConfig string `yaml:"channelconfig"`
27     ChannelID string `yaml:"channelid"`
28     UserName string `yaml:"username"`
29 }
30 type ResMgmt struct {
31     OrgName string `yaml:"orgname"`
32     OrgAdmin string `yaml:"orgadmin"`
33 }
34 type Msp struct {
35     OrgName string `yaml:"orgname"`
36     MSPID string `yaml:"mspid"`
37 }
38 type FilePath struct {
39     ConfigFilePath string `yaml:"configfilepath"`
40 }
41 
42 func GetPrescriptionClientConfig() (pcc *PrescriptionClientConfig,err error) {
43     err = json.Unmarshal(prescriptionclientconfig,&pcc)
44     return pcc,err
45 }
46 func init()  {
47     var err error
48     prescriptionclientconfig,err =ioutil.ReadFile(global.ConfigFile)
49     if err != nil{
50         global.Logger.Errorf("prescriptionclient.yaml file get error: %v",err)
51         log.Fatalf("prescriptionclient.yaml file get error: %v",err)
52     }
53 }
View Code

5.ecslfabricsdk.go代码

 1 package encapsulation
 2 
 3 import (
 4     "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
 5     "sync"
 6     "github.com/prescriptionserver/client/go/global"
 7     "github.com/hyperledger/fabric-sdk-go/pkg/core/config"
 8     "github.com/prescriptionserver/client/go/util"
 9     "github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
10     "github.com/hyperledger/fabric-sdk-go/pkg/client/event"
11 )
12 
13 type EcslFabricSDK struct {
14     sdk *fabsdk.FabricSDK
15 }
16 
17 var (
18     efsdk *EcslFabricSDK
19     once sync.Once
20 )
21 
22 func GetEcslFabricSDKInstance() (*EcslFabricSDK,error) {
23     var err error
24     once.Do(func() {
25         cf, e := util.GetPrescriptionClientConfig()
26         if e != nil{
27             global.Logger.Errorf("load prescriptionclient.yaml file error: %v\n",err)
28             err=e
29         }else {
30             configProvider :=config.FromFile(cf.PrescriptionClient.FabricSDK.ConfigFile)
31             sdk,err :=fabsdk.New(configProvider)
32             if err != nil{
33                 global.Logger.Errorf("initialize fabsdk error: %v\n",err)
34             }else {
35                 efsdk=&EcslFabricSDK{
36                     sdk:sdk,
37                 }
38                 err = nil
39             }
40         }
41 
42     })
43     return efsdk,err
44 }
45 func (efsdk *EcslFabricSDK) GetChannelAndEventClient(channelid string,
46     username string)(channelclient *channel.Client,eventclient *event.Client,err error) {
47     sdk, err := GetEcslFabricSDKInstance()
48     if err != nil{
49         return nil,nil,err
50     }
51     clientcontext := sdk.sdk.ChannelContext(channelid, fabsdk.WithUser(username))
52     channelclient,err = channel.New(clientcontext)
53     if err != nil{
54         return channelclient,nil,err
55     }
56     eventclient,err =event.New(clientcontext)
57     return channelclient,eventclient,err
58 }
59 func (efsdk *EcslFabricSDK) GetChannelClient(channelid string,
60     username string) (channelclient *channel.Client,err error) {
61     sdk, err := GetEcslFabricSDKInstance()
62     if err != nil{
63         return nil,err
64     }
65     clientcontext :=sdk.sdk.ChannelContext(channelid,fabsdk.WithUser(username))
66     channelclient,err =channel.New(clientcontext)
67     return channelclient,err
68 }
69 func (efsdk *EcslFabricSDK) Close()  {
70     if efsdk != nil{
71         efsdk.sdk.Close()
72         efsdk= nil
73     }
74 }
View Code

6.ecslresmgmt.go代码

  1 package encapsulation
  2 
  3 import (
  4     "github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
  5     "errors"
  6     "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
  7     "log"
  8     "github.com/prescriptionserver/client/go/global"
  9     "github.com/prescriptionserver/client/go/util"
 10     "os"
 11     mspc "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
 12     msp2 "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
 13     "github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
 14     "strings"
 15     "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
 16     "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
 17     "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
 18 )
 19 
 20 type EcslResmgmtClient struct {
 21     OrgName       string
 22     OrgAdmin      string
 23     ConfigFile    string
 24     initialized   bool
 25     resMgmtClient *resmgmt.Client
 26     efsdk     *EcslFabricSDK
 27 }
 28 
 29 func (e *EcslResmgmtClient) Initialize() error {
 30     if e.OrgAdmin=="" || e.OrgName=="" || e.ConfigFile==""{
 31         return errors.New("orgname or orgadmin or configfile is not null")
 32     }
 33     if e.initialized{
 34         global.Logger.Error("ecslresmgmtclient initialize again !")
 35         return nil
 36     }
 37     sdk,err := GetEcslFabricSDKInstance()
 38     if err != nil{
 39         return err
 40     }
 41     e.efsdk=sdk
 42     log.Printf("ecsl fabric sdk created")
 43     resourceManagerClientContext := e.efsdk.sdk.Context(fabsdk.WithUser(e.OrgAdmin),fabsdk.WithOrg(e.OrgName))
 44     if err != nil{
 45         return err
 46     }
 47     resMgmtClient,err:=resmgmt.New(resourceManagerClientContext)
 48     if err != nil{
 49         return err
 50     }
 51     e.resMgmtClient =resMgmtClient
 52     e.initialized=true
 53     return nil
 54 }
 55 func (e *EcslResmgmtClient) CreateChannel(channelid string,targetpeerendpoint string,
 56     orgname string,orgadmin string,ordererid string) error {
 57     if channelid == "" || targetpeerendpoint==""{
 58         return errors.New("channeldid or target peer endpoint is not null")
 59     }
 60     if e.ChannelExists(channelid,targetpeerendpoint){
 61         return errors.New("channelid already exists")
 62     }
 63     if e.initialized == false{
 64         err :=e.Initialize()
 65         if err != nil{
 66             return err
 67
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go在Linux上编译Windows执行文件发布时间:2022-07-10
下一篇:
Go学习入门发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap