浏览代码

[monitor]服务端编译打包脚本

tomsun28 4 年之前
父节点
当前提交
faf5bc7b87

+ 0 - 37
RequestDemo.json

@@ -1,37 +0,0 @@
-{
-  "detected": true,
-  "monitor": {
-    "app": "TanCloud",
-    "creator": "tom",
-    "description": "对SAAS网站TanCloud的可用性监控",
-    "gmtCreate": "2021-11-18T14:18:11.968Z",
-    "gmtUpdate": "2021-11-18T14:18:11.968Z",
-    "host": "139.198.109.64",
-    "id": 87584674384,
-    "intervals": 600,
-    "jobId": 43243543543,
-    "modifier": "tom",
-    "name": "Api-TanCloud.cn",
-    "status": 1
-  },
-  "params": [
-    {
-      "field": "port",
-      "gmtCreate": "2021-11-18T14:18:11.968Z",
-      "gmtUpdate": "2021-11-18T14:18:11.968Z",
-      "id": 87584674384,
-      "monitorId": 875846754543,
-      "type": 1,
-      "value": "8088"
-    },
-    {
-      "field": "host",
-      "gmtCreate": "2021-11-18T14:18:11.968Z",
-      "gmtUpdate": "2021-11-18T14:18:11.968Z",
-      "id": 87584674384,
-      "monitorId": 875846754543,
-      "type": 1,
-      "value": "139.198.109.64"
-    }
-  ]
-}

+ 5 - 4
alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java

