首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带Prometheus Pushgateway的千分尺-增加TLS支持

带Prometheus Pushgateway的千分尺-增加TLS支持
EN

Stack Overflow用户
提问于 2022-08-31 14:22:18
回答 1查看 155关注 0票数 0

我有一个使用千分尺的Pushgateway的Spring应用程序,主要基于本教程:https://luramarchanjo.tech/2020/01/05/spring-boot-2.2-and-prometheus-pushgateway-with-micrometer.html

pom.xml有以下相关的依赖关系:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
    <version>0.16.0</version>
</dependency>

application.properties文件有:

代码语言:javascript
复制
management.metrics.export.prometheus.pushgateway.enabled=true
management.metrics.export.prometheus.pushgateway.shutdown-operation=PUSH
management.metrics.export.prometheus.pushgateway.baseUrl=localhost:9091

在Dev环境下,它在没有任何TLS的情况下连接到Pushgateway时,在本地运行良好。在我们的CI环境中,Prometheus Pushgateway已经启用了TLS。如何在这个Spring引导应用程序中配置TLS支持和配置证书?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-03 15:51:05

由于TLS的使用,您需要定制几个Spring类:

一个HttpConnectionFactory,由prometheus的PushGateway用来创建一个安全连接,然后创建一个使用前一个推送网关的PrometheusPushGatewayManager

您将需要实现prometheus的接口HttpConnectionFactory,我假设您能够创建一个有效的javax.net.ssl.SSLContext对象(如果不能,则最终获得更多详细信息)。

HttpConnectionFactory示例:

代码语言:javascript
复制
public class MyTlsConnectionFactory implements io.prometheus.client.exporter.HttpConnectionFactory {
    @Override
    public HttpURLConnection create(String hostUrl) {
        // considering you can get javax.net.ssl.SSLContext or javax.net.ssl.SSLSocketFactory
        URL url = new URL(hostUrl);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setSSLSocketFactory(sslContext.getSocketFactory());
        return connection;
    }
}

PrometheusPushGatewayManagerPushGateway

代码语言:javascript
复制
@Bean 
public HttpConnectionFactory tlsConnectionFactory() {
    return new MyTlsConnectionFactory();
}

@Bean 
public PushGateway pushGateway(HttpConnectionFactory connectionFactory) throws MalformedURLException {
    String url = "https://localhost:9091"; // replace by your props
    PushGateway pushGateway = new PushGateway(new URL(url));
    pushGateway.setConnectionFactory(connectionFactory); 
    return pushGateway;
}

@Bean 
public PrometheusPushGatewayManager tlsPrometheusPushGatewayManager(PushGateway pushGateway,
                                                                    CollectorRegistry registry) {
    // fill the others params accordingly (the important is pushGateway!)
    return new PrometheusPushGatewayManager(
            pushGateway,
            registry,
            Duration.of(15, ChronoUnit.SECONDS),
            "some-job-id",
            null,
            PrometheusPushGatewayManager.ShutdownOperation.PUSH
    );
}

如果您在从java代码中检索SSLContext时遇到困难,我建议您学习库https://github.com/Hakky54/sslcontext-kickstarthttps://github.com/Hakky54/mutual-tls-ssl (这说明了如何使用不同的客户端库)。

然后,就可以以一种干净的方式在java代码中生成SSLContext,例如:

代码语言:javascript
复制
String keyStorePath = "client.jks";
char[] keyStorePassword = "password".toCharArray();
SSLFactory sslFactory = SSLFactory.builder()
        .withIdentityMaterial(keyStorePath, keyStorePassword)
        .build();
javax.net.ssl.SSLContext sslContext = sslFactory.getSslContext();

最后,如果您需要为测试目的设置本地Prometheus + TLS环境,我建议您按照下面的贴子:https://smallstep.com/hello-mtls/doc/client/prometheus

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73557400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档