Package: klempner
Constructs URLs that targeting other services.This library makes building URLs for inter-service communication safer
and easier to build.URL buildingurl=klempner.url.build_url('account','path','with spaces',query='arg',multi=['arg','support'])print(url)# http://account/path/with%20spaces?query=arg&multi=arg&multi=supportbuild_urltakes care of formatting the path and query parameters correctly
in addition to discovering the service name.  In this example, the service name
is used as-is (seeUnconfigured usagebelow).  The real power inbuild_urlis its ability to discover the scheme, host name, and port number based on the
operating environment.build_urluses thehttpscheme by default.  If the port is determined
by the discovery mechanism, then the scheme is set using a simple global
mapping from port number to scheme.Discovery examplesUnconfigured usageurl=klempner.url.build_url('account')print(url)# http://account/This isn’t very useful but if you do not configure the discovery mechanism,
thenbuild_urlassumes that the requested service is accessible directly
by name.Consul service discoveryThe basic form of using consul is notdiscoveryat all.  It is simply
URL construction that follows the naming convention that Consul’s DNS
interface exposes.os.environ['KLEMPNER_DISCOVERY']='consul'os.environ['CONSUL_DATACENTER']='production'url=klempner.url.build_url('account')print(url)# http://account.service.production.consul/If you append+agentto the discovery method, thenbuild_urlwill
connect to a Consul agent and retrieve the port number for services.  If the
port has a registered service associated with it, then the service name will
be used as the scheme.Assuming that theaccountservice is registered in consul with a service port
of 8000:os.environ['KLEMPNER_DISCOVERY']='consul+agent'url=klempner.url.build_url('account')print(url)# http://account.service.production.consul:8000/Now let’s look at what happens for a RabbitMQ connection:url=klempner.url.build_url('rabbit')print(url)# amqp://rabbit.service.production.consul:5432/The scheme is derived by looking up the port in theklempner.config.URL_SCHEME_MAPand using the result if the lookup
succeeds.The library will connect to the agent specified by theCONSUL_HTTP_ADDRenvironment variable.  If the environment variable is not specified, then the
agent listening on the localhost will be used.Kubernetes service discoveryos.environ['KLEMPNER_DISCOVERY']='kubernetes'url=klempner.url.build_url('account')print(url)# http://account.default.svc.cluster.local/os.environ['KLEMPNER_DISCOVERY']='kubernetes'os.environ['KUBERNETES_NAMESPACE']='my-team'url=klempner.url.build_url('account')print(url)# http://account.my-team.svc.cluster.local/Docker-compose service discoveryos.environ['KLEMPNER_DISCOVERY']='docker-compose'os.environ['COMPOSE_PROJECT_NAME']='foo'url=klempner.url.build_url('account')print(url)# http://127.0.0.1:32867/This discovery mechanism discovers IP and port numbers for services using
the Docker API.build_urlretrieves the list of services from the docker
host, filters the list using the “com.docker.compose.project” label, and
selects the service using the “com.docker.compose.service” label.Environment variable discoveryThis form of discovery uses environment variables with the service name encoded
into them:os.environ['KLEMPNER_DISCOVERY']='environment'os.environ['ACCOUNT_HOST']='10.2.12.23'os.environ['ACCOUNT_PORT']='11223'url=klempner.url.build_url('account')print(url)# http://10.2.12.23:11223/For a service namedadder, the following environment variables are used
if they are set.NameURL componentDefaultADDER_HOSThost portion of the authoritynoneADDER_PORTport portion of the authorityomittedADDER_SCHEMEschemesee belowThe URL scheme defaults to looking up the port number in theklempner.config.URL_SCHEME_MAPdictionary.  If the port number is not
in the dictionary, thenhttpis used as a default.os.environ['KLEMPNER_DISCOVERY']='environment'os.environ['ACCOUNT_HOST']='10.2.12.23'os.environ['ACCOUNT_PORT']='443'url=klempner.url.build_url('account')print(url)# https://10.2.12.23:443/Want to contribute?Thank you.Seedocs/contributing.rstorhttps://klempner.readthedocs.io/en/latest/contributing.htmlfor what you need
to do.