ITPub博客

首页 > 架构设计 > 软件结构 > java分布式微服务云架构- SrpingBoot Admin

java分布式微服务云架构- SrpingBoot Admin

原创 软件结构 作者:gung123 时间:2020-03-10 18:24:21 0 删除 编辑

一、SrpingBoot Admin 介绍

Spring Boot Admin 是一个开源社区项目,用于管理和监控 SpringBoot 应用程序,展示Spring Boot Admin Client 的 Actuator 端点上的一些监控信息。

了解springcloud架构可以加求求:三五三六二四七二五九

它为应用程序提供以下功能:


显示应用健康状况

关注并下载日志文件

查看jvm系统和环境属性

查看Spring Boot配置属性

支持Spring Cloud的postable / env-和/ refresh-endpoint

轻松的日志级管理


二、SrpingCloud Kubernetes 介绍

Spring Cloud Kubernetes 提供 Kubernetes 环境下服务发现的 Spring Cloud 通用接口实现。主要目的是促进在 Kubernetes 中运行的 Spring Cloud 和 Spring Boot 应用程序的集成。


这里我们主要用 SpringCloud Kubernetes 来为 SpringBoot Admin 提供 Kubernetes 环境下的服务发现。

三、创建 SpringBoot Admin 应用

创建 SpringBoot Admin 应用,且引入 SpringCloud Kubernetes 作为服务发现。


Maven 引入相关依赖

在 Maven 中引入 “spring-boot-admin-starter-server” 与 “spring-cloud-kubernetes-discovery” 依赖。

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>club.mydlq</groupId>
    <artifactId>springboot-admin-k8s</artifactId>
    <version>0.0.2</version>
    <name>springboot-admin-k8s</name>
    <description>demo</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--SpringBoot Admin-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.1.5</version>
        </dependency>
        <!--SpringCloud Kubernetes-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-kubernetes-discovery</artifactId>
            <version>1.0.2.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

配置 application 文件

加上两个参数:

  • spring.cloud.kubernetes.discovery.primaryPortName:
  • spring.cloud.kubernetes.discovery.serviceLabels:
server:
  port: 8080
  
management:
  server:
    port: 8081                          #---指定监控数据端口为8081,避免和 server.port 一致产生风险
  endpoints:
    web:
      exposure:
        include: "*"
spring:
  application:
    name: springboot-admin-k8s
  cloud:
    kubernetes:
      discovery:
        primaryPortName: management     #---按设要监控 Service 的端口名称
        serviceLabels:
          admin: enabled                #---设置要监控 Service 的 Label 标签

启动类

需要加上四个注解:


@SpringBootApplication:开启 SpringBoot 相关注解,会自动配置相关信息。

@EnableDiscoveryClient:开启 Spring服务发现机制。

@EnableAdminServer:开启 SpringBoot Admin。

@EnableScheduling:开启定时任务,不加此注解服务发现不会执行定时刷新。

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;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69952307/viewspace-2679481/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-11-01

  • 博文量
    200
  • 访问量
    79619