Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Tye to assist in developing k8s applications

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to use Tye to assist the development of k8s applications, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Newbe.Claptrap is a distributed development framework for dealing with concurrency problems easily. If this is your first time reading this series of articles. It is recommended to start with the introductory article at the end of this article.

Service Discovery-an indispensable component for Micro Service Development

Service discovery means that the newly registered service module can be discovered by other callers in a timely manner. Automatic discovery can be achieved regardless of service additions and service deletions.

In the process of calling a micro service, suppose you are calling a REST API or Thrift API, and in order to complete a call request, the code needs to specify the IP address and port where the service is located. In traditional applications, the network address and port are static and will not change. We only need to assign them to the configuration file, and we can complete the call by reading the configuration file. However, in the modern Cloud-based micro-service architecture, this approach will fail, because the instance of the service is dynamically assigned address, and the network address is also dynamic. The advantage of doing so is to facilitate automatic scaling, failure handling and upgrade of the service.

To put it simply, through service discovery, names can be used between services instead of specific addresses and ports and even access details. This makes it easier for services to adapt to cloud native environments where application instances are changeable.

First, we need two services.

As in the previous article, we use the command line to create two services.

Dotnet new sln-n TyeTestdotnet new webapi-n TyeTestdotnet sln.\ TyeTest.sln add.\ TyeTest\ TyeTest.csprojdotnet new webapi-n TyeTest2dotnet sln.\ TyeTest.sln add.\ TyeTest2\ TyeTest2.csproj

Then use tye init to create the tye.yml.

You can get the following content in tye.yml:

Name: tyetestservices:-name: tyetest project: TyeTest/TyeTest.csproj-name: tyetest2 project: TyeTest2/TyeTest2.csproj

This allows us to start two services locally using tye run.

Next, we will modify the TyeTest service to call TyeTest2 as its downstream service.

This allows us to verify the effectiveness of service discovery.

Then, add the package using Tye.Configuration

Run the following command to add a package for the TyeTest project:

Dotnet add. / TyeTest/TyeTest.csproj package Microsoft.Tye.Extensions.Configuration-- version 0.6.0-alpha.21070.5 adds HttpClientFactory

Since we need to use HttpClient to invoke downstream services, we need to use HttpClientFactory. Therefore, the registration of HttpClientFactory is added to the Startup.cs of the TyeTest project.

Public void ConfigureServices (IServiceCollection services) {+ services.AddHttpClient (); services.AddControllers (); services.AddSwaggerGen (c = > {c.SwaggerDoc ("v1", new OpenApiInfo {Title = "TyeTest", Version = "v1"});});} invoke the service using HttpClient

When we enter WeatherForecastController, we use HttpClient to invoke the downstream service and return the resulting data:

Using System;using System.Collections.Generic;using System.Linq;using System.Net.Http;using System.Text.Json;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.Logging;namespace TyeTest.Controllers {[ApiController] [Route ("[controller]")] public class WeatherForecastController: ControllerBase {private readonly ILogger _ logger; private readonly IConfiguration _ configuration; private readonly HttpClient _ httpClient Public WeatherForecastController (ILogger logger, IConfiguration configuration, HttpClient httpClient) {_ logger = logger; _ configuration = configuration; _ httpClient = httpClient;} [HttpGet] public async Task Get () {var serviceUri = _ configuration.GetServiceUri ("tyetest2"); Console.WriteLine (serviceUri) Var httpResponseMessage = await _ httpClient.GetAsync ($"{serviceUri} WeatherForecast"); var json = await httpResponseMessage.Content.ReadAsStringAsync (); return json;}}

It is worth noting that:

The IConfiguration injected in the constructor is the inherent mechanism of Aspnet Core and does not require special registration.

_ configuration.GetServiceUri ("tyetest2") is the key point of this example. It obtains the specific Uri address of the service through a service name, so that it can mask the details of the service address during deployment.

In this way, it's over.

Next, as long as you use tye run, you can view the modified services locally. Call the interface of the first service and get the expected data returned from the second service.

Finally, send it to K8S to try.

If you want to publish to K8s for testing, you can verify it by setting it to docker registry and ingress as described in the previous article.

Developers can configure and try on their own.

After reading the above, have you mastered how to use Tye to assist in the development of k8s applications? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report