前言
目前有一个 spring cloud 项目,查看日志需要到服务器上去反复拉取下来查看,多个服务之间查看起来非常不方便,所以想使 日志查看起来更加方便一点,原本想 搭建 ELK ,但是对服务器资源要求更多一点,所以选择使用个简单版的集成 spring boot admin 的方式来实现,既可看到日志,又可监控服务的信息。
实现步骤
adminServer 服务
POM 文件中增加配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.0.4</version> </dependency>
AdminServerApplication 代码
import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableAdminServer @EnableDiscoveryClient public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } } application.yml 配置 ```yaml spring: profiles: active: dev include: zSba,zLog # 配置访问信息 --- spring: security: user: name: admin password: tzkj@wx... eureka: # 配置 spring security 的用户名和密码,这时需要在服务注册时带上 metadata-map 的信息。 metadata-map: user: name: ${spring.security.user.name} password: ${spring.security.user.password} </code></pre> <pre><code class="line-numbers">application-dev.yml 配置 ```yaml spring: application: name: admin-server server: port: 8999 servlet: context-path: /adminServer # 注册中心地址 eureka: client: serviceUrl: defaultZone: http://localhost:8661/eureka/ instance: instance-id: ${spring.application.name}:${server.port} # 配置使用主机名注册服务 hostname: localhost # 优先使用IP地址方式进行注册服务 prefer-ip-address: true # 配置使用指定IP ip-address: localhost
application-zSba.yml 配置
############################## Spring boot admin start ############################## eureka: instance: # admin 配置 home-page-url-path: ${server.servlet.context-path} health-check-url-path: ${server.servlet.context-path}/actuator/health status-page-url-path: ${server.servlet.context-path}/actuator/info metadata-map: management: context-path: ${server.servlet.context-path}/actuator # 开启和暴露所有端点 management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS logfile: enabled: true
需要被监控的服务
POM 文件增加依赖
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.4</version> </dependency>
application-zSba.yml 配置 同上
即可
绿色六边形则为已连接上服务,点击进入后进入控制页面,即可看到 服务的信息 以及 日志等。
以上