|
| 1 | +# Microservices class |
| 2 | + |
| 3 | +The class Microservice is the core of all microservices built with PyMS. |
| 4 | + |
| 5 | + |
| 6 | +You can create a simple microservice such as: |
| 7 | + |
| 8 | +```python |
| 9 | +from flask import jsonify |
| 10 | + |
| 11 | +from pyms.flask.app import Microservice |
| 12 | + |
| 13 | +ms = Microservice(service="my-minimal-microservice", path=__file__) |
| 14 | +app = ms.create_app() |
| 15 | + |
| 16 | + |
| 17 | +@app.route("/") |
| 18 | +def example(): |
| 19 | + return jsonify({"main": "hello world"}) |
| 20 | + |
| 21 | + |
| 22 | +if __name__ == '__main__': |
| 23 | + app.run() |
| 24 | +``` |
| 25 | + |
| 26 | +And a config file like this config.yml |
| 27 | + |
| 28 | +```yaml |
| 29 | +my-minimal-microservice: |
| 30 | + APP_NAME: "Python Microservice" |
| 31 | +``` |
| 32 | +Check [Configuration](configuration.md) section to know how to create a configuration file. |
| 33 | +
|
| 34 | +Each keyword in our configuration block, can be accessed in our Microservice object through the attribute `config`. |
| 35 | + |
| 36 | +```yaml |
| 37 | +# Config.yml |
| 38 | +example-config: |
| 39 | + APP_NAME: "Python Microservice" |
| 40 | + foo: "var" |
| 41 | + multiplevars: |
| 42 | + config1: "test1" |
| 43 | + config2: "test2" |
| 44 | + |
| 45 | +``` |
| 46 | +```python |
| 47 | +#app.py |
| 48 | +from pyms.flask.app import Microservice |
| 49 | +
|
| 50 | +ms = Microservice(service="example-config", path=__file__) |
| 51 | +print(ms.config.APP_NAME) |
| 52 | +# >> "Python Microservice" |
| 53 | +print(ms.config.foo) |
| 54 | +# >> "bar" |
| 55 | +print(ms.config.multiplevars.config1) |
| 56 | +# >> "test1" |
| 57 | +print(ms.config.multiplevars.config2) |
| 58 | +# >> "test2" |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +# Looking for Configuration file |
| 64 | +By default, Microservice class search a config.yml in the same path. You can set a different route or set a json file. |
| 65 | +To change this path, define a environment variable `CONFIGMAP_FILE`. |
| 66 | + |
| 67 | +This way of looking for the configuration is useful when you work with Docker and Kubernetes. For example, you can integrate |
| 68 | +a configmap of Kubernetes, with this microservice and a deployment with: |
| 69 | + |
| 70 | +```yaml |
| 71 | +apiVersion: extensions/v1beta1 |
| 72 | +kind: Deployment |
| 73 | +metadata: |
| 74 | + name: my-microservice |
| 75 | +spec: |
| 76 | + replicas: 1 |
| 77 | + template: |
| 78 | + spec: |
| 79 | + containers: |
| 80 | + - name: my-microservice |
| 81 | + image: ... |
| 82 | + env: |
| 83 | + - name: CONFIGMAP_FILE |
| 84 | + value: "/usr/share/microservice/config.yaml" |
| 85 | +
|
| 86 | + volumeMounts: |
| 87 | + - mountPath: /usr/share/microservice |
| 88 | + name: ms-config-volume |
| 89 | + volumes: |
| 90 | + - name: ms-config-volume |
| 91 | + configMap: |
| 92 | + name: my-microservice-configmap |
| 93 | +``` |
| 94 | + |
| 95 | +Check more examples in [this Github page](https://github.com/python-microservices/pyms/tree/master/examples) |
0 commit comments