From 6a173c0504d930bd8315909c88c0dded0a6a6668 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 13 Jan 2021 18:25:25 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E8=BF=AA?= =?UTF-8?q?=E6=99=AE=E9=98=B2=E6=8A=A4=E5=AF=B9=E8=B1=A1=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=202.=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=BF=AA=E6=99=AE=E9=98=B2=E6=8A=A4=E6=A8=A1=E6=9D=BF=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=203.=20=E5=A2=9E=E5=8A=A0=E8=BF=AA=E6=99=AE=E9=98=B2?= =?UTF-8?q?=E6=8A=A4=E6=A8=A1=E6=9D=BF=E6=98=A0=E5=B0=84=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=204.=20=E4=BF=AE=E6=94=B9=E4=B8=9A=E5=8A=A1=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=B8=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=205.=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=8D=95=E6=B5=8B=E6=A1=86=E6=9E=B6=EF=BC=8C=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E9=98=B2=E6=8A=A4=E5=AF=B9=E8=B1=A1=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=206.=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=BF=AA=E6=99=AE=E9=98=B2=E6=8A=A4=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/application-template.properties | 51 ++++++++++++++ config/application.properties | 2 +- .../java/com/dispose/common/ServiceType.java | 68 ------------------- .../com/dispose/config/TemplateConfigure.java | 27 ++++++++ .../com/dispose/pojo/entity/ServiceInfo.java | 3 +- .../com/dispose/pojo/vo/TemplateInfo.java | 34 ++++++++++ .../com/dispose/service/TemplateService.java | 17 +++++ .../service/impl/TemplateServiceImpl.java | 50 ++++++++++++++ src/main/resources/db/schema.sql | 2 +- src/main/resources/mappers/ServiceGroup.xml | 2 +- src/main/resources/test_db/unit_test.sql | 2 +- .../test/common/CommonEnvironment.java | 2 +- .../test/dev/Global/InitTestEnvironment.java | 2 +- .../dev/config/TemplateConfigureTest.java | 37 ++++++++++ 14 files changed, 223 insertions(+), 76 deletions(-) create mode 100644 config/application-template.properties delete mode 100644 src/main/java/com/dispose/common/ServiceType.java create mode 100644 src/main/java/com/dispose/config/TemplateConfigure.java create mode 100644 src/main/java/com/dispose/pojo/vo/TemplateInfo.java create mode 100644 src/main/java/com/dispose/service/TemplateService.java create mode 100644 src/main/java/com/dispose/service/impl/TemplateServiceImpl.java create mode 100644 src/test/java/com/dispose/test/dev/config/TemplateConfigureTest.java diff --git a/config/application-template.properties b/config/application-template.properties new file mode 100644 index 00000000..8a1618c3 --- /dev/null +++ b/config/application-template.properties @@ -0,0 +1,51 @@ +template.testValue=12345 +template.templateInfo.name=Game_Server_10G +template.templateInfo.bandMin=1500 +template.templateInfo.bandMax=-1 +template.templateInfo.type=GAME +#模板配置 +template.umcTemplate[0].name=Game_Server_10G +template.umcTemplate[0].bandMin=1500 +template.umcTemplate[0].bandMax=-1 +template.umcTemplate[0].type=GAME + +template.umcTemplate[1].name=Game_Server_1G +template.umcTemplate[1].bandMin=0 +template.umcTemplate[1].bandMax=1500 +template.umcTemplate[1].type=GAME + +template.umcTemplate[2].name=DNS_Server_10G +template.umcTemplate[2].bandMin=1500 +template.umcTemplate[2].bandMax=-1 +template.umcTemplate[2].type=DNS + +template.umcTemplate[3].name=DNS_Server_1G +template.umcTemplate[3].bandMin=0 +template.umcTemplate[3].bandMax=1500 +template.umcTemplate[3].type=DNS + +template.umcTemplate[4].name=WEB_Server_10G +template.umcTemplate[4].bandMin=1500 +template.umcTemplate[4].bandMax=-1 +template.umcTemplate[4].type=WEB + +template.umcTemplate[5].name=WEB_Server_1G +template.umcTemplate[5].bandMin=0 +template.umcTemplate[5].bandMax=1500 +template.umcTemplate[5].type=WEB + +template.umcTemplate[6].name=General_Server_10G +template.umcTemplate[6].bandMin=1500 +template.umcTemplate[6].bandMax=-1 +template.umcTemplate[6].type=SERVER + +template.umcTemplate[7].name=General_Server_1G +template.umcTemplate[7].bandMin=500 +template.umcTemplate[7].bandMax=1500 +template.umcTemplate[7].type=SERVER + +template.umcTemplate[8].name=General_Server_100M +template.umcTemplate[8].bandMin=0 +template.umcTemplate[8].bandMax=500 +template.umcTemplate[8].type=SERVER + diff --git a/config/application.properties b/config/application.properties index b745d9d5..793906d1 100644 --- a/config/application.properties +++ b/config/application.properties @@ -1 +1 @@ -spring.profiles.active=local,dispose \ No newline at end of file +spring.profiles.active=local,dispose,template \ No newline at end of file diff --git a/src/main/java/com/dispose/common/ServiceType.java b/src/main/java/com/dispose/common/ServiceType.java deleted file mode 100644 index 2c316137..00000000 --- a/src/main/java/com/dispose/common/ServiceType.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.dispose.common; - -/** - * The enum Service type. - * - * @author - */ -public enum ServiceType implements BaseEnum { - /** - * The Service and others. - */ - SERVICE_AND_OTHERS(0, "服务器以及其它类型"), - - /** - * Web site service type. - */ - WEB_SITE(1, "网站"), - - /** - * Dns service service type. - */ - DNS_SERVICE(2, "DNS 服务"), - - /** - * Game service service type. - */ - GAME_SERVICE(3, "游戏 服务"), - ; - /** - * The Code. - */ - private final Integer code; - /** - * The Readme. - */ - private final String readme; - - /** - * Instantiates a new Service type. - * - * @param code the code - * @param readme the readme - */ - ServiceType(int code, String readme) { - this.code = code; - this.readme = readme; - } - - /** - * Gets value. - * - * @return the value - */ - @Override - public Integer getValue() { - return this.code; - } - - /** - * Gets description. - * - * @return the description - */ - @Override - public String getDescription() { - return this.readme; - } -} diff --git a/src/main/java/com/dispose/config/TemplateConfigure.java b/src/main/java/com/dispose/config/TemplateConfigure.java new file mode 100644 index 00000000..4b64344b --- /dev/null +++ b/src/main/java/com/dispose/config/TemplateConfigure.java @@ -0,0 +1,27 @@ +package com.dispose.config; + +import com.dispose.pojo.vo.TemplateInfo; +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * The type Template configure. + * + * @author + */ +@Getter +@Setter +@Component +@ConfigurationProperties(prefix = "template") +@Configuration +public class TemplateConfigure { + /** + * The Umc template. + */ + List umcTemplate; +} diff --git a/src/main/java/com/dispose/pojo/entity/ServiceInfo.java b/src/main/java/com/dispose/pojo/entity/ServiceInfo.java index 973789c5..40fb81ab 100644 --- a/src/main/java/com/dispose/pojo/entity/ServiceInfo.java +++ b/src/main/java/com/dispose/pojo/entity/ServiceInfo.java @@ -1,6 +1,5 @@ package com.dispose.pojo.entity; -import com.dispose.common.ServiceType; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.AllArgsConstructor; @@ -48,7 +47,7 @@ public class ServiceInfo { /** * The Service type. */ - private ServiceType serviceType; + private String serviceType; /** * The Service bandwidth. diff --git a/src/main/java/com/dispose/pojo/vo/TemplateInfo.java b/src/main/java/com/dispose/pojo/vo/TemplateInfo.java new file mode 100644 index 00000000..42a6a0f0 --- /dev/null +++ b/src/main/java/com/dispose/pojo/vo/TemplateInfo.java @@ -0,0 +1,34 @@ +package com.dispose.pojo.vo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * The type Template info. + * + * @author + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class TemplateInfo { + /** + * The Name. + */ + private String name; + /** + * The Band min. + */ + private Long bandMin; + /** + * The Band max. + */ + private Long bandMax; + /** + * The Type. + */ + private String type; +} diff --git a/src/main/java/com/dispose/service/TemplateService.java b/src/main/java/com/dispose/service/TemplateService.java new file mode 100644 index 00000000..865e0535 --- /dev/null +++ b/src/main/java/com/dispose/service/TemplateService.java @@ -0,0 +1,17 @@ +package com.dispose.service; + +/** + * The interface Template service. + * + * @author + */ +public interface TemplateService { + /** + * Gets template. + * + * @param type the type + * @param bandWidth the band width + * @return the template + */ + String getTemplate(String type, Long bandWidth); +} diff --git a/src/main/java/com/dispose/service/impl/TemplateServiceImpl.java b/src/main/java/com/dispose/service/impl/TemplateServiceImpl.java new file mode 100644 index 00000000..a7386ee5 --- /dev/null +++ b/src/main/java/com/dispose/service/impl/TemplateServiceImpl.java @@ -0,0 +1,50 @@ +package com.dispose.service.impl; + +import com.dispose.config.TemplateConfigure; +import com.dispose.pojo.vo.TemplateInfo; +import com.dispose.service.TemplateService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * The type Template service. + * + * @author + */ +@Service +@Slf4j +public class TemplateServiceImpl implements TemplateService { + @Resource + TemplateConfigure templateConfigure; + + /** + * Gets template. + * + * @param type the type + * @param bandWidth the band width + * @return the template + */ + @Override + public String getTemplate(String type, Long bandWidth) { + // 遍历所有模板 + for (TemplateInfo v : templateConfigure.getUmcTemplate()) { + // 判断类型是否一致 + if (v.getType().equals(type)) { + // 当前带宽大于当前模板最小值 + if (bandWidth > v.getBandMin()) { + // 模板最大值为-1或者带宽小于等于模板最小值,返回模板名称 + if (v.getBandMax() == -1 || bandWidth <= v.getBandMax()) { + log.debug("Template analyze: {}, {} match {}", type, bandWidth, v); + return v.getName(); + } + } + } + } + + // 纪录异常日志 + log.error("Template analyze: {}, {} match nothing!!!", type, bandWidth); + return null; + } +} diff --git a/src/main/resources/db/schema.sql b/src/main/resources/db/schema.sql index 752ed366..9b4f4db1 100644 --- a/src/main/resources/db/schema.sql +++ b/src/main/resources/db/schema.sql @@ -136,7 +136,7 @@ CREATE TABLE `service_group` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '业务ID唯一标识符', `serviceId` int(11) UNSIGNED NOT NULL COMMENT '业务ID', - `serviceType` int(11) NOT NULL COMMENT '业务类型:\r\n0:SEVER\r\n1:WEB\r\n2:DNS\r\n3:GAME', + `serviceType` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '业务类型:SERVER,WEB,DNS,GAME', `serviceBandwidth` int(11) NOT NULL COMMENT '业务带宽,单位M', `serviceIp` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '业务IP地址,逗号分割', PRIMARY KEY (`id`) USING BTREE diff --git a/src/main/resources/mappers/ServiceGroup.xml b/src/main/resources/mappers/ServiceGroup.xml index ea3e1630..029a0a6a 100644 --- a/src/main/resources/mappers/ServiceGroup.xml +++ b/src/main/resources/mappers/ServiceGroup.xml @@ -4,7 +4,7 @@ - + diff --git a/src/main/resources/test_db/unit_test.sql b/src/main/resources/test_db/unit_test.sql index 80d93e4a..a8d2f48c 100644 --- a/src/main/resources/test_db/unit_test.sql +++ b/src/main/resources/test_db/unit_test.sql @@ -212,7 +212,7 @@ CREATE TABLE `service_group` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '业务ID唯一标识符', `serviceId` int(11) UNSIGNED NOT NULL COMMENT '业务ID', - `serviceType` int(11) NOT NULL COMMENT '业务类型:\r\n0:SEVER\r\n1:WEB\r\n2:DNS\r\n3:GAME', + `serviceType` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '业务类型:SERVER,WEB,DNS,GAME', `serviceBandwidth` int(11) NOT NULL COMMENT '业务带宽,单位M', `serviceIp` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '业务IP地址,逗号分割', PRIMARY KEY (`id`) USING BTREE diff --git a/src/test/java/com/dispose/test/common/CommonEnvironment.java b/src/test/java/com/dispose/test/common/CommonEnvironment.java index 1eb16bc5..ae946cf7 100644 --- a/src/test/java/com/dispose/test/common/CommonEnvironment.java +++ b/src/test/java/com/dispose/test/common/CommonEnvironment.java @@ -26,7 +26,7 @@ import java.util.Optional; @Data @Slf4j @EnableEncryptableProperties -@ActiveProfiles({"test", "dispose"}) +@ActiveProfiles({"test", "dispose", "template"}) public class CommonEnvironment { /** diff --git a/src/test/java/com/dispose/test/dev/Global/InitTestEnvironment.java b/src/test/java/com/dispose/test/dev/Global/InitTestEnvironment.java index f852e0b1..bd4215bf 100644 --- a/src/test/java/com/dispose/test/dev/Global/InitTestEnvironment.java +++ b/src/test/java/com/dispose/test/dev/Global/InitTestEnvironment.java @@ -26,7 +26,7 @@ import javax.annotation.Resource; @Slf4j @Getter @EnableEncryptableProperties -@ActiveProfiles({"test", "dispose"}) +@ActiveProfiles({"test", "dispose", "template"}) public class InitTestEnvironment { /** * The constant HTTP_CONNECT_TIMEOUT. diff --git a/src/test/java/com/dispose/test/dev/config/TemplateConfigureTest.java b/src/test/java/com/dispose/test/dev/config/TemplateConfigureTest.java new file mode 100644 index 00000000..3051166a --- /dev/null +++ b/src/test/java/com/dispose/test/dev/config/TemplateConfigureTest.java @@ -0,0 +1,37 @@ +package com.dispose.test.dev.config; + +import com.dispose.config.TemplateConfigure; +import com.dispose.test.dev.Global.InitTestEnvironment; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +/** + * The type Template configure test. + * + * @author + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +public class TemplateConfigureTest extends InitTestEnvironment { + /** + * The Template configure. + */ + @Resource + TemplateConfigure templateConfigure; + + /** + * Template loads test. + */ + @Test + public void templateLoadsTest() { + log.info("Load template: {}", templateConfigure.getUmcTemplate()); + Assert.assertEquals(9, templateConfigure.getUmcTemplate().size()); + } +}