-
Notifications
You must be signed in to change notification settings - Fork 45
Support registration in Consul and created a driver to connect other Services Discovery #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
a008c28
feat: added basic consul register
avara1986 3088274
refactor!: init actions in services, not in create_app
avara1986 1f9b19d
fix: linters errors
avara1986 90c33bd
fix: removed assert to testing
avara1986 9012f38
fix: linters errors
avara1986 60d4227
feature: addec consulate fork
avara1986 2ff6c1a
fix: session reload out of scope
avara1986 bc1a543
feat: added Black and fix pylint errors of consul
avara1986 2d3fcd7
fix: removed python2 checks
avara1986 2b38505
fix: merge conflict, removed
avara1986 2d214e8
Merge remote-tracking branch 'origin/master' into feature/issue_190
avara1986 5f12cd2
feat: re-added pipfile lock
avara1986 7ca0d40
fix: flake8 errors
avara1986 0d9bd09
fix: bandit errors
avara1986 2fd7b63
feat: moved tests from consulate to pyms
avara1986 c96aed1
chore: increment coverage tests
avara1986 738dec3
fix: removed mock
avara1986 bbc1cde
fix: wait until docker is up
avara1986 fd89113
fix: assert raise depends of consul version
avara1986 fa17529
fix: tests, isort and linters
avara1986 6b06140
fix: bandit
avara1986 f947c45
fix: bandit
avara1986 d634a5b
fix: lint...
avara1986 00166e7
feat: moved consulate to py-ms-consulate package
avara1986 acbbf36
fix: removed unused code
avara1986 91c8e8f
docs: updated docstrings
avara1986 021b261
fix: updated example, added init in ServiceDiscoveryBase
avara1986 5ebfc24
tests: updated service discovery
avara1986 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| pyms: | ||
| services: | ||
| service_discovery: | ||
| client: "consul" | ||
| host: "http://localhost:8500" | ||
| config: | ||
| DEBUG: true | ||
| TESTING: false | ||
| APP_NAME: "Python Microservice" | ||
| APPLICATION_ROOT: "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from flask import jsonify | ||
|
|
||
| from pyms.flask.app import Microservice | ||
|
|
||
| ms = Microservice(path=__file__) | ||
| app = ms.create_app() | ||
|
|
||
|
|
||
| @app.route("/") | ||
| def example(): | ||
| return jsonify({"main": "hello world"}) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| app.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import json | ||
| import logging | ||
| import uuid | ||
|
|
||
| import requests | ||
| from requests.exceptions import RequestException | ||
| from pyms.constants import LOGGER_NAME | ||
| from pyms.exceptions import ServiceDiscoveryConnectionException | ||
| from pyms.flask.services.driver import DriverService | ||
|
|
||
| logger = logging.getLogger(LOGGER_NAME) | ||
|
|
||
| CONSUL_SERVICE_DISCOVERY = "consul" | ||
|
|
||
| DEFAULT_SERVICE_DISCOVERY = CONSUL_SERVICE_DISCOVERY | ||
|
|
||
|
|
||
| class ServiceDiscoveryBase: | ||
| def register_service(self, id_app, host, port, healtcheck_url, app_name): | ||
| pass | ||
|
|
||
|
|
||
| class ServiceDiscoveryConsul(ServiceDiscoveryBase): | ||
avara1986 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def register_service(self, id_app, host, port, healtcheck_url, app_name): | ||
| headers = { | ||
| 'Content-Type': 'application/json; charset=utf-8' | ||
| } | ||
| data = { | ||
| "id": app_name + "-" + id_app, | ||
| "name": app_name, | ||
| "port": port, | ||
| "check": { | ||
| "name": "ping check", | ||
| "http": healtcheck_url, | ||
| "interval": "30s", | ||
| "status": "passing" | ||
| } | ||
| } | ||
| error = False | ||
| msg_error = "Failed to establish a new connection" | ||
| try: | ||
| response = requests.put("{host}/v1/agent/service/register".format(host=host), data=json.dumps(data), | ||
| headers=headers) | ||
| if response.status_code != 200: | ||
| msg_error = response.content | ||
| error = True | ||
| except RequestException: | ||
| error = True | ||
|
|
||
| if error: | ||
| raise ServiceDiscoveryConnectionException("Host %s raise an error: %s" % (host, msg_error)) | ||
|
|
||
|
|
||
| class Service(DriverService): | ||
| id_app = str(uuid.uuid1()) | ||
avara1986 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| config_resource = "service_discovery" | ||
| default_values = { | ||
| "client": DEFAULT_SERVICE_DISCOVERY, | ||
| "host": "http://localhost:8500", | ||
| "port": 5000, | ||
| "healtcheck_url": "http://127.0.0.1.nip.io:5000/healthcheck", | ||
| } | ||
|
|
||
| def init_action(self, microservice_instance): | ||
| self.client.register_service( | ||
avara1986 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| id_app=self.id_app, | ||
| host=self.host, | ||
| healtcheck_url=self.healtcheck_url, | ||
| port=self.port, | ||
| app_name=microservice_instance.application.config["APP_NAME"]) | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.client = self.get_client() | ||
|
|
||
| def get_client(self) -> ServiceDiscoveryBase: | ||
| client = False | ||
| if self.config.client == CONSUL_SERVICE_DISCOVERY: | ||
| client = ServiceDiscoveryConsul() | ||
avara1986 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| logger.debug("Init %s as service discovery", client) | ||
| return client | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.