springboot-admin使用

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ù)啟動后,會看到如下

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

這個(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)注冊上去了:

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

當(dāng)然,這里也可以用eureka或者consul來做服務(wù)的注冊和發(fā)現(xiàn)。即client和server通過eureka或者consul來進(jìn)行服務(wù)的注冊和發(fā)現(xiàn)。因?yàn)閮烧叨际莝pringboot的web項(xiàng)目。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容