Microservices
In the previous article we have shown how to quickly create a single service, next we will show how to quickly create a microservice. In this section, the api part is actually the same logic as the monolithic service, except that there is no communication between services in the monolithic service, and the api service in the microservice will have more configuration for rpc calls.
#
PrefaceThis section will be a simple demonstration of an order service
to call the user service
, the demo code only to pass the idea, some of the links will not be enumerated.
#
Scenario SummarySuppose we are developing a mall project, and the developer Xiaoming is responsible for the development of the user module (user) and the order module (order), let's split these two modules into two microservices ①
note
①: The splitting of microservices is also a learning curve, so we won't discuss the details of how to split microservices here.
#
Demonstrate functional goals- Order service(order) provides a query interface
- user service (user) provides a method for the order service to obtain user information
#
Service Design AnalysisAccording to the scenario synopsis we can learn that the order is directly user-oriented, accessing data through the http protocol, and the order internal need to obtain some basic data about the user, since our service is designed using the microservices architecture. Then the two services (user,order) must exchange data, the data exchange between services that is, the communication between services, to here, the use of a reasonable communication protocol is also a developer needs to Here we choose rpc to realize the communication between services, and I believe I have already made a better scenario of "What is the role of rpc service? I believe I have already described a good scenario here. Of course, there is much more to a service than just design analysis before development, so we won't go into detail here. From the above, we know that we need a
- user rpc
- order api
two services to initially implement this little demo.
#
Create mall projectIf you run the monolithic example, which is also called
go-zero-demo
, you may need to change the parent directory.
note
If there is no cd
operation to change the directory, all operations are performed in the `go-zero-demo
directory
#
Create user rpc serviceCreate the user rpc service
Add
user.proto
file, addgetUser
methodAdd the following code.
Generate the code
tip
grpc directive details refer to https://grpc.io/docs/languages/go/quickstart/
Padding business logic
#
Create order api serviceCreate
order api
serviceAdd api file
Generate the order service
Add user rpc configuration
Add yaml configuration
refine the service dependencies
Adding order demo logic
Add business logic to
getorderlogic
#
Start the service and verifystart etcd
download dependencies
start user rpc
start order api
Accessing the order api
tip
The api syntax mentioned in the demo, rpc generation, goctl, goctl environment, etc. how to use and install, the quick start does not provide a detailed overview, we will have a detailed description of the subsequent documentation, you can also click the following [guess what you want to see] quick jump to see the corresponding documentation.