前言
 
微服务中规范往往比代码更加重要,一些良好的规范,能让我们少走弯路。mica-launcher 启动器就是对服务名和服务环境进行了定制的处理,使得企业开发更加方便快捷。
 
服务名规范
 
服务名在微服务中起着至关重要的位置,一个好的服务名应该见名知意。下面是笔者在工作中总结的规范。
 
小开发团队-两级
 
例如:user-api
 
 
 - 第一级 服务名
- 第二级 服务类型
多个开发团队-三级
 
将组名放到第一位,方便快速定位到技术组。
 
例如:mica-user-api
 
 
 - 第一级 组名
- 第二级 服务名
- 第三级 服务类型
环境划分
 
愿景
 
启动器的使命就是让我们的 jar 在各种环境中都可以方便启动,不用添加过多的配置,减少学习成本,能快速上手。提供了环境的日志打印,避免启动期间的各种问题。
 
环境划分
 
 
 dev(开发)、test(测试)、ontest(线上测试)、prod(正式),默认dev
 
 
启动环境变量
 
java命令行:
 
java -jar app.jar --spring.profiles.active=dev
 
JAVA_OPS
 
set JAVA_OPTS="-Dspring.profiles.active=test"
 
标注方式(代码层面,junit单元测试非常实用)
 
@ActiveProfiles({"junittest","productprofile"})
 
ENV方式
 
系统环境变量 SPRING_PROFILES_ACTIVE(注意:是大写)
 
背景故事
 
mica-launcher 最早的雏形初现在笔者的一些 JFinal 项目中,期初主要是为了适应开发阶段的 jetty 和正式环境的 tomcat 日志目录。
 

 
在 Spring boot 中我便设计了 mica-launcher 来处理服务环境,使其更加方便好用。
 
使用
 
使用起来比原生的 spring boot 启动器里多了一个服务名参数,微服务中服务名对一个服务特别重要,故在启动器启动时写死。
 
Maven
 
<dependency>
    <groupId>net.dreamlu</groupId>
    <artifactId>mica-launcher</artifactId>
</dependency>
 
Gradle
 
implementation "net.dreamlu:mica-launcher"
 
示例:
 
@SpringBootApplication
public class MicaExampleApplication {
  public static void main(String[] args) {
    MicaApplication.run("mica-example", MicaExampleApplication.class, args);
  }
}
 
注意:使用了 mica-launcher 启动器,需要结合 mica-test 进行单元测试,具体文章请查看 mica test 单元测试。
 
启动器自动化配置
 
代码中可以采用注入 MicaProperties 来读取启动器中的一些变量,比如 env 等。
 
mica.prop 可以在配置文件中自定义配置。然后再在代码中使用 MicaProperties 读取。
 
 
  
   
   | 配置项 | 默认值 | 说明 | 
 
  
  
   
   | mica.env | dev | 【只读】mica 环境变量,方便在代码中获取,设置无效. | 
 
   
   | mica.is-local | false | 【只读】判断是否为 本地开发环境 | 
 
   
   | mica.prop | 无 | 装载自定义配置 mica.prop.xxx | 
 
  
 
自定义配置
 
mica:
  prop:
    site-name: 如梦技术
    site-url: https://www.dreamlu.net
 
配置读取
 
@Autowired
private MicaProperties micaProperties;
public boolean savePost() {
    String siteName = micaProperties.get("site-name");
    String siteUrl = micaProperties.get("site-url");
    // .....
}
 
启动器插件扩展
 
启动器的主要目的是能更加方便的去注入一些通用配置,降低使用难度。mica-log4j2 就是一个启动器的扩展。
 
插件扩展基于 java SPI 技术,关于 java SPI 具体使用可以百度。
 
实现 LauncherService,重写 launcher 方法
 
下面是 LauncherService 的代码。
 
/**
 * launcher 扩展 用于一些组件发现
 *
 * @author L.cm
 */
public interface LauncherService {
   /**
    * 启动时 处理 SpringApplicationBuilder
    * @param builder SpringApplicationBuilder
    * @param env 系统变量 Environment
    * @param appName 服务名
    * @param profile 环境变量
    * @param isLocalDev 是否本地开发
    */
   void launcher(SpringApplicationBuilder builder, Environment env, String appName, String profile, boolean isLocalDev);
}
 
编写 SPI 文件放置到 resources 目录下
 
文件路径和文件名 META-INF/services/net.dreamlu.mica.launcher.LauncherService 。
 
内容为你编写的插件完整类名,例如:net.dreamlu.mica.log.LogLauncherServiceImpl。
 
最佳实践
 
mica-launcher 中我们预制了环境变量,您可以再服务里将配置中心按环境来划分域名。在配置中可以使用 ${mica.env} 占位符完成配置。
 
例如:(配置服务域名)
 
config-dev.dreamlu.net
config-test.dreamlu.net
......
 
使用示例:
 
spring:
  cloud:
    consul:
      host: https://config-${mica.env}.dreamlu.vip
      port: 8500
      config:
        format: yaml
 
相关链接
 
 
关注我们
 

 
扫描上面二维码,更多精彩内容每天推荐!