1 はじめに
Spring Boot Admin はプロジェクトでよく使用される監視方法であり、サービスが実行されているかどうか、およびクラス呼び出しやトラフィックなどの実行パラメーターを動的に監視できます。サーバーとクライアントに分かれています。
- サーバー: 表示 UI および監視サービスを提供します。
- client: サーバー、監視対象のプロジェクトに参加します。
同時に、監視プロセスでは spring-boot-starter-actuator がよく言及されますが、アクチュエーターが使用されると、プロジェクト内でプローブされ、ヒープ メモリやスタック メモリなどの一連の監視 API インターフェイスが提供されます。以下のデータはアクチュエータから提供されます。
企業が小規模なプロジェクトを開発しており、リソースとワークロードを集中的に使用するソリューションである Prometheus+Grafana を選択したくない場合は、Spring Boot Admin が最良の選択です。
上の図からわかるように、Spring Boot Admin はプロセス、スレッド、メモリ、プロジェクトが正常かどうかなどの基本的な指標を把握できます。日常の一般的なニーズを満たしています。以下では、Spring Boot Admin を日常のプロジェクトに統合する方法を紹介します。
2.スプリングブートを統合する
Spring Boot Admin を有効にするには、サーバーとクライアントを別々にデプロイする必要があります。これについては、以下で個別に説明します。
2.1 サーバー側
2.1.1 springboot プロジェクトの準備
まず、git でダウンロードするか、新しいプロジェクトを作成するかにかかわらず、springboot プロジェクトを準備する必要があります。
2.1.2 pom ファイルを変更する
pom.xmlにSpring Boot Adminの依存関係情報を追加 Spring Cloud alibaba Ecologyを利用するためnacosパッケージもここに追加しますので参加を選択してください。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.0</version>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
2.1.3 ymlファイルを変更する
プロジェクトが新しい springboot プロジェクトの場合、プロジェクトを開始する前に yml 構成ファイルを変更する必要があります
server:
port: 8008
servlet:
context-path: /monitor
spring:
application:
name: monitor-resource # Eureka页面显示
2.1.4 スタートアップクラスの変更
@EnableAdminServer アノテーションをアプリケーション起動クラスに追加します。アイテムがサーバー側で監視されていることを示します。
@SpringBootApplication
@EnableAdminServer
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
上記が終わるとSpring Boot Adminのサーバー側の設定が完了し、プロジェクトを起動するとアクセスできるようにip:端口/context-path
なります。上記の構成に従って、localhost:8088/monitor
訪問を完了できます。以下は、デプロイが成功した後のページ スタイルです。
2.2. クライアント側
他の springboot プロジェクトを監視したい場合は、サーバーサービスに接続する必要がありますので、その設定方法を紹介します。
2.2.1 pom ファイルを変更する
Spring Boot Admin クライアントの依存関係を pom.xml に追加します。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.2.0</version>
</dependency>
2.2.2 ymlファイルを変更する
context-path の場合、spring.boot 設定を追加する必要があります。追加しないとエラーが報告されます。この記事には nacos の構成が記載されています。必要に応じて構成情報を参照してください。
server:
port: 8008
servlet:
context-path: /monitor
spring:
application:
name: monitor-resource
boot:
admin:
client:
api-path:
url: http://127.0.0.1:8008/monitor # 这里为server地址,如果有context-path需要加入
instance:
prefer-ip: true # 使用ip注册进来
management-url: /monitor
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: ip:8848
metadata: # 如果有context-path需要加上,否者报错
management:
context-path: ${server.servlet.context-path}/actuator
config:
# 配置中心地址
server-addr: ip:8848
# 配置文件格式
file-extension: yml
management: # actuator配置
endpoint:
health:
show-details: always
endpoints:
enabled-by-default: true
web:
exposure:
include: '*'
以上で Spring Boot Admin クライアントの設定は完了です。開始後、監視サーバー サービスに参加し、サーバー サービスに再度アクセスすると、次のようなページが表示されます。このページを通じてフィードバックされたデータは、すでに日常のニーズを満たすことができます
。
3.セキュリティ
日常的なプロジェクトでは機密情報が多く、外部に漏洩するとセキュリティ上大きな問題を引き起こすため、監視基盤にはサーバー情報やインターフェース情報などの重要な情報が含まれるため、パスワード管理を追加することを推奨します。Spring Boot Adminにパスワード認証を追加する方法を紹介します。
3.1 pom ファイルを変更します。
ここでは、サーバー プロジェクトを開き、Spring Security テクノロジを使用して、依存関係情報を追加します。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
4.2 application.xml ファイルを変更する
spring.security 構成を追加します。これは、監視プラットフォームにアクセスするためのアカウントのパスワードです。
spring:
security:
user:
name: "admin"
password: "1qaz@WSX"
4.3 新しいログインおよびログアウト構成
他の人に簡単に監視入口を見つけられないよう、デフォルトのアクセスアドレスは使用せず、アクセスページをカスタマイズします。
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter( "redirectTo" );
http.authorizeRequests()
.antMatchers( adminContextPath + "/assets/**" ).permitAll()
.antMatchers( adminContextPath + "/login" ).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
.logout().logoutUrl( adminContextPath + "/logout" ).and()
.httpBasic().and()
.csrf().disable();
}
}
上記の設定が完了したら、サービスを再起動し、上記で設定したアドレスに再度アクセスすると、アカウントのパスワードを使用してログインする必要があることがわかります。