项目需要对Nexus进行健康检查,甲方自定义了接口返回的数据。基于此,准备在Nexus里面开发一个接口。在本项目中使用的Nexus版本号为3.63.0-01,通过查询资料以及对代码的拜读,明确需要修改nexus-base这个包。需要修改的文件截图如下:
具体为:
1、工程目录\components\nexus-base\src\main\java\org\sonatype\nexus\internal\metrics2、工程目录\Nexus\components\nexus-base\src\main\java\org\sonatype\nexus\internal\web\WebModule.java
HealCheckCustomer.java代码如下:
package org.sonatype.nexus.internal.metrics;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;/*** @ProjectName: Nexus* @Class: org.sonatype.nexus.internal.metrics.HealCheckCustomer* @Description: java类作用描述* @Author: crue* @CreateDate: 2024/4/22$ 20:35$* @Version: 1.0*/
public class HealCheckCustomer extends HttpServlet {private static final long serialVersionUID = 3972654177231086758L;private static final String CONTENT_TYPE = "text/plain";private static final String CONTENT = "{\"status\": 200, \"message\": \"service is running\"}";private static final String CACHE_CONTROL = "Cache-Control";private static final String NO_CACHE = "must-revalidate,no-cache,no-store";public HealCheckCustomer() {}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setStatus(200);resp.setHeader(CACHE_CONTROL, NO_CACHE);resp.setContentType(CONTENT_TYPE);PrintWriter writer = resp.getWriter();Throwable var4 = null;try {writer.println(CONTENT);} catch (Throwable var13) {var4 = var13;throw var13;} finally {if (writer != null) {if (var4 != null) {try {writer.close();} catch (Throwable var12) {var4.addSuppressed(var12);}} else {writer.close();}}}}
}
HealthCheckMetricsModule.java代码如下:
package org.sonatype.nexus.internal.metrics;import com.codahale.metrics.Clock;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.AbstractModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.nexus.common.app.FeatureFlag;
import org.sonatype.nexus.security.FilterChainModule;
import org.sonatype.nexus.security.SecurityFilter;
import org.sonatype.nexus.security.anonymous.AnonymousFilter;import javax.inject.Named;import static org.sonatype.nexus.common.app.FeatureFlags.SESSION_ENABLED;@Named
@FeatureFlag(name = SESSION_ENABLED)
public class HealthCheckMetricsModuleextends AbstractModule
{private static final Logger log = LoggerFactory.getLogger(HealthCheckMetricsModule.class);protected static final String MOUNT_POINT = "/v1/api/healthCheck";@Overrideprotected void configure() {// NOTE: AdminServletModule (metrics-guice integration) generates invalid links, so wire up servlets ourselvesfinal Clock clock = Clock.defaultClock();bind(Clock.class).toInstance(clock);final JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());bind(JsonFactory.class).toInstance(jsonFactory);install(new HealthCheckMetricsServletModule(MOUNT_POINT){@Overrideprotected void bindSecurityFilter() {filter(MOUNT_POINT + "/*").through(SecurityFilter.class);}});// require permission to use endpointsinstall(new FilterChainModule(){@Overrideprotected void configure() {addFilterChain(MOUNT_POINT + "/**",AnonymousFilter.NAME);}});log.info("Metrics support configured");}
}
HealthCheckMetricsServletModule.java代码如下:
package org.sonatype.nexus.internal.metrics;import com.codahale.metrics.servlet.InstrumentedFilter;
import com.google.inject.servlet.ServletModule;
import org.sonatype.nexus.security.SecurityFilter;public abstract class HealthCheckMetricsServletModuleextends ServletModule
{private final String mountPoint;protected HealthCheckMetricsServletModule(final String mountPoint) {this.mountPoint = mountPoint;}@Overrideprotected void configureServlets() {bind(HealCheckCustomer.class);serve(mountPoint + "/life").with(new HealCheckCustomer());// record metrics for all webapp accessfilter("/*").through(new InstrumentedFilter());bind(SecurityFilter.class);// configure securitybindSecurityFilter();}protected abstract void bindSecurityFilter();
}
WebModule中需要增加的代码为 highPriorityBinder.install(new HealthCheckMetricsModule());这句,完整代码如下:
protected void installMetricsModule(final Binder highPriorityBinder) {highPriorityBinder.install(new MetricsModule());// 自定义highPriorityBinder.install(new HealthCheckMetricsModule());}
利用idea自带的clean、compile以后,直接运行 mvn package -DskipTests即可得到修改后的包。截图如下:
访问效果如下: