后端配置维护
全局配置说明
HZERO平台提取了全局常量、平台通用业务功能等到hzero-boot-common组件中,即凡是涉及到跨服务的常量或平台通用的业务都可以提取到common中进行引用,便于统一管理、客制化修改等。
<groupId>org.hzero.boot</groupId>
<artifactId>hzero-boot-common</artifactId>
<version>0.1.0-SNAPSHOT</version>
每个产品平台自建一个xxx-boot-common组件,如srm-boot-common ,用于封装全局通用业务。并且依赖hzero-boot-common组件,如果有需要客制化的需求,覆盖源码修改即可(由于注解参数只支持编译时常量,采用这种方式为最佳)。
每个common可维护平台的相关服务常量:
/**
 * HZERO服务常量
 */
public interface HZeroService {
    /**
     * IAM Service
     */
    interface Iam {
        String NAME = "hzero-iam";
        String CODE = "hiam";
        Integer PORT = 8030;
    }
    /**
     * Oauth Service
     */
    interface Oauth {
        String NAME = "hzero-oauth";
        String CODE = "hoth";
        Integer PORT = 8120;
    }
    /**
     * Platform Service
     */
    interface Platform {
        String NAME = "hzero-platform";
        String CODE = "hpfm";
        Integer PORT = 8210;
    }
    /**
     * File Service
     */
    interface File {
        String NAME = "hzero-file";
        String CODE = "hfle";
        Integer PORT = 8240;
    }
    /**
     * Message Service
     */
    interface Message {
        String NAME = "hzero-message";
        String CODE = "hmsg";
        Integer PORT = 8260;
    }
    /**
     * Portal Service
     */
    interface Portal {
        String NAME = "hzero-portal";
        String CODE = "hptl";
        Integer PORT = 8200;
    }
    /**
     * Transfer Service
     */
    interface Transfer {
        String NAME = "hzero-transfer";
        String CODE = "hdtt";
        Integer PORT = 8310;
    }
}
hzero-boot-supporter
另外一个组件hzero-boot-supporter主要用于封装通用工具等,可以在各个平台使用,而不局限于某个平台。要区别二者的作用及范围。
属性配置
对于服务中的一些可变属性配置,可采用Springboot的@ConfigurationProperties进行配置,步骤如下:
1、在特定服务中创建一个XxxProperties属性配置类,并实现CustomProperties接口,如
/**
 * 验证码配置
 */
@ConfigurationProperties(prefix = CaptchaProperties.PREFIX_CAPTCHA)
public class CaptchaProperties implements CustomProperties {
    public static final String PREFIX_CAPTCHA = "hzero.captcha";
    /**
     * 验证码key
     */
    private String captchaKey = "captchaKey";
    /**
     * 图片验证码配置
     */
    private Image image = new Image();
    /**
     * 图片验证码
     */
    static class Image {
        /**
         * 验证码过期时间(分)
         */
        private Integer expire = 10;
        /**
         * 图片宽度
         */
        private Integer width = 125;
        /**
         * 图片高度
         */
        private Integer height = 45;
        /**
         * 图片样式
         */
        private ImageStyle style = ImageStyle.WATER_RIPPLE;
        enum ImageStyle {
            /**
             * 水纹
             */
            WATER_RIPPLE("com.google.code.kaptcha.impl.WaterRipple"),
            /**
             * 鱼眼
             */
            FISH_EYE("com.google.code.kaptcha.impl.FishEyeGimpy"),
            /**
             * 阴影
             */
            SHADOW("com.google.code.kaptcha.impl.ShadowGimpy");
            private String value;
            ImageStyle (String value) {
                this.value = value;
            }
            public String value() {
                return value;
            }
        }
        public Integer getExpire() {
            return expire;
        }
        public void setExpire(Integer expire) {
            this.expire = expire;
        }
        public Integer getWidth() {
            return width;
        }
        public void setWidth(Integer width) {
            this.width = width;
        }
        public Integer getHeight() {
            return height;
        }
        public void setHeight(Integer height) {
            this.height = height;
        }
        public ImageStyle getStyle() {
            return style;
        }
        public void setStyle(ImageStyle style) {
            this.style = style;
        }
    }
    public String getCaptchaKey() {
        return captchaKey;
    }
    public void setCaptchaKey(String captchaKey) {
        this.captchaKey = captchaKey;
    }
    public Image getImage() {
        return image;
    }
    public void setImage(Image image) {
        this.image = image;
    }
}
2、注册成bean
使用@EnableConfigurationProperties开启@ConfigurationProperties的支持
@Configuration
@EnableConfigurationProperties
public class CaptchaConfigure {
    @Bean
    public CaptchaProperties captchaProperties() {
        return new CaptchaProperties();
    }
}
3、POM中依赖spring-boot-configuration-processor,以支持在配置文件中显示自定义的配置属性
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
4、之后就可以在配置文件中看到可配置的内容

5、如果想要获取自定义的配置,可直接依赖该bean对象获取,或者使用Configurer工具来获取
String captchaKey = Configurer.getInteger("hzero.captcha.captcha-key");
Integer width = Configurer.getInteger("hzero.captcha.image.width");