[Chanaka Fernando] Designing Microservices Platforms with NATS [ENG, 2021]


English 2021 ISBN: 978-1801072212 356 Pages PDF, EPUB 20 MB


GitHub:
https://github.com/PacktPublishing/Designing-Microservices-Platforms-with-NATS


A Practical Example of Microservices with NATS

Сразу пробуем практику


$ apt install -y jq


$ docker run -p 4222:4222 -ti nats:latest


$ cd ~/tmp/
$ git clone [email protected]:PacktPublishing/Designing-Microservices-Platforms-with-NATS.git


$ cd Designing-Microservices-Platforms-with-NATS/chapter6/


$ vi docker-compose.yml


version: '3'
services:
  mysql-dev:
    restart: always
    image: mysql
    ports:
      - '3306:3306'
    volumes:
      - ./mysql:/etc/mysql/conf.d
    environment:
      MYSQL_DATABASE: opd_data
      MYSQL_ROOT_PASSWORD: pA55w0rd1


$ docker-compose up


$ telnet localhost 3306


$ sudo apt update -y
$ sudo apt install -y mysql-client


$ mysql --user=root --password=pA55w0rd1 -h 127.0.0.1 opd_data


mysql> SELECT DATABASE();


$ cd ~/tmp/Designing-Microservices-Platforms-with-NATS/chapter6/resources/


// Добавляем данные в базу
$ mysql --user=root --password=pA55w0rd1 -h 127.0.0.1 opd_data < ./mysql.sql


$ mysql --user=root --password=pA55w0rd1 -h 127.0.0.1 opd_data


mysql> show tables;
+-----------------------+
| Tables_in_opd_data    |
+-----------------------+
| discharge_details     |
| inspection_details    |
| inspection_reports    |
| medication_reports    |
| patient_details       |
| patient_registrations |
| release_reports       |
| test_reports          |
+-----------------------+


Registration service

$ cd ~/tmp/Designing-Microservices-Platforms-with-NATS/chapter6/registration-service/


$ go run cmd/main.go -dbName opd_data -dbUser root -dbPassword pA55w0rd1

output:

***
2021/12/25 19:37:34 Listening for HTTP requests on 0.0.0.0:9090


$ curl \
    --data '{"full_name":"chanaka fernando","address":"44, sw19, london","id":200, "sex":"male", "phone":222222222}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9090/opd/patient/register" \
    | jq

output:

{
  "id": 200,
  "token": 1
}


Inspection service

$ cd ~/tmp/Designing-Microservices-Platforms-with-NATS/chapter6/inspection-service/


$ go run cmd/main.go -dbName opd_data -dbUser root -dbPassword pA55w0rd1

output:

***
2021/12/25 19:44:20 Listening for HTTP requests on 0.0.0.0:9091


$ curl \
    --data '{"id":200, "time":"2021/07/12 10:21 AM", "observations":"ABC Syncrome", "medication":"XYZ x 3", "tests":"FBT, UC", "notes":"possible Covid-19"}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9091/opd/inspection/record" \
    | jq

output:

{
  "id": 200,
  "medication": "XYZ x 3",
  "tests": "FBT, UC",
  "notes": "possible Covid-19"
}


Treatment service

$ cd ~/tmp/Designing-Microservices-Platforms-with-NATS/chapter6/treatment-service


$ go run cmd/main.go -dbName opd_data -dbUser root -dbPassword pA55w0rd1

output:

***
2021/12/25 19:48:09 Listening for HTTP requests on 0.0.0.0:9092


$ curl \
    --data '{"id":200,"time":"2021 07 12 4:35PM","dose":"xyz x 1, abc x 2","notes":"low fever"}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9092/opd/treatment/medication" \
    | jq

output:

"Record updated successfully"


Release service

$ cd ~/tmp/Designing-Microservices-Platforms-with-NATS/chapter6/release-service


$ go run cmd/main.go -dbName opd_data -dbUser root -dbPassword pA55w0rd1

output:

***
2021/12/25 19:49:49 Listening for HTTP requests on 0.0.0.0:9093


$ curl \
    --data '{"id":200,"time":"2021 07 12 9:35 PM","state":"discharge","post_medication":"NM x 10 days","next_visit":"2021 08 12 09:00AM"}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9093/opd/release/discharge" \
    | jq

output:

"Patient discharge recorded successfully"


Testing the OPD application


$ curl \
    --data '{"full_name":"John Doe","address":"44, sw19, london","id":200, "sex":"male", "phone":222222222}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9090/opd/patient/register" \
    | jq


$ curl "http://localhost:9091/opd/inspection/pending"  \
    | jq


[
  {
    "id": 200,
    "token": 2
  }
]


$ curl \
    --data '{"id":200, "time":"2021/07/12 10:21 AM", "observations":"ABC Syncrome", "medication":"XYZ x 3", "tests":"FBT, UC", "notes":"possible Covid-19"}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9091/opd/inspection/record" \
    | jq


{
  "id": 200,
  "medication": "XYZ x 3",
  "tests": "FBT, UC",
  "notes": "possible Covid-19"
}


$ curl "http://localhost:9092/opd/treatment/pending" \
 | jq


[
  {
    "id": 200,
    "medication": "XYZ x 3",
    "tests": "FBT, UC",
    "notes": "possible Covid-19"
  }
]


$ curl \
    --data '{"id":200,"time":"2021 07 12 4:35 PM","dose":"xyz x 1, abcx 2","notes":"low fever"}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9092/opd/treatment/medication" \
    | jq


"Record updated successfully"


$ curl \
    --data '{"id":200,"time":"2021 07 12 4:35 PM","test_name":"FBC","status":"sample collected", "notes":"possible covid-19"}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9092/opd/treatment/tests" \
    | jq


"Test recorded successfully"


"Record updated successfully"


$ curl \
    --data '{"id":200,"time":"2021 07 12 8:35 PM","next_state":"discharge","post_medication":"NM x 10 days"}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9092/opd/treatment/release" \
    | jq


"Release event published"


$ curl "http://localhost:9093/opd/release/pending" \
 | jq


[
  {
    "id": 200,
    "time": "2021 07 12 8:35 PM",
    "next_state": "discharge",
    "post_medication": "NM x 10 days"
  }
]


$ curl \
    --data '{"id":200,"time":"2021 07 12 9:35 PM","state":"discharge","post_medication":"NM x 10 days","next_visit":"2021 08 12 09:00AM"}' \
    --header "Content-Type: application/json" \
    --request POST \
    --url "http://localhost:9093/opd/release/discharge" \
    | jq


"Patient discharge recorded successfully"