Skip to content

Commit

Permalink
discovery service
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyi.song committed Sep 13, 2018
1 parent 910196c commit f6c71a4
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 13 deletions.
5 changes: 2 additions & 3 deletions examples/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
FROM python:3.6-alpine3.6

ARG service
ADD ${service}/. /service
ADD requirements.txt /service/
WORKDIR /service

Expand All @@ -10,4 +8,5 @@ RUN apk add --no-cache gcc libc-dev git make postgresql-dev \
&& pip install -i 'http://pypi.douban.com/simple' --trusted-host pypi.douban.com -r requirements.txt \
&&rm -rf /var/cache/apk/* /tmp/* /var/tmp/* $HOME/.cache

EXPOSE 8000
ARG service
ADD ${service}/. /service
11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ docker-compose logs
docker-compose logs -f server
```

## Cluster Service
```sh
./develop/cluster.sh
```

## Test

```
Expand Down Expand Up @@ -79,3 +84,9 @@ open http://localhost:9411
```
![image](https://github.com/songcser/sanic-ms/raw/master/examples/images/1514528423339.jpg)
![image](https://github.com/songcser/sanic-ms/raw/master/examples/images/1514528479787.jpg)

## Access Consul
```
open http://192.168.99.100:8500/ui/#/dc1/services
```
![image](https://github.com/songcser/sanic-ms/raw/master/examples/images/1514528479788.jpg)
Empty file modified examples/develop/cluster.sh
100644 → 100755
Empty file.
9 changes: 6 additions & 3 deletions examples/docker-compose-cluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ services:
ACCESS_CONTROL_ALLOW_HEADERS: "Content-Type"
ACCESS_CONTROL_ALLOW_METHODS: "POST, PUT, DELETE, GET"
ZIPKIN_SERVER: 'http://zipkin:9411/api/v1/spans'
CONSUL_HTTP_ADDR: 'consul:8500'
CONSUL_AGENT_HOST: 'consul'
CONSUL_AGENT_PORT: 8500
#command: python -m sanic server.app --host=0.0.0.0 --port=8000 --workers=4
depends_on:
- "db"
Expand Down Expand Up @@ -46,7 +47,8 @@ services:
ACCESS_CONTROL_ALLOW_HEADERS: "Content-Type"
ACCESS_CONTROL_ALLOW_METHODS: "POST, PUT, DELETE, GET"
ZIPKIN_SERVER: 'http://zipkin:9411/api/v1/spans'
CONSUL_HTTP_ADDR: 'consul:8500'
CONSUL_AGENT_HOST: 'consul'
CONSUL_AGENT_PORT: 8500
#command: python -m sanic server.app --host=0.0.0.0 --port=8000 --workers=4
depends_on:
- "db"
Expand Down Expand Up @@ -75,7 +77,8 @@ services:
ACCESS_CONTROL_ALLOW_HEADERS: "Content-Type"
ACCESS_CONTROL_ALLOW_METHODS: "POST, PUT, DELETE, GET"
ZIPKIN_SERVER: 'http://zipkin:9411/api/v1/spans'
CONSUL_HTTP_ADDR: 'consul:8500'
CONSUL_AGENT_HOST: 'consul'
CONSUL_AGENT_PORT: 8500
#command: python -m sanic server.app --host=0.0.0.0 --port=8000 --workers=4
depends_on:
- "db"
Expand Down
3 changes: 2 additions & 1 deletion examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ services:
ACCESS_CONTROL_ALLOW_HEADERS: "Content-Type"
ACCESS_CONTROL_ALLOW_METHODS: "POST, PUT, DELETE, GET"
ZIPKIN_SERVER: 'http://zipkin:9411/api/v1/spans'
CONSUL_HTTP_ADDR: 'consul:8500'
CONSUL_AGENT_HOST: 'consul'
CONSUL_AGENT_PORT: 8500
#command: python -m sanic server.app --host=0.0.0.0 --port=8000 --workers=4
depends_on:
- "db"
Expand Down
Binary file added examples/images/1514528479788.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions examples/region_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@
}

ZIPKIN_SERVER = os.environ.get('ZIPKIN_SERVER', None)
ACCESS_CONTROL_ALLOW_ORIGIN = os.environ.get("ACCESS_CONTROL_ALLOW_ORIGIN", "")
ACCESS_CONTROL_ALLOW_HEADERS = os.environ.get("ACCESS_CONTROL_ALLOW_HEADERS", "")
ACCESS_CONTROL_ALLOW_METHODS = os.environ.get("ACCESS_CONTROL_ALLOW_METHODS", "")

CONSUL_AGENT_HOST = os.environ.get('CONSUL_AGENT_HOST', '127.0.0.1')
CONSUL_AGENT_PORT = os.environ.get('CONSUL_AGENT_PORT', 8500)
6 changes: 6 additions & 0 deletions examples/role_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@
}

ZIPKIN_SERVER = os.environ.get('ZIPKIN_SERVER', None)
ACCESS_CONTROL_ALLOW_ORIGIN = os.environ.get("ACCESS_CONTROL_ALLOW_ORIGIN", "")
ACCESS_CONTROL_ALLOW_HEADERS = os.environ.get("ACCESS_CONTROL_ALLOW_HEADERS", "")
ACCESS_CONTROL_ALLOW_METHODS = os.environ.get("ACCESS_CONTROL_ALLOW_METHODS", "")

CONSUL_AGENT_HOST = os.environ.get('CONSUL_AGENT_HOST', '127.0.0.1')
CONSUL_AGENT_PORT = os.environ.get('CONSUL_AGENT_PORT', 8500)
3 changes: 3 additions & 0 deletions examples/user_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@
ACCESS_CONTROL_ALLOW_ORIGIN = os.environ.get("ACCESS_CONTROL_ALLOW_ORIGIN", "")
ACCESS_CONTROL_ALLOW_HEADERS = os.environ.get("ACCESS_CONTROL_ALLOW_HEADERS", "")
ACCESS_CONTROL_ALLOW_METHODS = os.environ.get("ACCESS_CONTROL_ALLOW_METHODS", "")

CONSUL_AGENT_HOST = os.environ.get('CONSUL_AGENT_HOST', '127.0.0.1')
CONSUL_AGENT_PORT = os.environ.get('CONSUL_AGENT_PORT', 8500)
12 changes: 6 additions & 6 deletions sanicms/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ async def before_srver_start(app, loop):
tracer.register_required_propagators()
opentracing.tracer = tracer
app.db = await ConnectionPool(loop=loop).init(app.config['DB_CONFIG'])
service_names = app.config['SERVICES']
service = ServiceManager(loop=loop, host=app.config['CONSUL_AGENT_HOST'])
services = await service.discovery_services()
app.services = defaultdict(list)
service = ServiceManager(loop=loop)
for name in service_names:
s = service.discovery_service(name)
app.services[s].extend(s)
for name in services[1].keys():
s = await service.discovery_service(name)
app.services[name].extend(s)


@app.listener('after_server_start')
async def after_server_start(app, loop):
service = ServiceManager(app.name, loop=loop)
service = ServiceManager(app.name, loop=loop, host=app.config['CONSUL_AGENT_HOST'])
await service.register_service(app.config['PORT'])
app.service = service

Expand Down
5 changes: 5 additions & 0 deletions sanicms/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ async def discovery_service(self, service_name):
service_tags=['ServiceTags']
))
return services

async def discovery_services(self):
catalog =self.consul.catalog
result = await catalog.services()
return result

0 comments on commit f6c71a4

Please sign in to comment.