2023-12-15 03:04:47 +04:00
|
|
|
|
# Отчет по лабораторной работе №3
|
|
|
|
|
|
|
|
|
|
Выполнила студентка гр. ИСЭбд-41 Островская С. Ф.
|
|
|
|
|
|
|
|
|
|
## REST API, Gateway и синхронный обмен между микросервисами
|
|
|
|
|
|
|
|
|
|
## Создание микросервисов
|
|
|
|
|
|
|
|
|
|
1. Создала первый микросервис - `dotnet new web -n worker-1`
|
|
|
|
|
2. Добавила решение - `dotnet new sln`
|
|
|
|
|
3. Связала решение и проект - `dotnet sln worker-1.sln add worker-1.csproj`
|
|
|
|
|
4. Для второго микросервиса выполнила пункты 1-3
|
|
|
|
|
5. Добавила библиотеку Swagger и OpenAi - `dotnet add worker-1.csproj package Swashbuckle.AspNetCore` , `dotnet add worker-1.csproj package Microsoft.AspNetCore.OpenApi`
|
|
|
|
|
6. Запустила - `dotnet run`
|
|
|
|
|
|
|
|
|
|
Скриншоты протестированных микросервисов:
|
|
|
|
|
![](pic/pic1.jpg)
|
|
|
|
|
![](pic/pic2.jpg)
|
|
|
|
|
|
|
|
|
|
## Реализация синхронного обмена
|
|
|
|
|
|
|
|
|
|
Код реализации синхронного обмена:
|
|
|
|
|
|
|
|
|
|
```cs
|
|
|
|
|
//Файд Program.cs проекта worker-2
|
|
|
|
|
app.MapGet("/Requests/", async () =>
|
|
|
|
|
{
|
|
|
|
|
var httpClient = new HttpClient();
|
|
|
|
|
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
|
|
|
|
|
|
|
|
|
|
return secondWorkerResponse.ToArray();
|
|
|
|
|
})
|
|
|
|
|
.WithName("GetRequests")
|
|
|
|
|
.WithOpenApi();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Реализация gateway при помощи nginx
|
|
|
|
|
|
2023-12-15 03:06:09 +04:00
|
|
|
|
Добавила nginx.conf:
|
2023-12-15 03:04:47 +04:00
|
|
|
|
|
|
|
|
|
```conf
|
|
|
|
|
server {
|
|
|
|
|
listen 8080;
|
|
|
|
|
listen [::]:8080;
|
|
|
|
|
server_name localhost;
|
|
|
|
|
|
|
|
|
|
location / {
|
|
|
|
|
root /usr/share/nginx/html;
|
|
|
|
|
index index.html index.htm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
location /worker-1/ {
|
|
|
|
|
proxy_pass http://worker-1:8080/;
|
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
|
|
proxy_set_header X-Forwarded-Prefix /worker-1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
location /worker-2/ {
|
|
|
|
|
proxy_pass http://worker-2:8080/;
|
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
|
|
proxy_set_header X-Forwarded-Prefix /worker-2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Результат, после выполнения команды `docker compose up`:
|
|
|
|
|
|
|
|
|
|
Docker:
|
|
|
|
|
|
|
|
|
|
![](pic/pic3.jpg)
|
|
|
|
|
|
|
|
|
|
index.html на gateway-1:
|
|
|
|
|
|
|
|
|
|
![](pic/pic4.jpg)
|
|
|
|
|
|
|
|
|
|
worker-1:
|
|
|
|
|
|
|
|
|
|
![](pic/pic5.jpg)
|
|
|
|
|
|
|
|
|
|
worker-2:
|
|
|
|
|
|
|
|
|
|
![](pic/pic6.jpg)
|