springbootadmin+eureka实现服务监控

最近线上项目上的比较多,而之前并没有使用微服务的架构,所以监控就是一个大问题,经常需要登陆服务器进行操作,突然想到使用springbootadmin去做一个简易的服务监控,期间也走了很多弯路 ,这里做一下记录。


首先架构分析:

1、spring-boot-eureka:注册中心,被监控的服务以及admin-server都需要注册到eureka

2、spring-boot-admin-server:监控服务端

3、被监控服务

之前没有想清楚忽略了eureka的作用,所以浪费了一些时间,不得不说菜的一批。

这里是用的是spring-cloud的F版的release版本。以及spring-boot-admin2.1.3


eureka配置:

这里我们只需要一个非常简单的eureka注册中心就可以了。

如何创建一个spring-cloud-eureka工程这里就不赘述了,网上这样的资源太多啦,直接说配置吧。

spring.application.name=eureka-server
server.port=8001

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false  #是否注册到eureka
eureka.client.fetch-registry=false

启动访问:http://localhost:8001

admin-server配置:

gradle依赖:

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.str'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

ext {
    set('springBootAdminVersion', '2.1.3')
    set('springCloudVersion', 'Finchley.RELEASE')
}

dependencies {
    implementation 'de.codecentric:spring-boot-admin-starter-server'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    // https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-server-ui
    compile group: 'de.codecentric', name: 'spring-boot-admin-server-ui', version: '2.1.3'

}

dependencyManagement {
    imports {
        mavenBom "de.codecentric:spring-boot-admin-dependencies:${springBootAdminVersion}"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

配置文件:


spring.application.name=admin-server
eureka.client.service-url.defaultZone= http://54.223.207.217:8001/eureka/

server.port= 8000

启动访问:http://localhost:8000

 

如上所示已经可以看到自己作为一个服务已经显示在面板上了。

被监控服务配置:

gradle依赖:

    compile('org.springframework.boot:spring-boot-starter-actuator')
    // https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client
    compile group: 'de.codecentric', name: 'spring-boot-admin-starter-client', version: '2.1.3'
    // https://mvnrepository.com/artifact/org.jolokia/jolokia-core
    compile group: 'org.jolokia', name: 'jolokia-core', version: '1.6.0'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'


    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('com.jayway.jsonpath:json-path')
……
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

配置文件:

spring.application.name: xxxx
eureka.client.service-url.defaultZone=http://54.223.207.217:8001/eureka/
management.endpoints.web.exposure.include=refresh,health,info  #暴漏端口
management.endpoint.health.show-details= ALWAYS

此时启动服务,再次访问localhost:8001会发现该服务已经在面板中显示,但是如果你的服务中使用了security做权限认证管理,那么应用状态会编程down,点击Journal会看到log显示detail为401为认证。这是因为security拦截了监控服务器发出的请求,而这些请求都是以actuator为前缀的,知道了这一点,我们就可以通过配置security请求白名单来实现这个需求。

httpSecurity.antMatchers("/actuator/*").permitAll()

这个问题当初也困扰了我很久。


其他细节:

1、如果你的eureka和admin-server在服务器上,本地启动服务时,监控服务状态为offline,journal显示detail为unkownhostException,这个在服务上线了之后就好了。

2、在我第一次部署eureka到服务器时,导致我的阿里云服务器cpu直接卡死了(还好是测试服务器),后来觉得可能是分配的jvm控件太小了(-xms10m -xmx200m),我调整为-xms512m -xmx512m之后就没问题了。希望大家也注意下,注册中心给的空间不要太小。

3、以上是本人实践所得,如果有问题请大家留言一起探讨,菜鸡给你喊666。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值