transfer-example

module
v0.0.0-...-c435387 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2023 License: MIT

README

transfer

测试dtm添加dtmdriver-sponge驱动后,使用服务注册与发现示例,示例中所涉及的服务运行在同一个机器,因此ip地址都是127.0.0.1,如果有服务运行在不同机器,需要在transfer和dtm配置文件把127.0.0.1修改为对应的宿主机ip或域名。

关键代码:

使用etcd作为服务发现示例

  1. 启动etcd服务。

  2. 修改dtm配置文件。

MicroService:
   Driver: 'dtm-driver-sponge'
   Target: 'etcd://127.0.0.1:2379/dtmservice'
   EndPoint: 'grpc://127.0.0.1:36790'
  1. 启动dtm服务。
dmt -c conf.yml
  1. 修改transfer配置。

打开配置文件 configs/transfer.yml,修改app和grpcClient下字段registryDiscoveryType值,表示使用etcd作为服务注册与发现。

app:
  registryDiscoveryType: "etcd"       # registry and discovery types: consul, etcd, nacos, if empty, registration and discovery are not used

grpcClient:
  - name: "dtmservice"   # grpc service name, used for service discovery
    registryDiscoveryType: "etcd"    # registration and discovery types: consul, etcd, nacos, if empty, connecting to server using host and port
    host: "127.0.0.1"       # grpc service address, used for direct connection
    port: 36790               # grpc service port

etcd:
  addrs: ["127.0.0.1:2379"]
  1. 启动 transfer 服务
# 编译和运行服务
make run
  1. 测试

打开生成的grpc客户端测试代码 internal/service/transfer_client_test.go,测试前填写参数,示例

		{
			name: "Transfer",
			fn: func() (interface{}, error) {
				// todo type in the parameters to test
				req := &transferV1.TransferRequest{
					Amount:     100,
					FromUserId: 1,
					ToUserId:   2,
				}
				return cli.Transfer(ctx, req)
			},
			wantErr: false,
		},

打开一个新终端,切换到目录internal/service,执行命令测试:

go test -run Test_service_transfer_methods/Transfer

Note:如果使用Goland IDE打开代码internal/service/transfer_client_test.go,直接点击左边绿色按钮测试。

使用consul作为服务发现

与上面使用etcd作为服务发现的6个操作步骤一样,把etcd改为consul,重启dtm和transfer。

使用nacos作为服务发现

与上面使用etcd作为服务发现的6个操作步骤一样,把etcd改为nacos,重启dtm和transfer。

注:在dmt和transfer的配置文件的namespaceID必须一样,默认namespaceID是public,如果指定其他值,必须同时修改。

Directories

Path Synopsis
api
cmd
transfer
Package main is the grpc server of the application.
Package main is the grpc server of the application.
transfer/initial
Package initial is the package that starts the service to initialize the service, including the initialization configuration, service configuration, connecting to the database, and resource release needed when shutting down the service.
Package initial is the package that starts the service to initialize the service, including the initialization configuration, service configuration, connecting to the database, and resource release needed when shutting down the service.
Package configs used to locate config file.
Package configs used to locate config file.
internal
server
Package server is a package that holds the http or grpc service.
Package server is a package that holds the http or grpc service.
service
Package service A grpc server-side or client-side package that handles business logic.
Package service A grpc server-side or client-side package that handles business logic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL