springboot-admin使用

springboot admin server服务

官方文档
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

这个开启下,在控制台方便查看trace等信息。
这里注释掉的是权限信息。

  • bootstrap.java
    启动类
@Configuration
@EnableAutoConfiguration
@SpringBootApplication
@EnableAdminServer

服务启动后,会看到如下

屏幕快照 2017-08-19 下午4.08.03.png

这个时候看到服务已经启动了。

admin client

adminclient也是一个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

这里废话不多说,直接贴代码。大家自己试下就行了。

有个注意的点是:client的版本的问题,如果不一致,可能会导致注册不上去,如下保证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>

服务启动后,可以看到,springboot的client已经注册上去了:

屏幕快照 2017-08-19 下午4.15.20.png

当然,这里也可以用eureka或者consul来做服务的注册和发现。即client和server通过eureka或者consul来进行服务的注册和发现。因为两者都是springboot的web项目。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容