12 KiB
Getting Started
Make a new application
-
Make a new folder called
microserviceand navigate to it:mkdir microservice cd microservice -
Create a frontend project:
dotnet new razor -n frontend -
Run this new project with
tyecommand line:tye run frontendWith just a single application, tye will do two things: start the frontend application and run a dashboard. Navigate to http://localhost:8000 to see the dashboard running.
The dashboard should show the
frontendapplication running. You should be able to view the application logs and you should be able to hit navigate to the application in your browser. -
Create a backend API that the frontend will call inside of the
microservices/folder.dotnet new webapi -n backend -
Create a solution file and add both projects
dotnet new sln dotnet sln add frontend dotnet sln add backendYou should have a solution called
microservice.slnthat references thefrontendandbackendprojects. -
If you haven't already, stop the existing
tye runcommand usingCtrl + C. Run thetyecommand line in the folder with the solution.tye runThe dashboard should show both the
frontendandbackendservices.
Service Discovery and Communication
-
Now that we have 2 applications running, lets make them communicate. By default,
tyeenables service discovery by injecting environment variables with a specific naming convention.Add this method to the frontend project at the bottom of the Startup.cs class:
private Uri GetUri(IConfiguration configuration, string name) { return new Uri($"http://{configuration[$"service:{name}:host"]}:{configuration[$"service:{name}:port"]}"); }This method resolved the URL using the
tyenaming convention for services. For more information on this, see the Service Definition. -
Add a file
WeatherForecast.csto thefrontendproject.using System; namespace frontend { public class WeatherForecast { public DateTime Date { get; set; } public int TemperatureC { get; set; } public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); public string Summary { get; set; } } } -
Add a file
WeatherClient.csto thefrontendproject with the following contents:using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; namespace frontend { public class WeatherClient { private readonly HttpClient client; public WeatherClient(HttpClient client) { this.client = client; } public async Task<WeatherForecast[]> GetWeatherAsync() { var responseMessage = await this.client.GetAsync("/weatherforecast"); return await JsonSerializer.DeserializeAsync<WeatherForecast[]>(await responseMessage.Content.ReadAsStreamAsync()); } } } -
Now register this client in
Startup.csclass inConfigureServicesof thefrontendproject:public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddHttpClient<WeatherClient>(client => { client.BaseAddress = GetUri(Configuration, "backend"); }); }This will wire up the
WeatherClientto use the correct URL for thebackendservice. -
Add a
Forecastsproperty to theIndexpage model underPages\Index.cshtml.csin thefrontendproject.public WeatherForecast[] Forecasts { get; set; }Change the
OnGetmethod to take theWeatherClientto call thebackendservice and store the result in theMessageproperty:public async Task OnGet([FromServices]WeatherClient client) { Forecasts = await client.GetWeatherAsync(); } -
Change the
Index.cshtmlrazor view to render theMessageproperty in the razor page:<div class="text-center"> <h1 class="display-4">Welcome</h1> <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> </div> Weather Forecast: <table class="table"> <thead> <tr> <th>Date</th> <th>Temp. (C)</th> <th>Temp. (F)</th> <th>Summary</th> </tr> </thead> <tbody> @foreach (var forecast in @Model.Forecasts) { <tr> <td>@forecast.Date.ToShortDateString()</td> <td>@forecast.TemperatureC</td> <td>@forecast.TemperatureF</td> <td>@forecast.Summary</td> </tr> } </tbody> </table> -
Run the project and the
frontendservice should be able to successfully call thebackendservice!
Adding dependencies
We just showed how tye makes it easier to communicate between 2 applications running locally but what happens if we want to use redis to store weather information?
Tye can use docker to run images that run as part of your application. If you haven't already, make sure docker is installed on your operating system (install docker) .
-
To create a
tyemanifest from the solution file.tye init microservice.slnThis will create a manifest called
tye.yamlwith the following contents:name: microservice services: - name: frontend project: frontend\frontend.csproj - name: backend project: backend\backend.csprojThis will be the source of truth for
tyeexecution from now on. To see a full schema of file, see the reference in the schema reference. -
Change the
WeatherForecastController.Getmethod in thebackendproject to cache the weather information in redis using anIDistributedCache.[HttpGet] public async Task<string> Get([FromServices]IDistributedCache cache) { var weather = await cache.GetStringAsync("weather"); if (weather == null) { var rng = new Random(); var forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); weather = JsonSerializer.Serialize(forecasts); await cache.SetStringAsync("weather", weather, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(15) }); } return weather; }This will store the weather data in Redis with an expiration time of 15 seconds.
-
Add a package reference to
Microsoft.Extensions.Caching.StackExchangeRedisin the backend project:cd backend/ dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis -
Modify
Startup.ConfigureServicesin thebackendproject to add the redisIDistributedCacheimplementation.public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddStackExchangeRedisCache(o => { var connectionString = Configuration["connectionstring:redis"]; if (connectionString != null) { o.Configuration = connectionString; } else { o.Configuration = $"{Configuration["service:redis:host"]}:{Configuration["service:redis:port"]}"; } }); }The above configures redis to use the host and port for the
redisservice injected by thetyehost. -
Modify
tye.yamlto include redis as a dependency.name: microservice services: - name: backend project: backend\backend.csproj - name: frontend project: frontend\frontend.csproj - name: redis dockerImage: redis bindings: - port: 6379 - name: redis-cli dockerImage: redis args: "redis-cli -h host.docker.internal MONITOR"We've added 2 services to the
tye.yamlfile. The redis service itself and a redis-cli service that we will use to watch the data being sent to and retrieved from redis. -
Run the
tyecommand line in the solution roottye runNavigate to http://localhost:8000 to see the dashboard running. Now you will see both
redisand theredis-clirunning. Navigate to thefrontendapplication and verify that the data returned is the same after refreshing the page multiple times. New content will be loaded every 15 seconds, so if you wait that long and refresh again, you should see new data. You can also look at the redis-cli logs and see what data is being cached in redis.
Getting Started with Deployment
-
Installing docker on your operating system.
-
A container registry. Docker by default will create a container registry on DockerHub. You could also use Azure Container Registry or another container registry of your choice.
-
A Kubernetes Cluster. You can try using a local Kubernetes cluster by enabling Kubernetes in Docker Desktop, however it does take up quite a bit of memory. You could also use Azure Kubernetes Service or another kubernetes provider of your choice.
Deploying the application
Now that we have our application running locally with multiple containers, let's deploy the application. In this example, we will deploy to Kubernetes by using tye deploy.
-
Deploy redis to Kubernetes
tye deploywill not deploy the redis configuration, so you need to deploy it first. Run:kubectl apply -f https://raw.githubusercontent.com/dotnet/tye/d79f790ba13791c1964ed03c31da0cd12b101f39/docs/yaml/redis.yaml?token=AB7K4FLEULBCQQU6NLXZEDC6OPIU4This will create a deployment and service for redis. You can see that by running:
kubectl get deploymentsYou will see redis deployed and running.
-
Adding a container registry to
tye.yamlBased on what container registry you configured, add the following line in the
tye.yamlfile:name: microservice registry: <registry_name>If you are using dockerhub, the registry_name will be in the format of 'example'. If you are using Azure Kubernetes Service (AKS), the registry_name will be in the format of example.azurecr.io.
-
Deploy to Kubernetes
Next, deploy the rest of the application by running.
tye deploytye deploy will:
- Create a docker image for each project in your application.
- Push each docker image to your container registry.
- Generate a Kubernetes Deployment and Service.
- Apply the generated Deployment and Service to your current Kubernetes context.
-
Test it out!
You should now see three pods running after deploying.
kubectl get podsNAME READY STATUS RESTARTS AGE backend-ccfcd756f-xk2q9 1/1 Running 0 85m frontend-84bbdf4f7d-6r5zp 1/1 Running 0 85m redis-5f554bd8bd-rv26p 1/1 Running 0 98m
Going deep
Replicas Setting up rabbitmq