title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
22:149 | Open the GrpcMicroServiceDocker solution in Visual Studio. The solution contains two microservices, called FakeSource and GrpcMicroservice. The last project is just the data layer of the GrpcMicroservice project. |
22:150 | |
22:151 | Figure 22.7: GrpcMicroServiceDocker solution |
22:152 | The solution is already configured to launch the two microservices when it is run. In other cases, you might need to configure multiple project launches by right-clicking on the solution node and selecting Set Startup Projects…. |
22:153 | Adding Docker support to both microservices is super easy. Right-click on each microservice project within Visual Studio. Navigate to Add, and then select Docker Support. If prompted, choose the operating system for your Docker environment. If you are using Minikube, you must select Linux. |
22:154 | All the necessary Docker files are automatically created and configured by Visual Studio. And that’s it! |
22:155 | Now, we need to move the database to the newly installed SQL Server instance. |
22:156 | Moving GrpcMicroServiceDocker to SQL Server Express |
22:157 | You need to change all connection strings and configure the string that will be used at runtime so that it can be used from inside a Docker image. |
22:158 | First of all, let’s change the connection string that is inside GrpcMicroServiceStore-> LibraryDesignTimeDbContextFactory.cs. The new string should be something like this: |
22:159 | @`Server=<your machine name><your instance name>;Database=grpcmicroservice;Trusted_Connection=True;Trust Server Certificate=True;MultipleActiveResultSets=true` |
22:160 | |
22:161 | where the instance name should be SQLEXPRESS. You can take the above connection string directly from Visual Studio, as follows: |
22:162 | |
22:163 | Open the SQL Server Object Explorer window. |
22:164 | Right-click on the SQL Server node and select Add SQL Server. |
22:165 | In the window that opens, Visual Studio should enumerate all available SQL Server instances. Choose the newly installed SQL Server instance. |
22:166 | Select Windows authentication and connect. |
22:167 | A new server icon should appear below the SQL Server node. Select it. |
22:168 | In the Visual Studio Properties tab, you should see all database connection properties. Take the value of General-> Connection string. |
22:169 | |
22:170 | Now, you have to run all migrations to recreate the database in the new SQL Server instance. As usual, right-click on the library project and define it as a startup project. Then, in the Visual Studio Package Manager Console Default Project, select GrpcMicroServiceStore and issue the Update-Database command. |
22:171 | After the new database has been created, restore the two microservices as simultaneous startup projects. |
22:172 | Finally, update the runtime connection string in GrpcMicroService -> appsettings.json. If the newly installed SQL Server instance has been defined as the default instance on your machine, the connection string below should work: |
22:173 | Server=host. Docker.internal;Database=grpcmicroservice;User Id=<your user name>;Password=<your user password>;Trust Server Certificate=True;MultipleActiveResultSets=true` |
22:174 | |
22:175 | where host. Docker.internal is the URL used by Docker Desktop images to communicate with the host machine. If your SQL Server is not the machine’s default instance, you must replace host.docker.internal with host.docker.internal<your instance name>. |
22:176 | If, instead, you are using an external database, you can use its standard connection string with no modifications. |
22:177 | Enabling communication among microservices with a Docker virtual network |
22:178 | Creating a Docker virtual network in Docker Desktop is easy; just open a Windows console and run the following command: |
22:179 | docker network create test-net, |
22:180 | |
22:181 | where test-net is the virtual network name. Once the network has been defined when we create a container instance from an image, we can specify that the launched container must be connected to our network and its hostname, with something like: |
22:182 | docker run --rm --net test-net --name grpcmicroservice <microservice image name>, |
22:183 | |
22:184 | Here, the rm option specifies that the container must be destroyed when it stops running, --net test-net specifies the network where to connect the created container, and --name grpcmicroservice is the name of the created container that will also act as its hostname in the network. |
22:185 | We need to add to our test-net just the containers that must act as servers—in our case, the GrpcMicroService microservice. |
22:186 | Since Visual Studio automatically issues all necessary run Docker commands when the solution is launched, we need just to specify the command options to add to Visual Studio’s original command. They must be specified in each microservice project file with the DockerfileRunArguments parameter. Below is how to modify the GrpcMicroService microservice project file, which is the only microservice acting as a server: |
22:187 | <PropertyGroup> |
22:188 | <TargetFramework>net8.0</TargetFramework> |
22:189 | <Nullable>enable</Nullable> |
22:190 | <UserSecretsId>b4f03ff2-033c-4d5e-a33b-65f26786b052</UserSecretsId> |
22:191 | <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> |
22:192 | <DockerfileRunArguments> |
22:193 | --rm --net test-net --name grpcmicroservice |
22:194 | </DockerfileRunArguments> |
22:195 | </PropertyGroup> |
22:196 | |
22:197 | No modification is required to the FakeSource project, since it must not act as a server. |
22:198 | Now, the grpcmicroservice hostname must be used by FakeSource to communicate with the GrpcMicroService microservice. |
22:199 | Therefore, we must replace the URL in the FakeSource->Worker.cs file with http://grpcmicroservice:8080, as shown in the code snippet below: |
22:200 | … |
22:201 | using var channel = GrpcChannel.ForAddress(`http://grpcmicroservice:8080`); |
22:202 | var client = new Counter.CounterClient(channel); |
22:203 | … |
22:204 | |
22:205 | where we use the 8080 default Kestrel http port to communicate with the microservice. Therefore, we need to the Kestrel options in GrpcMicroService ->Program.cs that force Kestrel to listen to the 5000 port by replacing the code below: |
22:206 | builder.WebHost.ConfigureKestrel(options => |
22:207 | { |
22:208 | options.ListenLocalhost(5000, o => |
22:209 | o.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2); |
22:210 | }); |
22:211 | |
22:212 | Now, we are ready to run our project. To be sure that both microservices are started with Docker, please select each of them as a single startup project, and then select Docker in the select box next to the run solution Visual Studio button, as shown in the image below: |
22:213 | |
22:214 | Figure 22.8: Selecting Docker execution |
22:215 | After that, you can restore the simultaneous launch of both microservices. Visual Studio will launch both of them with Docker. |
22:216 | Now, we can launch the solution. In order to verify that the server is properly receiving purchase information, place a breakpoint in the GrpcMicroService->HostedServices-> ProcessPurchases.cs file inside of the if block below: |
22:217 | if (toProcess.Count > 0) |
22:218 | { |
22:220 | } |
22:221 | |
22:222 | In fact, GrpcMicroService enters that block only if it finds something in the input queue. |
22:223 | You can also inspect the content of the dbo.Purchases database table to verify that it is filled with statistics on purchases. You can do it from within SQL Server Object Explorer by right-clicking on the table and choosing View Data. |
22:224 | Having understood how to test our application with a Docker network, we must now understand when and how to test it with Minikube also. |
22:225 | When to test the application with Minikube |
22:226 | Most of the debug-fix cycle involved in the application development can be done with the Docker virtual network. |
22:227 | |
22:228 | Docker networks usually work well without creating issues. So, if you experience communication problems, they are probably due to misspelled service URLs. Therefore, please double-check the URLs in all calls to the microservice that does not receive communications. |
22:229 | |
22:230 | From time to time, we need to test an application with Minikube for the following reasons: |
22:231 | |
22:232 | Both ReplicaSets and StatefulSets can be tested with Docker and Visual Studio, but we are limited to a single POD for each of them. |
22:233 | We must also test the .yaml Kubernetes configuration file, which might contain more complex objects like ingresses, permanent storage, secrets, and other complex configurations. |
22:234 | You might need to integrate your microservices with other modules developed by other teams. |
22:235 | |
22:236 | Therefore, each developer should spend most of their time testing a few microservices that strongly interact among them with the Docker virtual network, but from time to time, they should try a wider integration with Minikube. This can be done before committing their code at the end of the working day, or just before closing a development iteration of the agile application development process. |
22:237 | Having learned when and how to test the application with Minikube, we must learn how to load and run our application on Minikube. |
22:238 | Running your application in Minikube |
22:239 | When Visual Studio runs your microservices with Docker, it creates special images that also contain information needed by the Visual Studio debugger and have a dev version name. These special images can be run just from Visual Studio, and if you try to launch them manually, you will get an error. For the same reason, you can’t use them in Minikube. |
22:240 | Therefore, the first step for running your microservice in Minikube is to create different “standard” images. You can do this by right-clicking both the FakeSource and GrpcMicroService Docker files in Visual Studio Solution Explorer and by selecting Build Docker Image. |
22:241 | This way, you will create a grpcmicroservice and a fakesource image, both with the latest version name, as shown in the image below: |
22:242 | |
22:243 | Figure 22.9: Creating Minikube-ready Docker images |
22:244 | As a next step, you must start Minikube: |
22:245 | minikube start |
22:246 | |
22:247 | Now, you must load your Docker images inside of the Minikube images cache with the following commands: |
22:248 | minikube image load fakesource:latest |
22:249 | minikube image load grpcmicroservice:latest |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.