Skip to content

Commit 3bdadac

Browse files
authored
docs: added Service discovery docs (#2)
1 parent 02e7514 commit 3bdadac

File tree

8 files changed

+139
-22
lines changed

8 files changed

+139
-22
lines changed

docs/structure.md renamed to docs/contributing/structure.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,3 @@ This view is usually used by Kubernetes, Eureka and other systems to check if ou
1515

1616
### pyms/logger
1717
Print logger in JSON format to send to server like Elasticsearch. Inject span traces in logger.
18-
19-
### pyms/tracer
20-
Create an injector `flask_opentracing.FlaskTracer` to use in our projects.

docs/imgs/consul_example.png

40.5 KB
Loading

docs/overrides/home.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
<div class="tx-hero__content">
1212
<br><br>
1313
<h1>Python Microservices for the real world </h1>
14-
<p>When we started to create a microservice with no previous knowledge, we started looking for tutorials, guides, best practices, but we found
15-
nothing to create professional projects. Most articles say: </p>
14+
<p>Without previous knowledge, we started to create a microservice. We started looking for tutorials,
15+
guides and best practices, but we found nothing to create professional projects. Most articles say: </p>
1616
<ul>
1717
<li>"Install flask"</li>
1818
<li>"Create routes"</li>
1919
<li>(Sometimes) "Create a swagger specs"</li>
2020
<li>"TA-DA! you have a microservice"</li>
2121
</ul>
22-
<p>But... what happens when we want our configuration out of the code, such as a Kubernetes configmap? what happens with transactionality?
22+
<p>But... what happens when we want to use our configuration out of the code, such as a Kubernetes configmap? what happens with transactionality?
2323
If we have many microservices, what happens with traces?.</p>
2424

25-
<p>There are many problems around Python and microservices and we couldn't find anyone to give us a solution.</p>
25+
<p>There are many problems around Python and microservices and we couldn't find anyone to give us any solutions.</p>
2626

27-
<p>We start creating these projects to try to solve all the problems we have found in our professional lives about
28-
microservices architecture.</p>
27+
<p>We start creating these projects to try to solve all the problems we have found in our careers about
28+
microservices architecture.</p>
2929

3030
<p>Nowadays, is not perfect and we have a looong roadmap, but we hope this library could help other felas and friends ;)</p>
3131
<br>
File renamed without changes.

docs/services.md renamed to docs/services/services.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Services
22

33
Services are libraries, resources and extensions added to the Microservice in the configuration file.
4-
These services are created as an attribute of the [Microservice class](ms_class.md) to use in the code.
4+
These services are created as an attribute of the [Microservice class](../ms_class.md) to use in the code.
55

6-
To add a service check the [configuration section](configuration.md).
6+
To add a service check the [configuration section](../configuration.md).
77

88
You can declare a service but **activate/deactivate** with the keyword `enabled`, like so:
99

