85 lines
2.4 KiB
Markdown
85 lines
2.4 KiB
Markdown
# Отчет по лабораторной работе №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. Повторил действие для второго микросервиса
|
||
5. Добавил библиотеку Swagger и OpenAi в проекты и запустил с помощью команды `dotnet run`
|
||
|
||
Скриншоты протестированных микросервисов:
|
||

|
||
|
||
## Реализация синхронного обмена
|
||
|
||
Реализовал код, который вызывает сихронно данные из соседнего микросервиса.
|
||
|
||
```cs
|
||
//worker-2
|
||
app.MapGet("/Genres/", async () =>
|
||
{
|
||
var httpClient = new HttpClient();
|
||
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
|
||
|
||
return secondWorkerResponse.ToArray();
|
||
})
|
||
.WithName("GetCategories")
|
||
.WithOpenApi();
|
||
```
|
||
|
||
## Реализация gateway при помощи nginx
|
||
|
||
Добавил nginx.conf:
|
||
|
||
```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:
|
||
|
||

|
||
|
||
index.html на gateway-1:
|
||
|
||

|
||
|
||
worker-1:
|
||
|
||

|
||
|
||
worker-2:
|
||
|
||
 |