Dapr is designed as an enterprise-class microservices programming platform for developers that is independent of specific technology platforms and can run “anywhere”. Dapr itself does not provide an “infrastructure”, but rather uses its own extensions to adapt to specific deployment environments. In its current state, a true native Dapr application can only be deployed in a K8S environment if you wish to take it into production. While Dapr also provides support for Hashicorp Consul, there does not appear to be a stable version available.
Kubernetes is not “standard” for many companies, and for some reason they may have a homegrown microservices or elastic cloud platform that may be more valuable for Dapr to adapt to. We’ve done some feasibility studies on this over the past two weeks and found that it’s really not that hard, so we’ll go over the general solution with a very simple example.
1. NameResolution Component
Although Dapr provides a series of programming models, such as service invocation, publish-subscribe and actor models, the one that is widely used should be service invocation. We know that service invocation in microservice environment needs to solve the problems of service registration and discovery, load balancing, elastic scaling, etc. In fact, Dapr does not do anything in this area, as mentioned above, Dapr itself does not provide the infrastructure, it leaves these functions to specific deployment platforms (such as K8S) to solve. The only component in Dapr related to this is a simple NameResolution component.
From a deployment point of view, all the functionality of Dapr is embodied in the Sidecar paired with the application. All we need to do when making a service call is to specify the ID (AppID) of the target application where the service is located. Service requests (HTTP or gRPC) are routed from the application to the sidecar, which “routes” the request to the appropriate node. If deployed on a Kubernetes cluster, the addressing of service requests is no longer an issue if the target service’s identity and other relevant metadata (namespace, cluster domain, etc.) are specified. In fact, the NameResolution component embodies the “resolution” for “Name” that solves the problem of converting, for example, the Dapr application-specific identifier AppID into a deployment environment-based application identifier. environment-based application identity. From the code provided by dapr, it currently registers 3 types of NameResolution components as follows.
- mdns: uses mDNS (Multicast DNS) for service registration and discovery, which is the type used by default if not explicitly configured. Since mDNS is only a type of DNS implemented using broadcast communication in small-scale networks, it is not suitable for formal generation environments at all.
- kubernetes: Adapted to Kubernetes name resolution, currently offering a stable version.
- consul: Name resolution for HashiCorp Consul, currently the latest version is Alpha.
2. Resolver
A registered NameResolution component is intended to provide a Resolver object, which is represented by the following interface. As shown in the following code snippet, the Resolver interface provides two methods. The Init method is called when the application is started and carries as parameters Metadata related to the current application instance (including the application identity and port, as well as Sidecar’s HTTP and gRPC ports, etc.) and the configuration for the current NameResolution component configuration for the current NameResolution component. For each service call, information about the target application identity and namespace is encapsulated by Sidecar into a ResolveRequest interface, and the ReolveID method of the Resolver object is invoked with the most parameters, resulting in a representation that matches the current deployment environment and is used to leverage the full target service call with the infrastructure This identifier is used to leverage the full target service invocation with the infrastructure.
|
|
3. Simulating Service Registration and Load Balancing
Assuming we have a private microservices platform that implements basic service registration, load balancing, and even elastic scaling, if we want to use Dapr on this platform, we just need to provide a corresponding Resolver object using a custom NameResolution component. We use an ASP.NET Core MVC application to simulate the microservices platform we wish to adapt to, as follows: The HomeController maintains a list of applications and endpoints (IP+port) using the static field _applications. For a service call against an application, we implement simple load balancing by polling the corresponding endpoint. For ease of description, we will refer to the application as “ServiceRegistry”.
|
|
The HomeController provides two Action methods, the Register method is used to register the application and is called by the Init method of the custom Resolver. The other method, Resolve, is used to complete the process of getting a specific endpoint based on the requested application representation, and is called by the ResolveID method of the custom Resolver. The parameter types of these two methods, RegisterRequest and ResolveRequest, are defined as follows, with the latter having the same definition as the interface of the same name given earlier. Both Actions output the appropriate text in the console showing the registered application information and the resolved endpoint.
|
|
4. Customizing the NameResolution component
Since Dapr does not support dynamic registration of components, we have to pull down its source code, modify it, and recompile it. There are two git operations involved here, dapr and components-contrib, the former for the core runtime and the latter for the community-driven contributed components. We put the cloned source code in the same directory.
We named our custom NameResolution component “svcreg” (meaning service registration), so we created a directory of the same name in the components-contrib/nameresolution directory (In this directory we will see the definition of several NameResolution components mentioned above) and defined the component code in the svcreg.go file in that directory. The complete definition of the NameResolution component is shown below.
|
|
As shown in the code snippet above, we define the core Resolver structure, which has a logger field for logging and two additional fields registerEndpoint and resolveEndpoint, representing the URLs of the two APIs provided by ServiceRegistry, respectively. In the Init method implemented for the Resolver structure, we extract the configuration from the metadata as parameters, and further extract the address of the ServiceRegistry from the configuration and add the routing paths “/register” and " /resolve" to initialize the registerEndpoint and resolveEndpoint fields of the Resolver structure. Next, we extract the AppID, IP address and internal gRPC port number (through which the external application calls the Sidecar of the current application) from the metadata, which are encapsulated into the RegisterRequest structure and serialized into JSON strings, and used as input to call the corresponding Web API to complete the corresponding service registration.
In the ResolveID implementation, we directly serialize the ResolveRequest structure as a parameter into JSON and call the Resolve API. the string carried in the body of the response is the endpoint (IP+Port) parsed for the target application, which we directly use as the return value of ResolveID.
5. Registering a Custom NameResolution Component
The custom NameResolution component needs to be explicitly registered to the executable daprd representing Sidecar, and the source file where the entry program is located is dapr/cmd/daprd/main.go. We first import the package where svcreg is located as follows.
“github.com/dapr/components-contrib/nameresolution/svcreg”.
|
|
In the main function, we find the part of the code used to register the NameResolution component and just follow the instructions for registering the svcreg as we did for the other NameResolution components. The NewResolver function used to provide the Resolver in the registration code is defined in the svcreg.go file above.
|
|
6. Compile and deploy daprd.exe
So far, all the programming work has been done, next we need to recompile daprd.exe which represents Sidecar.
As you can see from the code snippet above, the package paths of dapr are prefixed with “github.com/dapr”, so we need to modify the go.mod file (dapr/go.mod) to redirect the dependency paths to the local directory, so we added the paths for so we add a replacement rule for “github.com/dapr/components-contrib” as follows.
|
|
After switching the current directory to “dapr/cmd/daprd/”, executing “go build” from the command line will generate a daprd.exe executable file in the current directory. Now we need to use this new daprd.exe to replace the current one, which is located in the %userprofile%.dapr\bin
directory.
7. Configuring svcreg
As we have already mentioned, Dapr uses the mDNS-based NameResolution component by default (for which the registered name is “mdns”). To enable our custom component “svcreg”, we need to modify Dapr’s configuration file (%userprofile%.dapr\config.yaml). As shown in the following code snippet, we not only set the name of the used component to “svcreg” (the name provided when registering the NameResolution component in dapr/cmd/daprd/main.go), but also set the URL of the service registration API (http://127.0.0.1:3721
) in the configuration (the URL extracted by the Init method of Resolver is from here).
8. Testing the Effects
We now write a Dapr application to verify that the custom NameResolution component works. We use the example of a service call provided in ASP.NET Core 6 Framework Revealed Example Demo [03]: Dapr First Experience. App2, defined as follows, is an ASP.NET Core application that uses routing to provide APIs for addition, subtraction, multiplication, and division operations.
|
|
App1 with the following definition is a console application that calls the four APIs of the appeal using the Dapr client SDK.
|
|
After starting the ServiceRegistry, we start App2 and the console will elaborate the following output. As you can see from the name of the NameResolution component in the output, our custom svcreg is being used.
Since the Resolver’s Init method is called when the application is started to register, this is also reflected in the output of the ServiceRegistry as shown below. You can see that the AppID of the registered instance is “app2” and the corresponding endpoint is “10.181.22.4:60840”.
Then we start App1 again, and the output shown below indicates that all four service calls completed successfully.
The application instance of the launched App1 is also registered in the ServiceRegistry. Four service calls result in four calls to the Resolver’s ResolveID method, which is also reflected in the output of the ServiceRegistry.