@@ -1,14 +1,10 @@ | |||
# spring-boot-demo-admin | |||
## spring-boot-demo-admin | |||
> 本 demo 主要演示了 Spring Boot 如何集成 Admin 管控台,监控管理 Spring Boot 应用,分别为 admin 服务端和 admin 客户端,两个模块。 | |||
## 运行步骤 | |||
### 1.开发步骤 | |||
1. 进入 `spring-boot-demo-admin-server` 服务端,启动管控台服务端程序 | |||
2. 进入 `spring-boot-demo-admin-client` 客户端,启动客户端程序,注册到服务端 | |||
3. 观察服务端里,客户端程序的运行状态等信息 | |||
## pom.xml | |||
#### 1.1.pom.xml | |||
```xml | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
@@ -20,18 +16,21 @@ | |||
<groupId>com.xkcoding</groupId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
</parent> | |||
<modelVersion>4.0.0</modelVersion> | |||
<artifactId>spring-boot-demo-admin</artifactId> | |||
<artifactId>demo-admin</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
<packaging>pom</packaging> | |||
<properties> | |||
<spring-boot-admin.version>2.1.0</spring-boot-admin.version> | |||
<java.version>17</java.version> | |||
<spring-boot-admin.version>3.0.0-M4</spring-boot-admin.version> | |||
</properties> | |||
<modules> | |||
<module>spring-boot-demo-admin-client</module> | |||
<module>spring-boot-demo-admin-server</module> | |||
<module>admin-server</module> | |||
<module>admin-client</module> | |||
</modules> | |||
<dependencyManagement> | |||
@@ -49,3 +48,21 @@ | |||
</project> | |||
``` | |||
#### 1.2.admin-server开发 | |||
参考 [admin-server 模块](./admin-server/README.md) | |||
#### 1.3.admin-client开发 | |||
参考 [admin-client 模块](./admin-client/README.md) | |||
### 2.测试 | |||
1. 进入 [admin-server](./admin-server) 服务端,启动管控台服务端程序 | |||
2. 进入 [admin-client](./admin-client) 客户端,启动客户端程序,注册到服务端 | |||
3. 打开 `http://localhost:8000` 观察 client 是否注册成功,以及实例注册上来的详细信息 | |||
4. Spring Boot Admin 支持在线查看日志,但是需要客户端事先配置日志,配置好之后,就可以在线查看日志了 | |||
### 3.参考 | |||
官方文档:https://codecentric.github.io/spring-boot-admin/3.0.0-M4/#getting-started |
@@ -1,25 +0,0 @@ | |||
/target/ | |||
!.mvn/wrapper/maven-wrapper.jar | |||
### STS ### | |||
.apt_generated | |||
.classpath | |||
.factorypath | |||
.project | |||
.settings | |||
.springBeans | |||
.sts4-cache | |||
### IntelliJ IDEA ### | |||
.idea | |||
*.iws | |||
*.iml | |||
*.ipr | |||
### NetBeans ### | |||
/nbproject/private/ | |||
/build/ | |||
/nbbuild/ | |||
/dist/ | |||
/nbdist/ | |||
/.nb-gradle/ |
@@ -1,95 +1,92 @@ | |||
# spring-boot-demo-admin-client | |||
## spring-boot-demo-admin-client | |||
> 本 demo 主要演示了普通项目如何集成 Spring Boot Admin,并把自己的运行状态交给 Spring Boot Admin 进行展现。 | |||
## pom.xml | |||
### 1.开发步骤 | |||
#### 1.1.增加依赖 | |||
```xml | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||
<modelVersion>4.0.0</modelVersion> | |||
<artifactId>spring-boot-demo-admin-client</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
<packaging>jar</packaging> | |||
<name>spring-boot-demo-admin-client</name> | |||
<description>Demo project for Spring Boot</description> | |||
<parent> | |||
<groupId>com.xkcoding</groupId> | |||
<artifactId>spring-boot-demo-admin</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
</parent> | |||
<properties> | |||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||
<java.version>1.8</java.version> | |||
</properties> | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-web</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>de.codecentric</groupId> | |||
<artifactId>spring-boot-admin-starter-client</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-security</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
<finalName>spring-boot-demo-admin-client</finalName> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-maven-plugin</artifactId> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
</project> | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-web</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>de.codecentric</groupId> | |||
<artifactId>spring-boot-admin-starter-client</artifactId> | |||
</dependency> | |||
<!-- In order to secure the endpoints --> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-security</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.projectlombok</groupId> | |||
<artifactId>lombok</artifactId> | |||
<optional>true</optional> | |||
</dependency> | |||
</dependencies> | |||
``` | |||
#### 1.2.增加测试路由以及日志支持 | |||
```java | |||
@Slf4j | |||
@RestController | |||
@RequestMapping("/test") | |||
public class TestController { | |||
@GetMapping | |||
public String test() { | |||
int range = new Random().nextInt(1, 11); | |||
for (int i = 0; i < range; i++) { | |||
log.info("#TestController#test: info log total: {}, now is {}", range, (i + 1)); | |||
} | |||
return "Just for testing"; | |||
} | |||
} | |||
``` | |||
## application.yml | |||
#### 1.3.修改配置文件 | |||
```yaml | |||
server: | |||
port: 8080 | |||
servlet: | |||
context-path: /demo | |||
spring: | |||
application: | |||
# Spring Boot Admin展示的客户端项目名,不设置,会使用自动生成的随机id | |||
name: spring-boot-demo-admin-client | |||
security: | |||
user: | |||
name: xkcoding | |||
password: 123456 | |||
boot: | |||
admin: | |||
client: | |||
# Spring Boot Admin 服务端地址 | |||
url: "http://localhost:8000/" | |||
username: ${spring.security.user.name} | |||
password: ${spring.security.user.password} | |||
instance: | |||
service-host-type: ip | |||
metadata: | |||
# 客户端端点信息的安全认证信息 | |||
user.name: ${spring.security.user.name} | |||
user.password: ${spring.security.user.password} | |||
security: | |||
user: | |||
name: xkcoding | |||
password: 123456 | |||
management: | |||
endpoint: | |||
health: | |||
@@ -100,5 +97,18 @@ management: | |||
exposure: | |||
# 设置端点暴露的哪些内容,默认["health","info"],设置"*"代表暴露所有可访问的端点 | |||
include: "*" | |||
info: | |||
env: | |||
enabled: true | |||
# 增加日志配置,为了展示 Spring Boot Admin 的观察在线日志的能力 | |||
logging: | |||
file: | |||
name: logs/${spring.application.name}.log | |||
pattern: | |||
file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx" | |||
``` | |||
### 2.测试 | |||
1. 启动 `AdminServerApplication` 之后,再启动 `AdminClientApplication`,然后输入 `http://localhost:8080/demo` ,观察 client 程序是否启动成功 | |||
2. 输入 `http://localhost:8080/demo/test` ,观察 admin-server 是否可以在线查看日志 |
@@ -16,9 +16,7 @@ | |||
<description>Demo project for Spring Boot</description> | |||
<properties> | |||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||
<java.version>1.8</java.version> | |||
<java.version>17</java.version> | |||
</properties> | |||
<dependencies> | |||
@@ -32,6 +30,7 @@ | |||
<artifactId>spring-boot-admin-starter-client</artifactId> | |||
</dependency> | |||
<!-- In order to secure the endpoints --> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-security</artifactId> | |||
@@ -42,6 +41,12 @@ | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.projectlombok</groupId> | |||
<artifactId>lombok</artifactId> | |||
<optional>true</optional> | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
@@ -50,6 +55,13 @@ | |||
<plugin> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-maven-plugin</artifactId> | |||
<executions> | |||
<execution> | |||
<goals> | |||
<goal>build-info</goal> | |||
</goals> | |||
</execution> | |||
</executions> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
@@ -5,16 +5,16 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; | |||
/** | |||
* <p> | |||
* 启动类 | |||
* Spring Boot Admin Client 启动类 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-10-8 14:16 | |||
* @date Created in 2022-08-17 16:42 | |||
*/ | |||
@SpringBootApplication | |||
public class SpringBootDemoAdminClientApplication { | |||
public class AdminClientApplication { | |||
public static void main(String[] args) { | |||
SpringApplication.run(SpringBootDemoAdminClientApplication.class, args); | |||
SpringApplication.run(AdminClientApplication.class, args); | |||
} | |||
} |
@@ -5,16 +5,16 @@ import org.springframework.web.bind.annotation.RestController; | |||
/** | |||
* <p> | |||
* 首页 | |||
* 测试路由-首页 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-10-08 14:15 | |||
* @date Created in 2022-08-17 16:43 | |||
*/ | |||
@RestController | |||
public class IndexController { | |||
@GetMapping(value = {"", "/"}) | |||
public String index() { | |||
return "This is a Spring Boot Admin Client."; | |||
return "This is a Spring Boot Admin Client Demo."; | |||
} | |||
} |
@@ -0,0 +1,30 @@ | |||
package com.xkcoding.admin.client.controller; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.web.bind.annotation.GetMapping; | |||
import org.springframework.web.bind.annotation.RequestMapping; | |||
import org.springframework.web.bind.annotation.RestController; | |||
import java.util.Random; | |||
/** | |||
* 测试路由-Just for testing | |||
* | |||
* @author 一珩(沈扬凯 yk.shen@tuya.com) | |||
* @date 2022-08-17 16:51 | |||
*/ | |||
@Slf4j | |||
@RestController | |||
@RequestMapping("/test") | |||
public class TestController { | |||
@GetMapping | |||
public String test() { | |||
int range = new Random().nextInt(1, 11); | |||
for (int i = 0; i < range; i++) { | |||
log.info("#TestController#test: info log total: {}, now is {}", range, (i + 1)); | |||
} | |||
return "Just for testing"; | |||
} | |||
} |
@@ -2,24 +2,28 @@ server: | |||
port: 8080 | |||
servlet: | |||
context-path: /demo | |||
spring: | |||
application: | |||
# Spring Boot Admin展示的客户端项目名,不设置,会使用自动生成的随机id | |||
name: spring-boot-demo-admin-client | |||
security: | |||
user: | |||
name: xkcoding | |||
password: 123456 | |||
boot: | |||
admin: | |||
client: | |||
# Spring Boot Admin 服务端地址 | |||
url: "http://localhost:8000/" | |||
username: ${spring.security.user.name} | |||
password: ${spring.security.user.password} | |||
instance: | |||
service-host-type: ip | |||
metadata: | |||
# 客户端端点信息的安全认证信息 | |||
user.name: ${spring.security.user.name} | |||
user.password: ${spring.security.user.password} | |||
security: | |||
user: | |||
name: xkcoding | |||
password: 123456 | |||
management: | |||
endpoint: | |||
health: | |||
@@ -30,3 +34,12 @@ management: | |||
exposure: | |||
# 设置端点暴露的哪些内容,默认["health","info"],设置"*"代表暴露所有可访问的端点 | |||
include: "*" | |||
info: | |||
env: | |||
enabled: true | |||
# 增加日志配置,为了展示 Spring Boot Admin 的观察在线日志的能力 | |||
logging: | |||
file: | |||
name: logs/${spring.application.name}.log | |||
pattern: | |||
file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx" |
@@ -0,0 +1,13 @@ | |||
package com.xkcoding.admin.client; | |||
import org.junit.jupiter.api.Test; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
@SpringBootTest | |||
class AdminClientApplicationTests { | |||
@Test | |||
void contextLoads() { | |||
} | |||
} |
@@ -1,16 +0,0 @@ | |||
package com.xkcoding.admin.client; | |||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
import org.springframework.test.context.junit4.SpringRunner; | |||
@RunWith(SpringRunner.class) | |||
@SpringBootTest | |||
public class SpringBootDemoAdminClientApplicationTests { | |||
@Test | |||
public void contextLoads() { | |||
} | |||
} |
@@ -1,25 +0,0 @@ | |||
/target/ | |||
!.mvn/wrapper/maven-wrapper.jar | |||
### STS ### | |||
.apt_generated | |||
.classpath | |||
.factorypath | |||
.project | |||
.settings | |||
.springBeans | |||
.sts4-cache | |||
### IntelliJ IDEA ### | |||
.idea | |||
*.iws | |||
*.iml | |||
*.ipr | |||
### NetBeans ### | |||
/nbproject/private/ | |||
/build/ | |||
/nbbuild/ | |||
/dist/ | |||
/nbdist/ | |||
/.nb-gradle/ |
@@ -1,90 +1,67 @@ | |||
# spring-boot-demo-admin-server | |||
## spring-boot-demo-admin-server | |||
> 本 demo 主要演示了如何搭建一个 Spring Boot Admin 的服务端项目,可视化展示自己客户端项目的运行状态。 | |||
## pom.xml | |||
### 1.开发步骤 | |||
```xml | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||
<modelVersion>4.0.0</modelVersion> | |||
<artifactId>spring-boot-demo-admin-server</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
<packaging>jar</packaging> | |||
<name>spring-boot-demo-admin-server</name> | |||
<description>Demo project for Spring Boot</description> | |||
<parent> | |||
<groupId>com.xkcoding</groupId> | |||
<artifactId>spring-boot-demo-admin</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
</parent> | |||
<properties> | |||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||
<java.version>1.8</java.version> | |||
</properties> | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-web</artifactId> | |||
</dependency> | |||
#### 1.1.增加依赖 | |||
<dependency> | |||
<groupId>de.codecentric</groupId> | |||
<artifactId>spring-boot-admin-starter-server</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
<finalName>spring-boot-demo-admin-server</finalName> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-maven-plugin</artifactId> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
</project> | |||
```xml | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-web</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>de.codecentric</groupId> | |||
<artifactId>spring-boot-admin-starter-server</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
``` | |||
## SpringBootDemoAdminServerApplication.java | |||
#### 1.2.启动类配置 | |||
```java | |||
/** | |||
* <p> | |||
* 启动类 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-10-08 14:08 | |||
*/ | |||
@EnableAdminServer | |||
@SpringBootApplication | |||
public class SpringBootDemoAdminServerApplication { | |||
public class AdminServerApplication { | |||
public static void main(String[] args) { | |||
SpringApplication.run(SpringBootDemoAdminServerApplication.class, args); | |||
} | |||
public static void main(String[] args) { | |||
SpringApplication.run(AdminServerApplication.class, args); | |||
} | |||
} | |||
``` | |||
## application.yml | |||
#### 1.3.修改配置文件 | |||
```yaml | |||
server: | |||
port: 8000 | |||
spring: | |||
application: | |||
name: spring-boot-demo-admin-server | |||
boot: | |||
admin: | |||
instance-auth: | |||
enabled: true | |||
default-user-name: xkcoding | |||
default-password: 123456 | |||
ui: | |||
external-views: | |||
- label: "xkcoding 🚀" | |||
url: https://xkcoding.com | |||
order: 2000 | |||
``` | |||
### 2.测试 | |||
1. 启动 `AdminServerApplication`,然后打开 `http://localhost:8000` | |||
2. 再启动 `AdminClientApplication`,等待启动成功之后,观察是否成功注册上服务端 |
@@ -16,9 +16,7 @@ | |||
<description>Demo project for Spring Boot</description> | |||
<properties> | |||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||
<java.version>1.8</java.version> | |||
<java.version>17</java.version> | |||
</properties> | |||
<dependencies> | |||
@@ -6,17 +6,17 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; | |||
/** | |||
* <p> | |||
* 启动类 | |||
* Spring Boot Admin Server 启动类 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-10-08 14:08 | |||
* @date Created in 2022-08-17 16:41 | |||
*/ | |||
@EnableAdminServer | |||
@SpringBootApplication | |||
public class SpringBootDemoAdminServerApplication { | |||
public class AdminServerApplication { | |||
public static void main(String[] args) { | |||
SpringApplication.run(SpringBootDemoAdminServerApplication.class, args); | |||
SpringApplication.run(AdminServerApplication.class, args); | |||
} | |||
} |
@@ -1,2 +1,16 @@ | |||
server: | |||
port: 8000 | |||
spring: | |||
application: | |||
name: spring-boot-demo-admin-server | |||
boot: | |||
admin: | |||
instance-auth: | |||
enabled: true | |||
default-user-name: xkcoding | |||
default-password: 123456 | |||
ui: | |||
external-views: | |||
- label: "xkcoding 🚀" | |||
url: https://xkcoding.com | |||
order: 2000 |
@@ -0,0 +1,13 @@ | |||
package com.xkcoding.admin.server; | |||
import org.junit.jupiter.api.Test; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
@SpringBootTest | |||
class AdminServerApplicationTests { | |||
@Test | |||
void contextLoads() { | |||
} | |||
} |
@@ -1,16 +0,0 @@ | |||
package com.xkcoding.admin.server; | |||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
import org.springframework.test.context.junit4.SpringRunner; | |||
@RunWith(SpringRunner.class) | |||
@SpringBootTest | |||
public class SpringBootDemoAdminServerApplicationTests { | |||
@Test | |||
public void contextLoads() { | |||
} | |||
} |
@@ -7,18 +7,21 @@ | |||
<groupId>com.xkcoding</groupId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
</parent> | |||
<modelVersion>4.0.0</modelVersion> | |||
<artifactId>demo-admin</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
<packaging>pom</packaging> | |||
<properties> | |||
<spring-boot-admin.version>2.1.0</spring-boot-admin.version> | |||
<java.version>17</java.version> | |||
<spring-boot-admin.version>3.0.0-M4</spring-boot-admin.version> | |||
</properties> | |||
<modules> | |||
<module>admin-client</module> | |||
<module>admin-server</module> | |||
<module>admin-client</module> | |||
</modules> | |||
<dependencyManagement> | |||
@@ -13,7 +13,7 @@ | |||
<module>demo-helloworld</module> | |||
<module>demo-properties</module> | |||
<module>demo-actuator</module> | |||
<!-- <module>demo-admin</module>--> | |||
<module>demo-admin</module> | |||
<!-- <module>demo-logback</module>--> | |||
<!-- <module>demo-log-aop</module>--> | |||
<!-- <module>demo-exception-handler</module>--> | |||