@@ -75,8 +75,8 @@ public class CalculateAlarm {
                 // 采集异常
                 Alert.AlertBuilder alertBuilder = Alert.builder()
                         .monitorId(monitorId)
-                        .priority((byte) 0)
-                        .status((byte) 0)
+                        .priority(CommonConstants.ALERT_PRIORITY_CODE_EMERGENCY)
+                        .status(CommonConstants.ALERT_STATUS_CODE_PENDING)
                         .times(1);
                 if (metricsData.getCode() == CollectRep.Code.UN_REACHABLE) {
                     // UN_REACHABLE 对端不可达(网络层icmp)
@@ -101,7 +101,8 @@ public class CalculateAlarm {
                 if (stateCode != null) {
                     // 发送告警恢复
                     Alert resumeAlert = Alert.builder()
-                            .monitorId(monitorId).status((byte) 2).build();
+                            .monitorId(monitorId)
+                            .status(CommonConstants.ALERT_STATUS_CODE_RESTORED).build();
                     dataQueue.addAlertData(resumeAlert);
                 }
             }
@@ -167,7 +168,7 @@ public class CalculateAlarm {
                                             .monitorId(monitorId)
                                             .alertDefineId(define.getId())
                                             .priority(define.getPriority())
-                                            .status((byte) 0)
+                                            .status(CommonConstants.ALERT_STATUS_CODE_PENDING)
                                             .target(app + "." + metrics + "." + define.getField())
                                             .times(times)
                                             // 模板中关键字匹配替换

+ 65 - 0
assembly/server/assembly.xml

@@ -0,0 +1,65 @@
+<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd
+http://maven.apache.org/ASSEMBLY/2.0.0 ">
+    <!--必填,会追加到打包文件名称的末尾-->
+    <id>1.0</id>
+    <!--打包类型,可以设置多种类型,打包的时候不同的类型都会打包打出来-->
+    <formats>
+        <format>tar.gz</format>
+        <format>zip</format>
+    </formats>
+    <!--第三方依赖设置-->
+    <dependencySets>
+        <dependencySet>
+            <!--使用项目中的artifact,第三方包打包进tar.gz文件的lib目录下-->
+            <useProjectArtifact>true</useProjectArtifact>
+            <outputDirectory>lib</outputDirectory>
+        </dependencySet>
+    </dependencySets>
+
+    <!--文件相关设置-->
+    <fileSets>
+        <!--bin文件下的所有脚本文件输出到打包后的bin目录下-->
+        <fileSet>
+            <directory>../assembly/server/bin</directory>
+            <outputDirectory>bin</outputDirectory>
+        </fileSet>
+
+        <!-- src/main/resources目录下配置文件打包到config目录下 -->
+        <fileSet>
+            <directory>src/main/resources</directory>
+            <includes>
+                <include>application.yml</include>
+                <include>logback-spring.xml</include>
+            </includes>
+            <filtered>true</filtered>
+            <outputDirectory>${file.separator}config</outputDirectory>
+        </fileSet>
+
+        <!-- src/main/resources/define目录下配置文件打包到define目录下 -->
+        <fileSet>
+            <directory>src/main/resources</directory>
+            <includes>
+                <include>define/**</include>
+            </includes>
+            <filtered>true</filtered>
+            <outputDirectory>${file.separator}</outputDirectory>
+        </fileSet>
+
+        <!-- 将target目录下的启动jar打包到目录下-->
+        <fileSet>
+            <directory>target</directory>
+            <outputDirectory>/</outputDirectory>
+            <includes>
+                <include>*.jar</include>
+            </includes>
+        </fileSet>
+
+        <!-- 将webapp的静态资源打包到目录下-->
+        <fileSet>
+            <directory>../web-app/dist</directory>
+            <outputDirectory>dist</outputDirectory>
+        </fileSet>
+    </fileSets>
+</assembly>

+ 0 - 0
assembly/server/bin/shutdown.sh


+ 0 - 0
assembly/server/bin/startup.sh


+ 34 - 0
common/src/main/java/com/usthe/common/util/CommonConstants.java

@@ -62,6 +62,40 @@ public interface CommonConstants {
      */
     byte SUSPENDING_CODE = 0x04;
 
+    /**
+     * 告警状态: 0-正常告警(待处理)
+     */
+    byte ALERT_STATUS_CODE_PENDING = 0x00;
+
+    /**
+     * 告警状态: 1-阈值触发但未达到告警次数
+     */
+    byte ALERT_STATUS_CODE_NOT_REACH = 0x01;
+
+    /**
+     * 告警状态: 2-恢复告警
+     */
+    byte ALERT_STATUS_CODE_RESTORED = 0x02;
+
+    /**
+     * 告警状态: 3-已处理
+     */
+    byte ALERT_STATUS_CODE_SOLVED = 0x03;
+
+    /**
+     * 告警级别: 0:高-emergency-紧急告警-红色
+     */
+    byte ALERT_PRIORITY_CODE_EMERGENCY = 0x00;
+
+    /**
+     * 告警级别: 1:中-critical-严重告警-橙色
+     */
+    byte ALERT_PRIORITY_CODE_CRITICAL = 0x01;
+
+    /**
+     * 告警级别: 2:低-warning-警告告警-黄色
+     */
+    byte ALERT_PRIORITY_CODE_WARNING = 0x02;
 
     /**
      * 字段参数类型: 数字

+ 51 - 4
manager/pom.xml

@@ -113,16 +113,63 @@
     </dependencies>
 
     <build>
+        <finalName>hertz-beat</finalName>
+        <resources>
+            <resource>
+                <directory>src/main/resources</directory>
+                <filtering>true</filtering>
+                <includes>
+                    <include>application.yml</include>
+                    <include>sureness.yml</include>
+                    <include>banner.txt</include>
+                    <include>db/**</include>
+                </includes>
+            </resource>
+        </resources>
         <plugins>
             <plugin>
-                <groupId>org.springframework.boot</groupId>
-                <artifactId>spring-boot-maven-plugin</artifactId>
-                <version>2.4.13</version>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <version>3.2.0</version>
+                <configuration>
+                    <classesDirectory>target/classes/</classesDirectory>
+                    <archive>
+                        <!--生成的jar包不包含maven描述相关文件-->
+                        <addMavenDescriptor>false</addMavenDescriptor>
+                        <manifest>
+                            <!--项目启动类-->
+                            <mainClass>com.usthe.manager.Manager</mainClass>
+                            <useUniqueVersions>false</useUniqueVersions>
+                            <!--第三方JAR加入类构建的路径maven-dependency-plugin-->
+                            <addClasspath>true</addClasspath>
+                            <!--外部依赖jar包的位置-->
+                            <classpathPrefix>lib/</classpathPrefix>
+                        </manifest>
+                        <manifestEntries>
+                            <Class-Path>. config</Class-Path>
+                        </manifestEntries>
+                    </archive>
+                </configuration>
+            </plugin>
+            <!--关键插件,maven提供的assembly插件,需要放在最后-->
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-assembly-plugin</artifactId>
+                <version>3.3.0</version>
                 <executions>
                     <execution>
+                        <id>make-zip</id>
+                        <!--绑定的maven操作-->
+                        <phase>package</phase>
+                        <!--运行一次-->
                         <goals>
-                            <goal>repackage</goal>
+                            <goal>single</goal>
                         </goals>
+                        <configuration>
+                            <descriptors>
+                                <descriptor>../assembly/server/assembly.xml</descriptor>
+                            </descriptors>
+                        </configuration>
                     </execution>
                 </executions>
             </plugin>

+ 1 - 1
manager/src/main/java/com/usthe/manager/component/alerter/DispatchAlarm.java

@@ -94,7 +94,7 @@ public class DispatchAlarm {
             }
         } else {
             // 若是恢复告警 需对监控状态进行恢复
-           if (alert.getStatus() == 2) {
+           if (alert.getStatus() == CommonConstants.ALERT_STATUS_CODE_RESTORED) {
                monitorService.updateMonitorStatus(alert.getMonitorId(), CommonConstants.AVAILABLE_CODE);
            }
         }

+ 128 - 0
manager/src/main/java/com/usthe/manager/config/AngularErrorViewResolver.java

@@ -0,0 +1,128 @@
+package com.usthe.manager.config;
+
+import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
+import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders;
+import org.springframework.boot.autoconfigure.web.WebProperties;
+import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.Ordered;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.util.Assert;
+import org.springframework.util.FileCopyUtils;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.View;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Collections;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * 同DefaultErrorViewResolver 解决angular静态网站资源前端路由问题 把404的网站请求给angular前端进行路由
+ * @author tom
+ * @date 2021/12/24 21:07
+ */
+@Configuration
+public class AngularErrorViewResolver implements ErrorViewResolver, Ordered {
+
+    private static final Map<HttpStatus.Series, String> SERIES_VIEWS;
+    private static final String NOT_FOUND_CODE = "404";
+
+    static {
+        Map<HttpStatus.Series, String> views = new EnumMap<>(HttpStatus.Series.class);
+        views.put(HttpStatus.Series.CLIENT_ERROR, "4xx");
+        views.put(HttpStatus.Series.SERVER_ERROR, "5xx");
+        SERIES_VIEWS = Collections.unmodifiableMap(views);
+    }
+
+    private ApplicationContext applicationContext;
+
+    private final WebProperties.Resources resources;
+
+    private final TemplateAvailabilityProviders templateAvailabilityProviders;
+
+    private int order = Ordered.LOWEST_PRECEDENCE;
+
+    public AngularErrorViewResolver(ApplicationContext applicationContext, WebProperties.Resources resources) {
+        Assert.notNull(applicationContext, "ApplicationContext must not be null");
+        Assert.notNull(resources, "Resources must not be null");
+        this.applicationContext = applicationContext;
+        this.resources = resources;
+        this.templateAvailabilityProviders = new TemplateAvailabilityProviders(applicationContext);
+    }
+
+    @Override
+    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
+        ModelAndView modelAndView = resolve(String.valueOf(status.value()), model);
+        if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) {
+            modelAndView = resolve(SERIES_VIEWS.get(status.series()), model);
+        }
+        return modelAndView;
+    }
+
+    private ModelAndView resolve(String viewName, Map<String, Object> model) {
+        String errorViewName = "error/" + viewName;
+        if (NOT_FOUND_CODE.equals(viewName)) {
+            errorViewName = "index";
+        }
+        TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName,
+                this.applicationContext);
+        if (provider != null) {
+            return new ModelAndView(errorViewName, model);
+        }
+        return resolveResource(errorViewName, model);
+    }
+
+    private ModelAndView resolveResource(String viewName, Map<String, Object> model) {
+        for (String location : this.resources.getStaticLocations()) {
+            try {
+                Resource resource = this.applicationContext.getResource(location);
+                resource = resource.createRelative(viewName + ".html");
+                if (resource.exists()) {
+                    return new ModelAndView(new HtmlResourceView(resource), model);
+                }
+            }
+            catch (Exception ex) {
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public int getOrder() {
+        return this.order;
+    }
+
+    public void setOrder(int order) {
+        this.order = order;
+    }
+
+    /**
+     * {@link View} backed by an HTML resource.
+     */
+    private static class HtmlResourceView implements View {
+
+        private Resource resource;
+
+        HtmlResourceView(Resource resource) {
+            this.resource = resource;
+        }
+
+        @Override
+        public String getContentType() {
+            return MediaType.TEXT_HTML_VALUE;
+        }
+
+        @Override
+        public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
+                throws Exception {
+            response.setContentType(getContentType());
+            FileCopyUtils.copy(this.resource.getInputStream(), response.getOutputStream());
+        }
+
+    }
+}

+ 7 - 12
manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java

@@ -8,7 +8,6 @@ import com.usthe.manager.pojo.dto.ParamDefineDto;
 import com.usthe.manager.pojo.entity.ParamDefine;
 import com.usthe.manager.service.AppService;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.common.protocol.types.Field;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.stereotype.Service;
@@ -18,7 +17,6 @@ import org.yaml.snakeyaml.Yaml;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
-import java.net.URL;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -123,12 +121,11 @@ public class AppServiceImpl implements AppService, CommandLineRunner {
     public void run(String... args) throws Exception {
         // 读取app定义配置加载到内存中 define/app/*.yml
         Yaml yaml = new Yaml();
-        String defineAppPath = "define" + File.separator + "app";
-        URL url = Thread.currentThread().getContextClassLoader().getResource(defineAppPath);
-        assert url != null;
-        File directory = new File(url.toURI());
+        String classpath = this.getClass().getResource(File.separator).getPath();
+        String defineAppPath = classpath + File.separator + "define" + File.separator + "app";
+        File directory = new File(defineAppPath);
         if (!directory.exists() || directory.listFiles() == null) {
-            throw new  IllegalArgumentException("define app directory not exist");
+            throw new  IllegalArgumentException("define app directory not exist: " + defineAppPath);
         }
         for (File appFile : Objects.requireNonNull(directory.listFiles())) {
             if (appFile.exists()) {
@@ -142,12 +139,10 @@ public class AppServiceImpl implements AppService, CommandLineRunner {
             }
         }
         // 读取监控参数定义配置加载到数据库中 define/param/*.yml
-        String defineParamPath = "define" + File.separator + "param";
-        url = Thread.currentThread().getContextClassLoader().getResource(defineParamPath);
-        assert url != null;
-        directory = new File(url.toURI());
+        String defineParamPath = classpath + File.separator + "define" + File.separator + "param";
+        directory = new File(defineParamPath);
         if (!directory.exists() || directory.listFiles() == null) {
-            throw new  IllegalArgumentException("define param directory not exist");
+            throw new  IllegalArgumentException("define param directory not exist: " + defineParamPath);
         }
         for (File appFile : Objects.requireNonNull(directory.listFiles())) {
             if (appFile.exists()) {

+ 4 - 1
manager/src/main/resources/sureness.yml

@@ -12,6 +12,8 @@ resourceRole:
 excludedResource:
   - /account/auth/form===post
   - /i18n/**===get
+  # web ui 静态资源
+  - /console/**===get
   - /**/*.html===get
   - /**/*.js===get
   - /**/*.css===get
@@ -19,10 +21,11 @@ excludedResource:
   - /**/*.ttf===get
   - /**/*.png===get
   - /**/*.gif===get
+    - /**/*.png===*
+  # swagger ui 资源
   - /swagger-resources/**===get
   - /v2/api-docs===get
   - /v3/api-docs===get
-  - /**/*.png===*
 
 # account info
 # there are three account: admin, root, tom

+ 1 - 1
pom.xml

@@ -102,7 +102,7 @@
                 <configuration>
                     <rulesets>
                         <ruleset>rulesets/java/ali-comment.xml</ruleset>
-                        <ruleset>rulesets/java/ali-concurrent.xml</ruleset>
+                        <!--<ruleset>rulesets/java/ali-concurrent.xml</ruleset>-->
                         <ruleset>rulesets/java/ali-constant.xml</ruleset>
                         <!--<ruleset>rulesets/java/ali-exception.xml</ruleset>-->
                         <!--<ruleset>rulesets/java/ali-flowcontrol.xml</ruleset>-->

+ 3 - 1
warehouse/src/main/java/com/usthe/warehouse/controller/MetricsDataController.java

@@ -33,6 +33,8 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
 @Api(tags = "监控指标数据API")
 public class MetricsDataController {
 
+    private static final Integer METRIC_FULL_LENGTH = 3;
+
     @Autowired
     private RedisDataStorage redisDataStorage;
 
@@ -77,7 +79,7 @@ public class MetricsDataController {
             @RequestParam(required = false) String history
             ) {
         String[] names = metricFull.split(".");
-        if (names.length != 3) {
+        if (names.length != METRIC_FULL_LENGTH) {
             throw new IllegalArgumentException("metrics full name: " + metricFull + " is illegal.");
         }
         String app = names[0];

+ 6 - 1
web-app/README.md

@@ -2,4 +2,9 @@
 
 > 前端工程    
 
-**面向开发者,易用友好的高性能监控云服务**       
+**面向开发者,易用友好的高性能监控云服务**
+
+
+## 编译打包  
+
+```ng build --prod --base-href /console/```

+ 1 - 1
web-app/angular.json

@@ -52,7 +52,7 @@
         "build": {
           "builder": "@angular-devkit/build-angular:browser",
           "options": {
-            "outputPath": "dist/web-app",
+            "outputPath": "dist",
             "index": "src/index.html",
             "main": "src/main.ts",
             "polyfills": "src/polyfills.ts",

+ 2 - 2
web-app/src/environments/environment.prod.ts

@@ -2,9 +2,9 @@ import { Environment } from '@delon/theme';
 
 export const environment = {
   production: true,
-  useHash: true,
+  useHash: false,
   api: {
-    baseUrl: './',
+    baseUrl: '/',
     refreshTokenEnabled: true
   }
 } as Environment;

+ 1 - 1
web-app/src/environments/environment.ts

@@ -9,7 +9,7 @@ import * as MOCK_DATA from '../../_mock';
 
 export const environment = {
   production: false,
-  useHash: true,
+  useHash: false,
   api: {
     baseUrl: 'http://localhost:8080/',
     refreshTokenEnabled: true