mirror of
https://github.com/Bryanthelol/namekoplus
synced 2025-09-13 20:26:02 +08:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
0777788609 | |||
dd23c91192 | |||
cb5762720f | |||
920e204756 | |||
9ac4a39c0d | |||
37bdf56862 | |||
c76238125f | |||
8cf674f0df | |||
08e818e1e1 | |||
7ba162971e |
39
.github/workflows/python-publish.yml
vendored
Normal file
39
.github/workflows/python-publish.yml
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# This workflow will upload a Python Package using Twine when a release is created
|
||||||
|
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
|
||||||
|
|
||||||
|
# This workflow uses actions that are not certified by GitHub.
|
||||||
|
# They are provided by a third-party and are governed by
|
||||||
|
# separate terms of service, privacy policy, and support
|
||||||
|
# documentation.
|
||||||
|
|
||||||
|
name: Upload Python Package
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [published]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v3
|
||||||
|
with:
|
||||||
|
python-version: '3.x'
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install build
|
||||||
|
- name: Build package
|
||||||
|
run: python -m build
|
||||||
|
- name: Publish package
|
||||||
|
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
|
||||||
|
with:
|
||||||
|
user: ${{ secrets.PYPI_API_USER }}
|
||||||
|
password: ${{ secrets.PYPI_API_PASSWORD }}
|
@@ -1,3 +1,5 @@
|
|||||||
include *.py
|
include *.py *.md *.mako
|
||||||
recursive-include namekoplus/chassis *.py
|
recursive-include namekoplus/chassis *.py *.mako
|
||||||
recursive-include namekoplus/templates *.py *.yml
|
recursive-include namekoplus/templates *.py *.yml *.mako
|
||||||
|
recursive-include namekoplus/chassis-agent *.py *.yml *.mako
|
||||||
|
recursive-include namekoplus/tests *.py *.mako
|
19
README.md
19
README.md
@@ -1,10 +1,7 @@
|
|||||||
# namekoplus
|
# namekoplus
|
||||||
|
|
||||||
A lightweight Python distributed microservice solution
|
A lightweight Python distributed microservice solution
|
||||||
|
|
||||||
## Document
|
|
||||||
|
|
||||||
[中文文档](https://doc.bearcatlog.com/)
|
|
||||||
|
|
||||||
## Command Line Tool Usage
|
## Command Line Tool Usage
|
||||||
|
|
||||||
### Checkout Command
|
### Checkout Command
|
||||||
@@ -13,8 +10,22 @@ A lightweight Python distributed microservice solution
|
|||||||
namekoplus --help
|
namekoplus --help
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Start a middleware that nameko depends on
|
||||||
|
|
||||||
|
```shell
|
||||||
|
namekoplus start -m rabbitmq
|
||||||
|
```
|
||||||
|
|
||||||
### Initialize a nameko service from templates
|
### Initialize a nameko service from templates
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
namekoplus init --directory <dir_name> --type <template_type>
|
namekoplus init --directory <dir_name> --type <template_type>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Detailed Usage
|
||||||
|
|
||||||
|
See Documents:
|
||||||
|
|
||||||
|
- [中文](https://doc.bearcatlog.com/)
|
||||||
|
- [English](https://legendary-sopapillas-e2626d.netlify.app/)
|
@@ -0,0 +1,25 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
rabbitmq:
|
||||||
|
image: rabbitmq:3-management
|
||||||
|
container_name: 'rabbitmq'
|
||||||
|
hostname: 'rabbitmq'
|
||||||
|
ports:
|
||||||
|
- "5672:5672"
|
||||||
|
- "15672:15672"
|
||||||
|
- "25672:25672"
|
||||||
|
volumes:
|
||||||
|
- rabbitmq_data:/var/lib/rabbitmq
|
||||||
|
- rabbitmq_log:/var/log/rabbitmq
|
||||||
|
environment:
|
||||||
|
RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER:-admin}
|
||||||
|
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS:-admin}
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
rabbitmq_data:
|
||||||
|
driver: local
|
||||||
|
rabbitmq_log:
|
||||||
|
driver: local
|
@@ -3,10 +3,32 @@ import shutil
|
|||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
from python_on_whales import DockerException, ClientNotFoundError, DockerClient, docker as docker_testing
|
||||||
|
|
||||||
|
|
||||||
|
def check_docker():
|
||||||
|
"""
|
||||||
|
Check if docker and docker compose are installed and running.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
docker_testing.ps()
|
||||||
|
except ClientNotFoundError:
|
||||||
|
click.echo('Please install docker first', err=True)
|
||||||
|
raise
|
||||||
|
except DockerException:
|
||||||
|
click.echo('Please start docker correctly', err=True)
|
||||||
|
raise
|
||||||
|
|
||||||
|
if not docker_testing.compose.is_installed():
|
||||||
|
click.echo('Please install docker-compose first', err=True)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def status(status_msg: str, newline: bool = False, quiet: bool = False):
|
def status(status_msg: str, newline: bool = False, quiet: bool = False):
|
||||||
|
"""
|
||||||
|
Show status message and yield.
|
||||||
|
"""
|
||||||
msg_suffix = ' ...' if not newline else ' ...\n'
|
msg_suffix = ' ...' if not newline else ' ...\n'
|
||||||
click.echo(status_msg + msg_suffix)
|
click.echo(status_msg + msg_suffix)
|
||||||
try:
|
try:
|
||||||
@@ -20,15 +42,25 @@ def status(status_msg: str, newline: bool = False, quiet: bool = False):
|
|||||||
click.echo(' Done\n')
|
click.echo(' Done\n')
|
||||||
|
|
||||||
|
|
||||||
def get_template_directory() -> str:
|
def get_directory(dir_name: str) -> str:
|
||||||
"""Return the directory where nameko_plus setup templates are found.
|
"""
|
||||||
|
Return the directory path of the given nameko-plus directory name.
|
||||||
This method is used by the nameko_plus ``init`` commands.
|
|
||||||
"""
|
"""
|
||||||
import namekoplus
|
import namekoplus
|
||||||
|
|
||||||
package_dir = os.path.abspath(os.path.dirname(namekoplus.__file__))
|
package_dir = os.path.abspath(os.path.dirname(namekoplus.__file__))
|
||||||
return os.path.join(package_dir, 'templates')
|
return os.path.join(package_dir, dir_name)
|
||||||
|
|
||||||
|
|
||||||
|
def copy_files(src_dir, dest_dir):
|
||||||
|
for file_ in os.listdir(src_dir):
|
||||||
|
if file_ == '__pycache__':
|
||||||
|
continue
|
||||||
|
|
||||||
|
src_file_path = os.path.join(src_dir, file_)
|
||||||
|
output_file = os.path.join(dest_dir, file_)
|
||||||
|
with status(f'Generating {os.path.abspath(output_file)}'):
|
||||||
|
shutil.copy(src_file_path, output_file)
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
@@ -40,10 +72,10 @@ def cli():
|
|||||||
@click.option('-d', '--directory',
|
@click.option('-d', '--directory',
|
||||||
required=True,
|
required=True,
|
||||||
help='The directory name of nameko services')
|
help='The directory name of nameko services')
|
||||||
@click.option('-f', '--type', '_type',
|
@click.option('-t', '--type', '_type',
|
||||||
default='rpc',
|
default='all',
|
||||||
show_default=True,
|
show_default=True,
|
||||||
type=click.Choice(['rpc', 'event', 'http', 'timer'], case_sensitive=False),
|
type=click.Choice(['all', 'rpc', 'event', 'http', 'timer', 'demo'], case_sensitive=False),
|
||||||
help='The template type of nameko service')
|
help='The template type of nameko service')
|
||||||
def init(directory, _type):
|
def init(directory, _type):
|
||||||
"""
|
"""
|
||||||
@@ -53,32 +85,89 @@ def init(directory, _type):
|
|||||||
click.echo('Directory {} already exists and is not empty'.format(directory), err=True)
|
click.echo('Directory {} already exists and is not empty'.format(directory), err=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
template_dir = os.path.join(get_template_directory(), _type)
|
template_dir = os.path.join(get_directory('templates'), _type)
|
||||||
if not os.access(template_dir, os.F_OK):
|
if not os.access(template_dir, os.F_OK):
|
||||||
click.echo('No such template type {}'.format(_type), err=True)
|
click.echo('No such template type {}'.format(_type), err=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
# 创建目录
|
|
||||||
if not os.access(directory, os.F_OK):
|
if not os.access(directory, os.F_OK):
|
||||||
with status(f'Creating directory {os.path.abspath(directory)!r}'):
|
with status(f'Creating directory {os.path.abspath(directory)!r}'):
|
||||||
os.makedirs(directory)
|
os.makedirs(directory)
|
||||||
|
|
||||||
# 把 templates 放入新建的目录
|
copy_files(template_dir, directory)
|
||||||
for file_ in os.listdir(template_dir):
|
|
||||||
if file_ == '__pycache__':
|
|
||||||
continue
|
|
||||||
src_file_path = os.path.join(template_dir, file_)
|
|
||||||
output_file = os.path.join(directory, file_)
|
|
||||||
with status(f'Generating {os.path.abspath(output_file)}'):
|
|
||||||
shutil.copy(src_file_path, output_file)
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
def start():
|
@click.option('-m', '--middleware',
|
||||||
|
required=True,
|
||||||
|
type=click.Choice(['rabbitmq'], case_sensitive=False),
|
||||||
|
help='The middleware name')
|
||||||
|
@click.option('-u', '--user',
|
||||||
|
required=False,
|
||||||
|
help='The user name of the middleware')
|
||||||
|
@click.option('-p', '--password',
|
||||||
|
required=False,
|
||||||
|
help='The password of the middleware')
|
||||||
|
def start(middleware, user, password):
|
||||||
"""
|
"""
|
||||||
Start a middleware, such as RabbitMQ.
|
Start a middleware that the nameko service depends on.
|
||||||
"""
|
"""
|
||||||
click.echo('Initialized the database')
|
check_docker()
|
||||||
|
|
||||||
|
if user and password:
|
||||||
|
os.environ['RABBITMQ_DEFAULT_USER'] = user
|
||||||
|
os.environ['RABBITMQ_DEFAULT_PASS'] = password
|
||||||
|
|
||||||
|
docker_compose_file_dir = os.path.join(get_directory('chassis-agent'), middleware)
|
||||||
|
for file_ in os.listdir(docker_compose_file_dir):
|
||||||
|
compose_file_path = os.path.join(docker_compose_file_dir, file_)
|
||||||
|
with status(f'Starting {middleware}'):
|
||||||
|
docker = DockerClient(compose_files=[compose_file_path])
|
||||||
|
docker.compose.up(detach=True)
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.option('-m', '--middleware',
|
||||||
|
required=True,
|
||||||
|
type=click.Choice(['rabbitmq'], case_sensitive=False),
|
||||||
|
help='The middleware name')
|
||||||
|
def stop(middleware):
|
||||||
|
"""
|
||||||
|
Stop a middleware that the nameko service depends on.
|
||||||
|
"""
|
||||||
|
check_docker()
|
||||||
|
|
||||||
|
docker_compose_file_dir = os.path.join(get_directory('chassis-agent'), middleware)
|
||||||
|
for file_ in os.listdir(docker_compose_file_dir):
|
||||||
|
compose_file_path = os.path.join(docker_compose_file_dir, file_)
|
||||||
|
with status(f'Stopping {middleware}'):
|
||||||
|
docker = DockerClient(compose_files=[compose_file_path])
|
||||||
|
docker.compose.down()
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.option('-e', '--existed_dir', 'directory',
|
||||||
|
required=True,
|
||||||
|
help='The existed directory name of the nameko service')
|
||||||
|
@click.option('-t', '--type', '_type',
|
||||||
|
default='unit',
|
||||||
|
show_default=True,
|
||||||
|
type=click.Choice(['unit'], case_sensitive=False),
|
||||||
|
help='The test type of the nameko service')
|
||||||
|
def test_gen(directory, _type):
|
||||||
|
"""
|
||||||
|
Generate test files for nameko services.
|
||||||
|
"""
|
||||||
|
if not os.access(directory, os.F_OK) or not os.listdir(directory):
|
||||||
|
click.echo('Directory {} dose not exist or is empty'.format(directory), err=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
tests_dir = os.path.join(get_directory('tests'), _type)
|
||||||
|
if not os.access(tests_dir, os.F_OK):
|
||||||
|
click.echo('No such test type {}'.format(_type), err=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
copy_files(tests_dir, directory)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
0
namekoplus/templates/all/__init__.py
Normal file
0
namekoplus/templates/all/__init__.py
Normal file
144
namekoplus/templates/all/all_demo.py
Normal file
144
namekoplus/templates/all/all_demo.py
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from nameko.events import EventDispatcher, event_handler
|
||||||
|
from nameko.rpc import rpc, ServiceRpc
|
||||||
|
from nameko.timer import timer
|
||||||
|
from nameko.web.handlers import http
|
||||||
|
from werkzeug.wrappers import Response
|
||||||
|
from nameko_tracer import Tracer
|
||||||
|
from namekoplus import init_statsd, init_sentry
|
||||||
|
|
||||||
|
|
||||||
|
class HttpDemoService:
|
||||||
|
|
||||||
|
name = "http_demo_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
|
sentry = init_sentry()
|
||||||
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
@http("GET", "/broken")
|
||||||
|
@statsd.timer('broken')
|
||||||
|
def broken(self, request):
|
||||||
|
raise ConnectionRefusedError()
|
||||||
|
|
||||||
|
@http('GET', '/books/<string:uuid>')
|
||||||
|
@statsd.timer('demo_get')
|
||||||
|
def demo_get(self, request, uuid):
|
||||||
|
data = {'id': uuid, 'title': 'The unbearable lightness of being',
|
||||||
|
'author': 'Milan Kundera'}
|
||||||
|
return Response(json.dumps({'book': data}),
|
||||||
|
mimetype='application/json')
|
||||||
|
|
||||||
|
@http('POST', '/books')
|
||||||
|
@statsd.timer('demo_post')
|
||||||
|
def demo_post(self, request):
|
||||||
|
return Response(json.dumps({'book': request.data.decode()}),
|
||||||
|
mimetype='application/json')
|
||||||
|
|
||||||
|
class RpcResponderDemoService:
|
||||||
|
|
||||||
|
name = "rpc_responder_demo_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
|
sentry = init_sentry()
|
||||||
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
@rpc
|
||||||
|
@statsd.timer('hello')
|
||||||
|
def hello(self, name):
|
||||||
|
return "Hello, {}!".format(name)
|
||||||
|
|
||||||
|
|
||||||
|
class RpcCallerDemoService:
|
||||||
|
|
||||||
|
name = "rpc_caller_demo_service"
|
||||||
|
|
||||||
|
remote = ServiceRpc("rpc_responder_demo_service")
|
||||||
|
|
||||||
|
sentry = init_sentry()
|
||||||
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
@rpc
|
||||||
|
@statsd.timer('remote_hello')
|
||||||
|
def remote_hello(self, value="John Doe"):
|
||||||
|
res = u"{}".format(value)
|
||||||
|
return self.remote.hello(res)
|
||||||
|
|
||||||
|
|
||||||
|
class EventPublisherService:
|
||||||
|
|
||||||
|
name = "publisher_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
|
sentry = init_sentry()
|
||||||
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
dispatch = EventDispatcher()
|
||||||
|
|
||||||
|
@rpc
|
||||||
|
@statsd.timer('publish')
|
||||||
|
def publish(self, event_type, payload):
|
||||||
|
self.dispatch(event_type, payload)
|
||||||
|
|
||||||
|
|
||||||
|
class AnEventListenerService:
|
||||||
|
|
||||||
|
name = "an_event_listener_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
|
sentry = init_sentry()
|
||||||
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
@event_handler("publisher_service", "an_event")
|
||||||
|
@statsd.timer('consume_an_event')
|
||||||
|
def consume_an_event(self, payload):
|
||||||
|
print("service {} received:".format(self.name), payload)
|
||||||
|
|
||||||
|
|
||||||
|
class AnotherEventListenerService:
|
||||||
|
|
||||||
|
name = "another_event_listener_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
|
sentry = init_sentry()
|
||||||
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
@event_handler("publisher_service", "another_event")
|
||||||
|
@statsd.timer('consume_another_event')
|
||||||
|
def consume_another_event(self, payload):
|
||||||
|
print("service {} received:".format(self.name), payload)
|
||||||
|
|
||||||
|
|
||||||
|
class ListenBothEventsService:
|
||||||
|
|
||||||
|
name = "listen_both_events_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
|
sentry = init_sentry()
|
||||||
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
@event_handler("publisher_service", "an_event")
|
||||||
|
@statsd.timer('consume_an_event')
|
||||||
|
def consume_an_event(self, payload):
|
||||||
|
print("service {} received:".format(self.name), payload)
|
||||||
|
|
||||||
|
@event_handler("publisher_service", "another_event")
|
||||||
|
@statsd.timer('consume_another_event')
|
||||||
|
def consume_another_event(self, payload):
|
||||||
|
print("service {} received:".format(self.name), payload)
|
||||||
|
|
||||||
|
|
||||||
|
class Timer:
|
||||||
|
|
||||||
|
name = 'timer'
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
|
sentry = init_sentry()
|
||||||
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
@timer(interval=1)
|
||||||
|
@statsd.timer('ping')
|
||||||
|
def ping(self):
|
||||||
|
# method executed every second
|
||||||
|
print("pong")
|
25
namekoplus/templates/all/config.yml
Normal file
25
namekoplus/templates/all/config.yml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
|
WEB_SERVER_ADDRESS: '0.0.0.0:8000'
|
||||||
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
|
max_workers: 10
|
||||||
|
parent_calls_tracked: 20
|
||||||
|
|
||||||
|
LOGGING:
|
||||||
|
version: 1
|
||||||
|
formatters:
|
||||||
|
tracer:
|
||||||
|
(): nameko_tracer.formatters.PrettyJSONFormatter
|
||||||
|
handlers:
|
||||||
|
tracer:
|
||||||
|
class: logging.StreamHandler
|
||||||
|
formatter: tracer
|
||||||
|
loggers:
|
||||||
|
nameko_tracer:
|
||||||
|
level: INFO
|
||||||
|
handlers: [tracer]
|
||||||
|
|
||||||
|
SENTRY:
|
||||||
|
DSN: ${SENTRY_DSN}
|
||||||
|
CLIENT_CONFIG:
|
||||||
|
site: ${SENTRY_SITE}
|
0
namekoplus/templates/demo/__init__.py
Normal file
0
namekoplus/templates/demo/__init__.py
Normal file
5
namekoplus/templates/demo/config.yml
Normal file
5
namekoplus/templates/demo/config.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
|
max_workers: 10
|
||||||
|
parent_calls_tracked: 20
|
20
namekoplus/templates/demo/rpc_demo.py
Normal file
20
namekoplus/templates/demo/rpc_demo.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from nameko.rpc import rpc, ServiceRpc
|
||||||
|
|
||||||
|
|
||||||
|
class RpcResponderDemoService:
|
||||||
|
name = "rpc_responder_demo_service"
|
||||||
|
|
||||||
|
@rpc
|
||||||
|
def hello(self, name):
|
||||||
|
return "Hello, {}!".format(name)
|
||||||
|
|
||||||
|
|
||||||
|
class RpcCallerDemoService:
|
||||||
|
name = "rpc_caller_demo_service"
|
||||||
|
|
||||||
|
remote = ServiceRpc("rpc_responder_demo_service")
|
||||||
|
|
||||||
|
@rpc
|
||||||
|
def remote_hello(self, value="John Doe"):
|
||||||
|
res = u"{}".format(value)
|
||||||
|
return self.remote.hello(res)
|
@@ -1,4 +1,4 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
@@ -5,14 +5,15 @@ from namekoplus import init_statsd, init_sentry
|
|||||||
|
|
||||||
|
|
||||||
class EventPublisherService:
|
class EventPublisherService:
|
||||||
name = "publisher_service"
|
|
||||||
|
|
||||||
dispatch = EventDispatcher()
|
name = "publisher_service"
|
||||||
|
|
||||||
tracer = Tracer()
|
tracer = Tracer()
|
||||||
sentry = init_sentry()
|
sentry = init_sentry()
|
||||||
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
dispatch = EventDispatcher()
|
||||||
|
|
||||||
@rpc
|
@rpc
|
||||||
@statsd.timer('publish')
|
@statsd.timer('publish')
|
||||||
def publish(self, event_type, payload):
|
def publish(self, event_type, payload):
|
||||||
@@ -20,8 +21,10 @@ class EventPublisherService:
|
|||||||
|
|
||||||
|
|
||||||
class AnEventListenerService:
|
class AnEventListenerService:
|
||||||
|
|
||||||
name = "an_event_listener_service"
|
name = "an_event_listener_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
sentry = init_sentry()
|
sentry = init_sentry()
|
||||||
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
@@ -32,8 +35,10 @@ class AnEventListenerService:
|
|||||||
|
|
||||||
|
|
||||||
class AnotherEventListenerService:
|
class AnotherEventListenerService:
|
||||||
|
|
||||||
name = "another_event_listener_service"
|
name = "another_event_listener_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
sentry = init_sentry()
|
sentry = init_sentry()
|
||||||
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
@@ -44,8 +49,10 @@ class AnotherEventListenerService:
|
|||||||
|
|
||||||
|
|
||||||
class ListenBothEventsService:
|
class ListenBothEventsService:
|
||||||
|
|
||||||
name = "listen_both_events_service"
|
name = "listen_both_events_service"
|
||||||
|
|
||||||
|
tracer = Tracer()
|
||||||
sentry = init_sentry()
|
sentry = init_sentry()
|
||||||
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
statsd = init_statsd('statsd_prefix', 'statsd_host', 'statsd_port')
|
||||||
|
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
|
WEB_SERVER_ADDRESS: '0.0.0.0:8000'
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
@@ -6,6 +6,7 @@ from namekoplus import init_statsd, init_sentry
|
|||||||
|
|
||||||
|
|
||||||
class HttpDemoService:
|
class HttpDemoService:
|
||||||
|
|
||||||
name = "http_demo_service"
|
name = "http_demo_service"
|
||||||
|
|
||||||
tracer = Tracer()
|
tracer = Tracer()
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
AMQP_URI: pyamqp://${RABBIT_USER:guest}:${RABBIT_PASSWORD:guest}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
|
||||||
RPC_EXCHANGE: 'nameko-rpc'
|
RPC_EXCHANGE: 'nameko-rpc'
|
||||||
|
|
||||||
max_workers: 10
|
max_workers: 10
|
||||||
|
@@ -3,8 +3,9 @@ from nameko_tracer import Tracer
|
|||||||
from namekoplus import init_statsd, init_sentry
|
from namekoplus import init_statsd, init_sentry
|
||||||
|
|
||||||
|
|
||||||
class Service:
|
class Timer:
|
||||||
name = "service"
|
|
||||||
|
name = 'timer'
|
||||||
|
|
||||||
tracer = Tracer()
|
tracer = Tracer()
|
||||||
sentry = init_sentry()
|
sentry = init_sentry()
|
||||||
|
0
namekoplus/tests/__init__.py
Normal file
0
namekoplus/tests/__init__.py
Normal file
0
namekoplus/tests/unit/__init__.py
Normal file
0
namekoplus/tests/unit/__init__.py
Normal file
29
namekoplus/tests/unit/test_service.py
Normal file
29
namekoplus/tests/unit/test_service.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"""
|
||||||
|
Service unit testing best practice.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from nameko.testing.services import worker_factory
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'value, expected',
|
||||||
|
[
|
||||||
|
('John Doe', 'Hello, John Doe!'),
|
||||||
|
('', 'Hello, !'),
|
||||||
|
('Bryant', 'Hello, Bryant!'),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_example_service(value, expected):
|
||||||
|
"""
|
||||||
|
Test example service.
|
||||||
|
"""
|
||||||
|
# create worker with mock dependencies
|
||||||
|
service = worker_factory(ServiceName) # TODO replace ServiceName with the name of the service and import it
|
||||||
|
|
||||||
|
# add side effects to the mock rpc dependency on the "remote" service
|
||||||
|
service.remote.hello.side_effect = lambda name: "Hello, {}!".format(name)
|
||||||
|
|
||||||
|
# test remote_hello business logic
|
||||||
|
assert service.remote_hello(value) == expected
|
||||||
|
service.remote.hello.assert_called_once_with(value)
|
39
setup.py
39
setup.py
@@ -9,11 +9,16 @@ with open(path.join(here, 'README.md'), encoding='utf-8') as f:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='namekoplus',
|
name='namekoplus',
|
||||||
version='0.1.0',
|
version='0.3.1',
|
||||||
description='A lightweight Python distributed microservice solution',
|
description='A lightweight Python distributed microservice solution',
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
url='https://github.com/Bryanthelol/namekoplus',
|
long_description_content_type='text/markdown',
|
||||||
|
url='',
|
||||||
|
project_urls={
|
||||||
|
'Documentation': 'https://doc.bearcatlog.com/',
|
||||||
|
'Source Code': 'https://github.com/Bryanthelol/namekoplus',
|
||||||
|
'Bug Tracker': 'https://github.com/Bryanthelol/namekoplus/issues',
|
||||||
|
},
|
||||||
author='Bryant He',
|
author='Bryant He',
|
||||||
author_email='bryantsisu@qq.com',
|
author_email='bryantsisu@qq.com',
|
||||||
|
|
||||||
@@ -28,12 +33,13 @@ setup(
|
|||||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||||
],
|
],
|
||||||
platforms='any',
|
platforms='any',
|
||||||
python_requires='>=3',
|
python_requires='>=3.8, <4',
|
||||||
|
|
||||||
keywords='lightweight python distributed microservice solution',
|
keywords='lightweight python distributed microservice solution',
|
||||||
|
|
||||||
packages=find_packages(exclude=['contrib', 'docs', 'tests']),
|
packages=find_packages(exclude=['contrib', 'docs', 'tests']),
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
|
data_files=['README.md'],
|
||||||
|
|
||||||
entry_points={
|
entry_points={
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
@@ -43,20 +49,19 @@ setup(
|
|||||||
|
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'nameko==3.0.0rc11',
|
'nameko==3.0.0rc11',
|
||||||
'nameko-sentry==1.0.0',
|
|
||||||
'nameko-tracer==1.4.0',
|
|
||||||
'click==8.1.5',
|
'click==8.1.5',
|
||||||
'pytest==7.4.0',
|
'python-on-whales==0.62.0',
|
||||||
'environs==9.5.0',
|
'pytest==7.4.0'
|
||||||
'logstash_formatter==0.5.17',
|
|
||||||
'statsd==4.0.1',
|
|
||||||
'tenacity==8.2.2',
|
|
||||||
'cachetools==5.3.0',
|
|
||||||
'circuitbreaker==2.0.0',
|
|
||||||
'shortuuid==1.0.11',
|
|
||||||
'cryptography'
|
|
||||||
],
|
],
|
||||||
extras_require={
|
extras_require={
|
||||||
|
'ha': ['tenacity==8.2.2',
|
||||||
|
'cachetools==5.3.0',
|
||||||
|
'circuitbreaker==2.0.0',
|
||||||
|
'statsd==4.0.1',
|
||||||
|
'logstash_formatter==0.5.17',
|
||||||
|
'nameko-sentry==1.0.0',
|
||||||
|
'nameko-tracer==1.4.0',
|
||||||
|
'shortuuid==1.0.11'],
|
||||||
'apiflask': ['apiflask>=1.3.1',
|
'apiflask': ['apiflask>=1.3.1',
|
||||||
'gevent>=22.10.2',
|
'gevent>=22.10.2',
|
||||||
'gunicorn==20.1.0'],
|
'gunicorn==20.1.0'],
|
||||||
@@ -66,6 +71,8 @@ setup(
|
|||||||
'sqlalchemy==2.0.15',
|
'sqlalchemy==2.0.15',
|
||||||
'sqlacodegen==2.3.0',
|
'sqlacodegen==2.3.0',
|
||||||
'alembic==1.11.1'],
|
'alembic==1.11.1'],
|
||||||
'dev': ['mako==1.2.4'],
|
'security': ['cryptography'],
|
||||||
|
'dev': ['mako==1.2.4',
|
||||||
|
'environs==9.5.0']
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user