In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Python scripts read Consul configuration information. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Let's talk about the background first. why write a script to read the configuration information of Consul? What is Consul? Consul is a google open source service discovery and configuration management center service developed in the go language. The company currently uses this thing to manage some configuration information on the project. The company's environment is deployed through docker images, and images are managed through rancher. One of the problems with this set of things is that after each update of the service, the corresponding ip address of the service changes dynamically. Every time you need to use swagger to test the interface, you have to go to rancher to find a new ip address, which is troublesome. As it happens, recently, the department is considering preparing for interface automation testing, and the problem that ip always changes will also need to be solved. Therefore, we should first write a script to call the interface to get configuration information and get the ip and port information of a service. Print out the swagger address by the way, and it will be much more convenient to access the swagger address of a service in the future.
If python reads the Consul configuration information, it uses the python-consul library. Before using it, you need to install the corresponding plug-in.
Pip install python-consul
The encapsulated classes for reading consul configuration are as follows:
The functions defined in the following script only print data and do not return specific data, because the above configuration information needs to be processed for different consul addresses. For example, I need to retrieve the ip and port information of some deployed services this time, but there are some other configuration information on consul that can be filtered out. In addition, some configuration items may be duplicated due to configuration errors and have not been deleted for the time being. in this case, you can get valid configuration information later in the script according to the relevant fields returned.
Import consul class RFConsul (object): def _ _ init__ (self, host, port): initialize Connect to consul server "" self._client = consul.Consul (host, port, scheme='http', verify=False) def getAllServices (self): services = self._client.agent.services () for i in services: service = services.get (I) print ({service ['Service']: service}) def getServiceByName (self) Service_name): data = self._client.catalog.service (service_name) for value in data [1]: print (swagger address of service_name + "service:" + "http://" + value ['ServiceAddress'] +": "+ str (value [' ServicePort']) +" / swagger-ui.html ")
GetServiceByName: according to the service name, the access address of the stitched swagger-ui is printed directly after the call.
GetAllServices: prints all configuration information under a consul address.
Call method:
Host = "127.0.0.1" # ipport of consul server = "8500" # external port of consul server consul_client = RFConsul (host, port) consul_client.getAllServices () res = consul_client.getServiceByName ("xxx-servicename") # enter the name of the configuration item
After calling it, wouldn't it be nice to get the swagger access address similar to the following figure:
Then, in a company, the address of a configuration center like this generally needs to be accessed through an agent. The company uses a socket5 agent and usually opens the agent software when invoking the script. You can also add configuration information for the socket agent to the script:
Import socket import socks socks.set_default_proxy (socks.SOCKS5, "ip", port) socket.socket = socks.socksocket Thank you for reading! This is the end of the article on "how Python scripts read Consul configuration information". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 282
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.