インストールと初期化
このガイドでは、NetFUNNEL Javaエージェントのインストールと初期化プロセスを説明します。
ステップ1: ライブラリのインストール
Gradle (Kotlin DSL)
build.gradle.ktsに次の依存関係を追加します:
implementation(files("libs/netfunnel-agent.jar"))
implementation("com.google.code.gson:gson:2.9.1")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
Gradle (Groovy DSL)
build.gradleに次の依存関係を追加します:
implementation files('libs/netfunnel-agent.jar')
implementation 'com.google.code.gson:gson:2.9.1'
// Java使用時
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
Maven
pom.xmlに次の依存関係を追加します:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.15.2</version>
</dependency>
netfunnel-agent.jarファイルをlibsフォルダに追加し、次の設定を追加します:
<dependency>
<groupId>com.stclab</groupId>
<artifactId>netfunnel-agent</artifactId>
<version>4.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/netfunnel-agent.jar</systemPath>
</dependency>
ステップ2: Client IDとSecret Keyの取得
- NetFUNNELコンソールに移動
- 右上のプロフィールアイコンをクリック
統合資格情報(Integration Credentials) メニューを選択Client IDとSecret Keyをコピー
Client IDとSecret Keyの確認場所
コンソール画面右上のプロフィールアイコンをクリックした後、統合資格情報メニューで確認できます。
ステップ3: InterceptorまたはFilterの適用
プロジェクト構造に応じて、InterceptorとFilterのいずれか1つだけを選択して適用します。両方の方式は同じNetFUNNEL待機列制御機能を提供するため、重複して実装する必要はありません。
Interceptor適用方法
1. Interceptorの作成
サーバーのリクエストを最初に受信するInterceptorに次のようなコードを記述します:
import com.stclab.Netfunnel;
import com.stclab.servlet.NetFunnelServletService;
import com.stclab.utils.NetFunnelInitialize;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class NetFunnelInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
NetFunnelInitialize config = NetFunnelInitialize.Companion.builder()
.clientId("{{CLIENT_ID}}") // ステップ2で取得したClient ID
.secretKey("{{SECRET_KEY}}") // ステップ2で取得したSecret Key
.build();
Netfunnel netfunnel = new Netfunnel(config, new NetFunnelServletService(request, response), null, null);
return netfunnel.run();
}
}
2. Interceptorの登録
作成したinterceptorをWebMvcConfigurerのaddInterceptorsメソッドをオーバーライドして登録します:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final NetFunnelInterceptor netFunnelInterceptor;
@Autowired
public WebConfig(NetFunnelInterceptor netFunnelInterceptor) {
this.netFunnelInterceptor = netFunnelInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(netFunnelInterceptor)
.addPathPatterns("/**");
}
}
Filter適用方法
1. Filterの作成
サーバーのリクエストを最初に受信するFilterに次のようなコードを記述します:
import com.stclab.Netfunnel;
import com.stclab.servlet.NetFunnelServletService;
import com.stclab.utils.NetFunnelInitialize;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class NetFunnelFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
NetFunnelInitialize config = NetFunnelInitialize.Companion.builder()
.clientId("{{CLIENT_ID}}") // ステップ2で取得したClient ID
.secretKey("{{SECRET_KEY}}") // ステップ2で取得したSecret Key
.build();
Netfunnel netfunnel = new Netfunnel(config, new NetFunnelServletService(request, response), null, null);
if (!netfunnel.run()) {
return;
}
chain.doFilter(servletRequest, servletResponse);
}
}
2. Filterの登録
作成したFilterをビーンに登録します:
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean<Filter> filterRegistrationBean() {
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
registration.setFilter(new NetFunnelFilter());
registration.addUrlPatterns("/*");
registration.setOrder(1);
return registration;
}
}
ステップ4: 初期化設定
初期化設定の詳細については、初期化設定ドキュメントを参照してください。
基本初期化例
NetFunnelInitialize config = NetFunnelInitialize.Companion.builder()
.clientId("{{CLIENT_ID}}")
.secretKey("{{SECRET_KEY}}")
.build();
ステップ5: インストール確認
エージェントが正しくインストールされているか確認するには、次を確認してください:
- 依存関係の確認:
netfunnel-agent.jar、gson、jackson-module-kotlinが正しく追加されているか確認 - 初期化成功: サーバー起動時にエラーが発生しないこと
- Interceptor/Filter適用: リクエストが正しく処理されているか確認
トラブルシューティング
ライブラリインストール失敗:
netfunnel-agent.jarファイルがlibsフォルダにあるか確認- Gradle/Maven依存関係が正しく追加されているか確認
初期化失敗:
clientIdとsecretKeyが正しく設定されているか確認- 空文字列でないか確認
Interceptor/Filterが動作しない:
- Interceptor/Filterが正しく登録されているか確認
addPathPatternsが正しく設定されているか確認