RPC Implement & Call
In a large system, there must be data transfer between multiple subsystems (services). If there is data transfer, you need a communication method. You can choose the simplest http for communication or rpc service for communication. In go-zero, we use zrpc to communicate between services, which is based on grpc.
#
ScenesIn the front, we have improved the interface protocol for user login, user query of books, etc., but the user did not do any user verification when querying the book. If the current user is a non-existent user, we do not allow him to view book information. From the above information, we can know that the user service needs to provide a method to obtain user information for use by the search service, so we need to create a user rpc service and provide a getUser method.
#
rpc service writing- Compile the proto file
- Generate rpc service code
tip
If the installed version of protoc-gen-go
is larger than 1.4.0, it is recommended to add go_package
to the proto file
- Add configuration and improve yaml configuration items
tip
$user: mysql database user
$password: mysql database password
$url: mysql database connection address
$db: mysql database db name, that is, the database where the user table is located
$host: Redis connection address Format: ip:port, such as: 127.0.0.1:6379
$pass: redis password
$etcdHost: etcd connection address, format: ip:port, such as: 127.0.0.1:2379
For more configuration information, please refer to rpc configuration introduction
- Add resource dependency
- Add rpc logic
#
Use rpcNext we call user rpc in the search service
- Add UserRpc configuration and yaml configuration items
tip
$AccessSecret: This value must be consistent with the one declared in the user api.
$AccessExpire: Valid period
$etcdHost: etcd connection address
The Key
in etcd must be consistent with the Key in the user rpc service configuration
- Add dependency
- Supplementary logic
#
Start and verify the service- Start etcd, redis, mysql
- Start user rpc
- Start search api
- Verification Service