{"id":1574,"date":"2023-03-25T19:14:11","date_gmt":"2023-03-25T11:14:11","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1574"},"modified":"2023-04-27T21:32:20","modified_gmt":"2023-04-27T13:32:20","slug":"prometheus-dynamically-modifies-targets-access-through-consul","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/prometheus-dynamically-modifies-targets-access-through-consul\/","title":{"rendered":"Prometheus\u901a\u8fc7Consul\u52a8\u6001\u4fee\u6539Targets\u63a5\u5165"},"content":{"rendered":"<h3>\u9759\u6001\u914d\u7f6e<\/h3>\n<p>\u901a\u5e38<code>Prometheus<\/code>\u8981\u589e\u52a0\u4e00\u4e2a<code>target<\/code>\uff0c\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5df2\u6dfb\u52a0\u4e00\u4e2a<code>job<\/code>\uff0c\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-yml\">- job_name: &#039;prometheus&#039;\n  static_configs:\n    - targets: [&#039;localhost:9090&#039;]<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u6bcf\u6b21\u4fee\u6539\u9700\u8981\u76f4\u63a5\u4fee\u6539\u670d\u52a1\u5668\u4e0a\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u975e\u5e38\u9ebb\u70e6\u3002<code>Prometheus<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u52a8\u6001\u670d\u52a1\u53d1\u73b0\u7684\u529f\u80fd\uff0c\u8fd9\u91cc\u4f7f\u7528<code>Consul<\/code>\u6765\u505a\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n<h3>\u5728Prometheus\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6econsul<\/h3>\n<pre><code class=\"language-yml\">- job_name: &#039;consul-prometheus&#039;\n  consul_sd_configs:\n  #consul \u5730\u5740\n    - server: &#039;xx.xx.xx.xx:8500&#039;\n      services: []\n  relabel_configs:\n    - source_labels: [__meta_consul_tags]\n      regex: .*prometheus-target.*\n      action: keep<\/code><\/pre>\n<p>\u914d\u7f6e\u8fd9\u4e2a\u4e4b\u540e\uff0c<code>Prometheus<\/code>\u5c31\u4f1a\u52a8\u6001\u53d1\u73b0<code>consul<\/code>\u7684<code>Service<\/code><br \/>\n\u8fd9\u91cc\u4f7f\u7528\u4e86<code>relabel_configs<\/code>\uff0c\u7528\u6cd5\u53ef\u4ee5\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/prometheus.io\/docs\/prometheus\/latest\/configuration\/configuration\/#relabel_configs\">https:\/\/prometheus.io\/docs\/prometheus\/latest\/configuration\/configuration\/#relabel_configs<\/a><br \/>\n\u610f\u601d\u662f\u8fc7\u6ee4\uff0c\u53ea\u6709<code>service<\/code>\u7684<code>tag<\/code>\u4e3a<code>prometheus-target<\/code>\u7684\u52a8\u6001\u53d1\u73b0<br \/>\n\u968f\u540e\u6211\u4eec\u53ea\u8981\u5728<code>consul<\/code>\u4fee\u6539<code>Service<\/code>\u5373\u53ef<\/p>\n<h3>consul\u4e2d\u6ce8\u518c\u670d\u52a1<\/h3>\n<p><code>consul<\/code>\u6ce8\u518c\u6ce8\u518c<code>service<\/code>\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5982\u679c\u9759\u6001\u6ce8\u518c\uff0c\u521b\u5efa\u6587\u4ef6\u5939<code>consul.d<\/code>\uff0c\u6dfb\u52a0\u5982\u4e0b<code>test.json<\/code>\uff1a<\/p>\n<pre><code class=\"language-json\">{\n  &quot;service&quot;:{\n    &quot;id&quot;: &quot;node&quot;,\n    &quot;name&quot;: &quot;prometheus-node&quot;,\n    &quot;address&quot;: &quot;127.0.0.1&quot;,\n    &quot;port&quot;: 9100,\n    &quot;tags&quot;: [&quot;prometheus-target&quot;],\n    &quot;checks&quot;: [\n        {\n            &quot;http&quot;: &quot;http:\/\/127.0.0.1:9100\/metrics&quot;,\n            &quot;interval&quot;: &quot;15s&quot;\n        }\n    ]\n  }\n}<\/code><\/pre>\n<p>\u5728<code>consul<\/code>\u542f\u52a8\u547d\u4ee4\u4e2d\uff0c\u6307\u5b9a\u914d\u7f6e\u8def\u5f84<\/p>\n<pre><code>-config-dir=consul.d<\/code><\/pre>\n<p>\u542f\u52a8\u540e\u67e5\u770b<code>Prometheus<\/code>\u548c<code>consul<\/code>\u754c\u9762\uff0c\u53ef\u4ee5\u770b\u5230<code>target<\/code>\u662f\u5426\u5f15\u5165\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>Http Api<\/code>\u7684\u65b9\u5f0f<\/p>\n<pre><code>curl -X PUT -d &#039;{&quot;service&quot;:{&quot;id&quot;:&quot;node&quot;,&quot;name&quot;:&quot;prometheus-node&quot;,&quot;address&quot;:&quot;127.0.0.1&quot;,&quot;port&quot;:9100,&quot;tags&quot;:[&quot;prometheus-target&quot;],&quot;checks&quot;:[{&quot;http&quot;:&quot;http:\/\/127.0.0.1:9100\/metrics&quot;,&quot;interval&quot;:&quot;15s&quot;}]}}&#039; http:\/\/127.0.0.1:8500\/v1\/agent\/service\/register<\/code><\/pre>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5404\u8bed\u8a00\u7248\u672c\u7684<code>sdk<\/code>\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.consul.io\/api\/libraries-and-sdks.html\">https:\/\/www.consul.io\/api\/libraries-and-sdks.html<\/a><\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528Java\u7248\u672c\u7684<\/p>\n<pre><code class=\"language-xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/com.orbitz.consul\/consul-client --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.orbitz.consul&lt;\/groupId&gt;\n    &lt;artifactId&gt;consul-client&lt;\/artifactId&gt;\n    &lt;version&gt;1.4.2&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public class ConsulTest {\n    Consul client;\n\n    \/**\n     * \u521d\u59cb\u5316.\n     *\/\n    @Before\n    public void init() {\n        client = Consul.builder().withHostAndPort(HostAndPort.fromParts(&quot;xx.xx.xx.xx&quot;, 8500)).build();\n        \/\/catalogClient = client.catalogClient();\n    }\n\n    @Test\n    public void queryAll() {\n        Map&lt;String, Service&gt; services = client.agentClient().getServices();\n        for (Map.Entry&lt;String, Service&gt; entry : services.entrySet()) {\n            System.out.println(&quot;key:&quot; + entry.getKey());\n            System.out.println(&quot;value:&quot; + entry.getValue().toString());\n        }\n    }\n\n    @Test\n    public void testDelete() {\n        client.agentClient().deregister(&quot;etcd&quot;);\n    }\n\n    @Test\n    public void testAdd() {\n        String serviceName = &quot;prometheus-etcd&quot;;\n        String serviceId = &quot;etcd&quot;;\n        Registration.RegCheck single = Registration.RegCheck.http(&quot;http:\/\/127.0.0.1:2379\/metrics&quot;, 20);\n        Registration reg = ImmutableRegistration.builder()\n                .check(single)\n                .addTags(&quot;prometheus-target&quot;)\n                .address(&quot;127.0.0.1&quot;)\n                .port(2379)\n                .name(serviceName)\n                .id(serviceId)\n                .build();\n        client.agentClient().register(reg);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9759\u6001\u914d\u7f6e \u901a\u5e38Prometheus\u8981\u589e\u52a0\u4e00\u4e2atarget\uff0c\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5df2\u6dfb\u52a0\u4e00\u4e2ajob\uff0c\u4f8b\u5982\u4e0b\uff1a &#8211; jo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[386],"tags":[394,387],"class_list":["post-1574","post","type-post","status-publish","format-standard","hentry","category-monitor","tag-consul","tag-prometheus"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1574"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1574\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}