Service-mesh-application Access
Preparation
Service Mesh is an infrastructure layer dedicated to solving communication between services, and is used to handle communication between services. Service Mesh is usually implemented through a group of sidecar proxies deployed with application codes without sensing the application itself.
We have provided simple demos for you to quickly get to know and experience our platform: Provider and consumer demos.
Create a Provider
Here, taking the Go language as an example, a provider application is created locally and its own service is registered with the service registration center of the micro service platform so that the consumer can call it.
1.Create a web application that listens on Port 9091 for consumers to call
// Provider health check interface
http.HandleFunc("/health", healthCheck)
// Provider functional interface
http.HandleFunc("/echo", echo)
if err := http.ListenAndServe(":9091", nil); err != nil {
og.Println("ListenAndServe: ", err)
}
2.Code compilation
GOOS=linux GOARCH=amd64 go build
Create a Consumer
Here, taking the Go language as an example, a consumer application is created locally and its own service is registered with the service registration center of the micro service platform. At the same time, the application can discover the provider service and complete the call.
1.Create a web application that listens on Port 9090 for external access
// External access interface, which calls the echo interface of mesh-provider and returns the processing result
http.HandleFunc("/echo", echo)
// Consumer health check interface
http.HandleFunc("/health", healthCheck)
if err := http.ListenAndServe(":9090", nil); err != nil {
log.Println("ListeneAndServe error: ", err)
os.Exit(-1)
}
2.Add call chain headers to codes
TRACE_HEADERS = []string{
"X-B3-SpanId",
"X-B3-TraceId",
"X-B3-ParentSpanId",
"X-B3-Sampled",
"X-B3-Flags",
"X-Ot-Span-Context",
"X-Request-Id",
"X-Method",
"X-ParentMethod"}
Note: Mesh's call chain is implemented through header passing. If the user wants to obtaine different service call relationship, he needs to bring the parent call chain head when accessing other services. Call chain and topology are omitted if not used. 3.Code compilation
GOOS=linux GOARCH=amd64 go build
Deploy CNAP
1.Make an image
Refer to the Mesh Image section in the image production document.
Taking the consumer as an example, the Dockerfile needed to make the image is as follows:
Use the above Dockerfile to complete the image compilation:
docker build -t hub.baidubce.com/[namespace]/[ImageName]:[Image version] .
> Note: namespace is the name of the namespace created in the environment preparation; ImageName is the name of the image specified by the user to create;
Push the compiled image to the image warehouse:
docker push hub.baidubce.com/[namespace]/[ImageName]:[Image version]
# Basic image, optional for user
FROM hub.baidubce.com/cnap-public/mesh-base:ubuntu
# Add binary files
COPY mesh-consumer /usr/local/bin
# Add startup command
CMD ["/usr/local/bin/mesh-consumer", "-a", "http://mesh-provider:9091"]
2.Deploy using CNAP
Please refer to the service deployment section in CNAP Operation Guide.
Note: For mesh-type applications, you need to fill in the mesh service port on the console. At the same time, the filled application name needs to be consistent with the service name.
3.Check the deployment results
You will see the service information on the registration in CNAP service list page after successful deployment.
4.Service verification You can observe if the call returns as expected by calling the following interface.
curl xx.xx.xx.xx:9090/echo?value=helloworld
5.Routing Service Mesh supports routing without the need for users to transform services. For the user manual, refer to the routing section in microservice management.