Virtualizing gRPC Service
Install devtool
Install the npm cli tool. Make sure npm module in on system path by running a command.
Initialize project
- Go to projejet directory and run
grpc init
. - Enter an output path
- Enter path containing proto files
- Choose files to create default mappings for
- There is a new directory created in project.
Check created grpc.yaml
-
This is the file with default values
-
All these values can be overrided using command line parameters
-
grpc.yaml
# Address for our server host: 'localhost' port: '3009' # We will add adress of real service here remoteHost: 'localhost' remotePort: '3010' # Relativ path to any other folder containing our proto files protos: ../protos # While recording sreams, it will save only 10 messages by default trimmedStreamSize: 10
Check created mappings.yaml
and default repsonses
-
It created a
mapping.yaml
file asiit.paas.services.fx.FxAutoQuote.GetPrice: - data/GetPrice/default.yaml iit.paas.services.fx.FxAutoQuote.GetPriceStream: - data/GetPriceStream/default.yaml iit.paas.services.fx.FxAutoQuote.GetIncrementalPriceStream: - data/GetIncrementalPriceStream/default.yaml
-
And corresponding templates that can return some values for eny endpoint :
request@ : { "uic": "@any", "spread_set_id": "@any", "commission_group_id": "@any", "client_class_id": "@any", "liquidity_factor_option": "@any", "amount": "@any", "order_price": "@any", "trade_history_amount_option": "@any", "accumulated_amount": "@any", "buy_sell": "@any", "order_type": "@any", "order_duration": "@any", "field_ids": "@any" } response@ : { "field_values": ["2.3"] }
Configure app to use devtool server
-
Change the address of remote service in app to use our grpc-devtool server
"ServiceSettings": { - "gRPCServer": "DEVTCE1-DK1.sys.dom", + "gRPCServer": "localhost", - "gRPCPort": 7903, + "gRPCPort": 3009, }
-
Also in
grpc.yaml
set the address of our real service. The devtool will use it to record from remote service.host: 'localhost' port: '3009' - remoteHost: '' + remoteHost: 'DEVTCE1-DK1.sys.dom' - remotePort: '' + remotePort: '7903' protos: ../protos trimmedStreamSize: 10
Record gRPC interactions
-
Go to our project and run devtool in record mode
cd Stub grpc record .
-
Make an API call in the app
Map recorded files
- New files are created in devtool folder under
recorded@
- Rename and organize these files as per your preferences
- Update the
mapping.yaml
to map them to endpoints
Run service against devtool server
-
No stop the recorder and start devtool in server mode
grpc serve .
Thats it ! Now our app is running against fixed data. Check out the streaming API. It returns the same set of elements circularily.