12/10/2023 0 Comments Angular 4 flask blueprint issueIf dont’ want the development server we also can start our API using gunicornĭocker run -p 5000:5000 -env REDIS_HOST=192.168.1.100 front gunicorn -w 1 app:app -b 0.0.0.0:5000Īnd that works. We can pass to our our Docker container the real host where our Redis resides (suposing my IP address is 192.168.1.100):ĭocker run -p 5000:5000 -env REDIS_HOST=192.168.1.100 front python app.py Our script the Redis host is localhost by default but it can be passed from an environment variable, Now our API’s localhost isn’t the same than our host’s one. It worked before, when our application and our Redis were within the same host. That’s because our Docker container is trying to use one Redis host within localhost. If we open now our browser with the url we’ll get a 500 error. First of of all we need to create one Docker image from a docker file:ĭocker run -p 5000:5000 front python app.py Now we’re going to run our application within a Docker container. We open our browser with the url: and it works. To be able to run our application we must ensure that we’ve a Redis database ready. Redis = Redis(host=os.getenv('REDIS_HOST', 'localhost'),įirst of all we create a virtual environment to ensure that we’re going to install your dependencies isolatedly: The persistence layer will be a Redis database. Each time we perform a GET request to “/” we increase one counter and see the number of hits. Let’s start from a dummy Python application. I’ve learning how to deploy one Python application to Kubernetes. Without reinventing the wheel only to make me easy to use the client/and server. We can use those files directly but I preffer to use an extra wrapper. We’ll import those files to create our client and server. Of course we can create clients using one language and servers using another. In my case python: python -m grpc_tools.protoc -I./protos -python_out=. With our proto file (language agnostic) we can create a the wrapper of our service using our programming language. Rpc sayHello (HelloRequest) returns (Hello) We need to define the types of our service and variables using a proto file. It’s something that we don’t need to do in with HTTP APIs (we use JSON and we forget the rest). With gRPC we create a persistent connection between client and server (instead of open and close connection like REST) and also we use a binary payload to reduce the size improving the performance.įirst we need to define the protocol we’re going to use. To solve this situation we’ve another tool in our toolbox. Big payloads over TCP connection means slow response time. It’s easy to encode/decode JSON in almost all languages but JSON is plain text. We normally use JSON (we also can use XML). We need to define how we’re going to encode the information. If we need to perform a lot of connections maybe we can face a bottleneck. When we create a REST connection we’re opening a TCP connection to the server, we send the request payload, we receive the response, and we close the connection. REST API is a HTTP service and HTTP is a protocol built over TCP. REST is simple, and it can fit in a lot of cases but sometimes is not enough. For example, in Python, we can use Flask. We’ve a lot of frameworks and microframeworks to create REST APIs. REST APIs are a pretty straightforward way to create a communication channel. Basically we have two choices: Synchronous (APIs) and asynchronous communications (message queues). When we work with microservices normally we need to have, in one way or another, something to communicate between them. Logs are also ready to send to ELK using a filebeat. I normally deploy it in production using a nginx as a reverse proxy and n replicas of my api. I also like to use a library to help me to work with psycopg2: a library ( ) created by me a long time ago.Īnd that’s all. This connection is a raw psycopg2 connection. We also can create the connection using a constructor. For example, we can use a function decorator to inject the connection (in this case the connection named DEFAULT) in the function signatura. We can obtain our database connection in diverse ways. Return db2.fetch_all(SQL_USERS, dict(name=name)) # Example of how to obtain new connection from database name. # Create new transaction from connection injected with a decoratorĭb.upsert('users', dict(email=email), dict(name=name)) From lib.db import get_db_from_conn, get_conn_from_dbnameįrom lib.decorators import use_schema, inject_connįrom.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |