前言

上一节为springboot项目添加springboot-admin监控 学习了基于springboot1.5自己注册到admin的方法。接下来学习结合Eureka使用以及2.0的改变。

1.5spring-boot-admin集成eureka

我们继续上一节的项目修改,admin-server依赖修改如下

<dependencies><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-server-ui</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency>
</dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>   

修改启动类,添加

@EnableDiscoveryClient
@EnableAdminServer

修改配置文件application.properties

server.port=8081
spring.application.name=admin-server
eureka.client.serviceUrl.defaultZone=${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management.security.enabled=false

注意,这里的eureka server要提前启动。具体细节,参见SpringCloud入门1-服务注册与发现(Eureka)

到这里就结束,可以直接启动。admin会自己拉取Eureka上注册的app信息,主动去注册。这也是唯一区别之前手动注册的地方,就是client端不需要admin-client的依赖,也不需要配置admin地址了,一切全部由admin-server自己实现。这样的设计对环境变化很友好,不用改了admin-server后去改所有app的配置了。

spring-boot-admin2.0的巨变

以为2.0比1.5区别不大,也确实不很大。关于client主动注册的部分没有变化。

这里,重新学习一遍,并添加上安全登录功能。

新建一个springboot项目

项目地址:

https://github.com/Ryan-Miao/spring-cloud-demo/tree/master/admin-server

添加spring-boot-admin-server, 最终pom依赖如下


<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-boot-admin.version>2.0.0</spring-boot-admin.version><spring-cloud.version>Finchley.RC2</spring-cloud.version></properties><dependencies><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId></dependency><!--还没有发布--><!--<dependency>--><!--<groupId>de.codecentric</groupId>--><!--<artifactId>spring-boot-admin-server-cloud</artifactId>--><!--</dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-dependencies</artifactId><version>${spring-boot-admin.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><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><repositories><repository><id>central</id><url>http://maven.aliyun.com/nexus/content/groups/public/</url><name>aliyun</name></repository><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>

注意到里面添加spring cloud eureka的依赖,我们也把admin给注册到eureka。

这次的配置文件比较复杂

spring:application:name: admin-serverprofiles:active:- secure# tag::configuration-eureka[]
eureka:   #<1>instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: /actuator/health #2.0后actuator的地址发生了变化client:registryFetchIntervalSeconds: 5serviceUrl:defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/# 2.0开始,actuator默认不开放,所以要设置为开放
management:endpoints:web:exposure:include: "*"  #<2>endpoint:health:show-details: ALWAYS
server:port: 8081
# end::configuration-eureka[]---
spring:profiles: insecure---
# admin登录的用户名和密码
spring:profiles: securesecurity:user:name: "user"password: "password"# 注册给eureka的时候告诉eureka自己的密码
eureka:instance:metadata-map:"user.name": ${spring.security.user.name}         #These two are needed so that the server"user.password": ${spring.security.user.password} #can access the protected client endpoints

首先,我们同时支持两种安全配置,一种没有安全认证,一种有。注意到前面的依赖pom里有security的依赖。针对security方案,需要配置用户名和密码。

其次,配置了eureka client相关参数,把自己注册到eureka里。

然后,关于actuator的端点接口,设置为全部开放。

最后,设置我们的用户名和密码,并把用户名和密码放到eureka里,方便识别。

配置security的安全过滤

最终的启动类如下


@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}@Profile("insecure")@Configurationpublic static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().permitAll()//.and().csrf().disable();}}@Profile("secure")@Configurationpublic static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {private final String adminContextPath;public SecuritySecureConfig(AdminServerProperties adminServerProperties) {this.adminContextPath = adminServerProperties.getContextPath();}@Overrideprotected void configure(HttpSecurity http) throws Exception {// @formatter:offSavedRequestAwareAuthenticationSuccessHandler 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();// @formatter:on}}
}

和之前没啥不同,唯一的区别是添加了安全认证相关的,即采用secure模式的时候必须输入用户名和密码。

启动

我们启动并选择激活配置环境secure

访问localhost:8081

登录

详细页

界面确实比1.5好很多啊。

新建一个app来注册

依旧采用手动注册的方式,新建一个springboot项目,项目地址

https://github.com/Ryan-Miao/spring-cloud-demo/tree/master/provider-demo

首先,pom依赖要有eureka

