使用 Apache SkyWalking 进行 Spring Cloud 应用的分布式追踪与监控:完整教程
@Configuration
public class SkyWalkingConfig {
@Bean
public ServletRegistrationBean<Servlet> servletRegistrationBean() {
return new ServletRegistrationBean<>(new Servlet() {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String agentOSS = request.getHeader("User-Agent");
if (agentOSS != null && agentOSS.toLowerCase().contains("apache skyapm")) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
}, "/v1/trace");
}
}
这段代码定义了一个名为SkyWalkingConfig
的配置类,并在其中创建了一个Servlet注册Bean。这个Servlet处理SkyWalking代理发送的追踪数据,验证请求头并相应正确的HTTP状态码。这样可以确保只有SkyWalking代理可以与服务通信,增加系统的安全性。
评论已关闭