@@ -84,6 +84,8 @@ pyms:
8484
Add traces to all executions with [opentracing](https://github.com/opentracing-contrib/python-flask). This service
8585
solves the problem of [distributed tracing](https://microservices.io/patterns/observability/distributed-tracing.html)
8686

87+
See [Propagate traces tutorial](/tutorials/tutorial_propagate_traces) in this documentation for more information.
88+
8789
### Installation
8890

8991
You must install `pyms` with `pip install pyms[all]` or `pip install pyms[trace]`
@@ -111,7 +113,7 @@ pyms:
111113
Adds [Prometheus](https://prometheus.io/) metrics using the [Prometheus Client
112114
Library](https://github.com/prometheus/client_python).
113115

114-
The folowing metrics are currently available:
116+
The following metrics are currently available:
115117

116118
- Incoming requests latency as a histogram
117119
- Incoming requests number as a counter, divided by HTTP method, endpoint and
@@ -128,12 +130,37 @@ You must install `pyms` with `pip install py-ms[all]` or `pip install py-ms[metr
128130
```yaml
129131
pyms:
130132
services:
131-
metrics: true
133+
metrics: true
134+
```
135+
136+
This will add the endpoint `/metrics` to your microservice, which will expose
137+
the metrics.
138+
139+
## Service discovery
140+
Adds [Consul](https://www.consul.io/) as service discovery using the [Consul Client
141+
Library](https://github.com/python-microservices/consulate) or create your own service discovery
142+
143+
See [Service Ddiscovery section](services_discovery.md) in this documentation for more information.
144+
145+
### Installation
146+
147+
You must install `pyms` with `pip install py-ms[all]` or `pip install py-ms[consul]`
148+
149+
### Example
150+
151+
```yaml
152+
pyms:
153+
services:
154+
service_discovery:
155+
service: consul
156+
host: localhost
157+
port: 8500
158+
autoregister: true
132159
```
133160

134161
This will add the endpoint `/metrics` to your microservice, which will expose
135162
the metrics.
136163

137164
## How to contrib:
138165

139-
[See tutorial](tutorial_create_services.md)
166+
[See tutorial](/tutorials/tutorial_create_services)

docs/services/services_discovery.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Service Discovery
2+
3+
PyMS support at this moment this service discovery
4+
5+
- Consul
6+
- Eureka (TODO)
7+
- Kubernetes (TODO)
8+
9+
10+
## Consul
11+
This service use [Consul Client
12+
Library](https://github.com/python-microservices/consulate) to connect to [Consul](https://www.consul.io/)
13+
14+
```yaml
15+
pyms:
16+
services:
17+
service_discovery:
18+
service: "consul"
19+
host: "localhost"
20+
autoregister: true
21+
config:
22+
DEBUG: true
23+
TESTING: false
24+
APP_NAME: "Python Microservice"
25+
APPLICATION_ROOT: ""
26+
```
27+
28+
Check your local configuration with Docker. Run:
29+
30+
```shell
31+
docker run -d --net=host -e 'CONSUL_LOCAL_CONFIG={"leave_on_terminate": true, "enable_script_checks": true}' consul
32+
```
33+
34+
Open `http://localhost:8500/` to check if Consul is up. If it's ok, when you run your microservice with
35+
`autoregister: true` parameter, you will see in Consul your microservice like this image:
36+
37+
![Consul](/imgs/consul_example.png)
38+
39+
## Consulate client
40+
41+
You can use all [consulate](https://github.com/python-microservices/consulate) options with
42+
`ms.service_discovery.client` or `current_app.ms.service_discovery.client`. In example:
43+
44+
```python
45+
ms.service_discovery.client.agent.checks()
46+
```
47+
48+
See [official docs](https://consulate.readthedocs.io/) for more information.
49+
50+
## Create your own Service Discovery
51+
52+
Instead of define a service keyword, you can point to a class in your proyect that inherit from ServiceDiscoveryBase
53+
(see example above)
54+
55+
```yaml
56+
pyms:
57+
services:
58+
service_discovery:
59+
service: "myproject.servicesdiscovery_file.MySvcDiscovery"
60+
host: "localhost"
61+
autoregister: true
62+
config:
63+
DEBUG: true
64+
TESTING: false
65+
APP_NAME: "Python Microservice"
66+
APPLICATION_ROOT: ""
67+
```
68+
69+
70+
Under `myproject/servicesdiscovery_file.py`:
71+
72+
```python
73+
from pyms.flask.services.service_discovery import ServiceDiscoveryBase
74+
75+
76+
class ServiceDiscoveryConsulBasic(ServiceDiscoveryBase):
77+
def register_service(self, id_app, host, port, healtcheck_url, app_name):
78+
headers = {"Content-Type": "application/json; charset=utf-8"}
79+
data = {
80+
"id": app_name + "-" + id_app,
81+
"name": app_name,
82+
"check": {"name": "ping check", "http": healtcheck_url, "interval": "30s", "status": "passing"},
83+
}
84+
response = requests.put(
85+
"http://{host}:{port}/v1/agent/service/register".format(host=host, port=port),
86+
data=json.dumps(data),
87+
headers=headers,
88+
)
89+
if response.status_code != 200:
90+
raise Exception(response.content)
91+
```

docs/tutorials/tutorial_propagate_traces.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Each service handles a request by performing one or more operations, e.g. databa
77

88
PyMS injects a unique request ID with [opentracing](https://github.com/opentracing-contrib/python-flask) and
99
passes the external request id to all services that are involved in handling the current request with the
10-
[service request](services.md)
10+
[service request](/services/services)
1111

1212
## 1. Simple Trace
1313

@@ -133,7 +133,7 @@ curl 'http://localhost:5001/'
133133
}
134134
```
135135

136-
![Terminal](imgs/multiple-ms.png)
136+
![Terminal](/imgs/multiple-ms.png)
137137

138138
The second MS will print these logs:
139139

@@ -184,7 +184,7 @@ As you can see, both microservices have `"trace": "bb785b88d0456d69"`.
184184
185185
You can see the flow of these requests in this diagram:
186186
187-
![Distributed tracing](imgs/PyMSdistributedtracing.png)
187+
![Distributed tracing](/imgs/PyMSdistributedtracing.png)
188188
189189
## Code
190190

mkdocs.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ nav:
1111
- Installation: installation.md
1212
- Quickstart: quickstart.md
1313
- Configuration: configuration.md
14-
- Services: services.md
1514
- Microservice class: ms_class.md
16-
- Routing: routing.md
15+
- Services:
16+
- List of services: services/services.md
17+
- Routing: services/routing.md
18+
- Service Discovery: services/services_discovery.md
1719
- Encrypt/Decrypt Configuration: encrypt_decryt_configuration.md
18-
- PyMS structure: structure.md
1920
- Command line: command_line.md
2021
- Introduction to microservices:
2122
- Introduction to microservices (ENG): microservices.md
@@ -35,11 +36,12 @@ nav:
3536
- Run in Kubernetes: scaffold/runinkubernetes.md
3637
- Template:
3738
- Index: template/index.md
39+
- Contributing:
40+
- Index: contributing/index.md
41+
- PyMS structure: contributing/structure.md
3842
- Articles and events:
3943
- External Links and Articles: links_articles.md
4044
- HacktoberfestES: hacktoberfest_es_2020.md
41-
- Contributing:
42-
- Index: contributing/index.md
4345

4446
markdown_extensions:
4547
- admonition: null

0 commit comments

Comments
 (0)