<?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><groupId>com.test</groupId><artifactId>provider-demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>provider-demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.RC2</spring-cloud.version><spring-boot-admin.version>2.0.0</spring-boot-admin.version></properties><dependencies><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-security</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-dependencies</artifactId><version>${spring-boot-admin.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><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><repositories><repository><id>central</id><url>http://maven.aliyun.com/nexus/content/groups/public/</url><name>aliyun</name></repository><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

注意,配置了eureka,用来注册。

依赖springfox-swagger2用来展示API。

spring-boot-admin-starter-client才是主动注册的核心库,用来发送注册申请。

需要再次强调了是build

  <build><plugins><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>

这里是为了生成版本信息,提供给spring-boot-admin展示。

provider-demo的配置文件

eureka:instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: /actuator/healthclient:registryFetchIntervalSeconds: 5serviceUrl:defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/management:endpoints:web:exposure:include: "*"endpoint:health:show-details: ALWAYSserver:port: 8082spring:application:name: provider-demoboot:admin:client:url: "http://localhost:8081"password: passwordusername: userinstance:prefer-ip: true

eureka的配置和admin-server相同,毕竟都是eureka的client嘛。不同的是这里没有开启spting security,所以不添加用户名和密码信息到meta-data里。

actuator同样需要暴露全部。

关于spring-boot-admin-client的注册配置里多了username和password,这个不是自己的,而是admin-server设置的,即前文的里的。

启动后就可以看到前面的详细信息了。

巨变在这里

在1.5版本里,我们只要加上eureka注册,就可以admin-server自动发现所有的app并自己注册监控了。但这里居然行不通了。我调试了几个小时,把依赖包翻来覆去研究了好多遍----就是不行!!!

无奈,只好去github把spring-boot-admin的源码下载下来,找到注册的代码开始追踪。我发现,根本没有提供任何和eureka注册中心相关的代码。看到了spring-boot-admin-server-cloud, 进去看了源码,这里有关于注册中心的监听代码,也就是admin自动发现app的密码所在。那我把这个加入dependency不就好了吗,太年轻。加上后发现找不到版本。我说不应该啊,我有导入dependencyManagement的pom啊。手动指定版本也不行。最后无奈的发现,原来根本没有发布!!!

⚠️Note: Since Spring Cloud Finchley is not released yet, this version doesn't include Spring Cloud Discovery support

所以,手动注册吧,骚年。不然你再等等。

结语

通过这一下午的尝试,追踪源码,我发现自己确实挺乐在其中的。然而,这花费了大量的时间和精力。所以,我花费了更多的时间一定要把这次经历记录下来,也就是本文了。想说的是,最新的版本果然是坑巨多,慎入!!!如果是作为生产环境的选型,必须跳过这样的版本。当然,如果是自己个人研究,看新的没错。

那么,我到底应不应该学习2.0呢,毕竟1.5也没学完呢。

学吧,按2.0的学习,工作中还是用1.5. 嗯,就这样吧。

Miao语

没有解决不了的bug,只要你用心研究,都可以解决。

参考

  • Spring-boot-admin-server-cloud没有发布的肯人issue
  • Spring-boot-admin-server2.0发布Note
  • Spring-boot-admin官方文档

SpringCloud2.0入门4-springboot-admin监控相关推荐

  1. 使用SpringBoot Admin监控SpringCloud微服务

    原文:https://www.cnblogs.com/yangzhilong/p/9378876.html spring-boot admin的github地址:https://github.com/ ...

  2. springboot 创建地址_使用 SpringBoot Admin监控Spring Boot 服务

    简介 SpringBoot-Amind是什么?Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件.,可监控的信息包含:应用状态.内存.线程.堆栈等等,比较 ...

  3. 玩转SpringBoot 2.x 之搭建 Actuator 和 SpringBoot Admin监控篇

    1 搭建SpringBoot Admin 服务端 创建SpringBoot 项目并引入SpringBoot Admin 服务端的依赖. <dependency><groupId> ...

  4. 使用 SpringBoot Admin 监控你的 SpringBoot 程序

    1.Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 Sp ...

  5. springboot日志可视化_使用 SpringBoot Admin 监控你的 SpringBoot 程序

    1.Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 Sp ...

  6. SpringCloud2.0入门3-新的eureka依赖

    前言 Springboot2.0推出有一段时间了,是要学习1.5+还是从2.0开始?犹豫的原因是资料不全,目前现有的资料大部分是1.0的.但作为学习者,肯定要学习最新的.不如,先试试. 搭建Eurek ...

  7. SpringBoot实战(十二):集成 Spring Boot Admin 监控

    强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan [前言] 程序开发完实现相应的功能只是一个部分,如何让系统在线上运行更好创造更高的价值是另外一个 ...

  8. SpringBoot2.X监控和管理神器:SpringBoot Admin

    前言 我们在使用SpringBoot的时候,特别的敏捷与方便,开发起来特别的快.比如加入spring-boot-starter-web这个启动器,就能开发JavaWeb项目:我们再加入mybatis- ...

  9. Admin监控Sleuth链路追踪 skywalking链路追踪

    Admin监控&Sleuth链路追踪,skywalking **Sleuth&Zipkin** 一.Sleuth&Zipkin介绍 二.搭建环境 三.Sleuth入门操作 四. ...

最新文章

  1. 前端最佳实践之可维护性
  2. php 析构不执行,PHP析构方法 __destruct() 不触发的两个解决办法
  3. H5网站接入支付宝的支付接口
  4. 【每周CV论文推荐】 初学深度学习图像分割必须要读的文章
  5. hdu 1166 敌兵布阵(线段树之 单点更新+区间求和)
  6. matlab梯度检测,Matlab:关于梯度的一阶导数边缘检测
  7. vue系列(1)安装vue
  8. Centos7 CMake升级
  9. 我的新书,《人人都是产品经理》自序
  10. windows phone 学习(4)
  11. Kubernetes—如何批量删除对象资源?(二十三)
  12. ninjala服务器维护,Ninjala2.0版本更新内容一览
  13. 靠谱测试人员需具备逻辑思考能力
  14. ElasticSearch 相关性
  15. 解决序列长期依赖的法宝——注意力机制
  16. 计算机里面的固态硬盘,怎么判断电脑里面的是不是固态硬盘?
  17. 前字节跳动程序员 28 岁提前退休引热议,网友:我也想
  18. MapX学习基本教程
  19. 惠普 ProBook 笔记本下的 WIN10 解决 VMware 开启Intel VT-x问题
  20. 人机界面和组态软件有什么区别?

热门文章

  1. ZZ: How to remove 'Open in Windows Explorer' from the 'Actions Menu'
  2. DataSet写入Excel
  3. GIS可视性分析概述
  4. NC命令行作为服务端、客户端以及win32 套接字程序的简单连接测试
  5. 微信消息类型和事件类型
  6. 《区块链100问》笔记整理——23~41问
  7. 即时通讯 TCP UDP
  8. RabbitMQ 记录
  9. Ninject(二)——Modules和Kernel
  10. mysql服务器端口cpu_mysql导致服务器cpu100%的问题一例