springboot admin server服務(wù)
官方文檔
Spring Boot Admin is a application to manage and monitor your Spring Boot Applications. The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud (e.g. Eureka). The UI is just an AngularJs application on top of the Spring Boot Actuator endpoints.
admin server
- pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
注意client和server的版本,最好保持一致。
- application.yml
server:
port: 8081
management:
context-path: "/actuator"
security:
enabled: false
security:
basic:
enabled: false
spring:
application:
name: "@pom.artifactId@"
boot:
admin:
url: http://localhost:${server.port}
# username: "${security.user.name}" #These two are needed so that the client
# password: "${security.user.password}" #can register at the protected server api
# client:
# metadata:
# user.name: "${security.user.name}" #These two are needed so that the server
# user.password: "${security.user.password}" #can access the proteceted client endpoints
jackson:
serialization:
indent_output: true
endpoints:
health:
sensitive: false
#security:
# user:
# name: user
# password: pass
這里說明下:
management:
context-path: "/actuator"
security:
enabled: false
這個(gè)開啟下,在控制臺方便查看trace等信息。
這里注釋掉的是權(quán)限信息。
- bootstrap.java
啟動類
@Configuration
@EnableAutoConfiguration
@SpringBootApplication
@EnableAdminServer
服務(wù)啟動后,會看到如下
這個(gè)時(shí)候看到服務(wù)已經(jīng)啟動了。
admin client
adminclient也是一個(gè)springboot工程
- application.yml文件
spring:
application:
name: bootadmin-client
boot:
admin:
url: http://localhost:8081
client:
name: ${spring.application.name}
# enabled: true
# metadata:
# user.name: ${security.user.name}
# user.password: ${security.user.password}
jackson:
serialization:
indent_output: true
server:
port: 8002
management:
context-path: "/actuator"
security:
enabled: false
security:
basic:
enabled: false
#
#security:
# user:
# name: user
# password: pass
這里廢話不多說,直接貼代碼。大家自己試下就行了。
有個(gè)注意的點(diǎn)是:client的版本的問題,如果不一致,可能會導(dǎo)致注冊不上去,如下保證client和server的client的版本一致。
- pom.xml
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
服務(wù)啟動后,可以看到,springboot的client已經(jīng)注冊上去了:
當(dāng)然,這里也可以用eureka或者consul來做服務(wù)的注冊和發(fā)現(xiàn)。即client和server通過eureka或者consul來進(jìn)行服務(wù)的注冊和發(fā)現(xiàn)。因?yàn)閮烧叨际莝pringboot的web項(xiàng)目。