基本配置

本文介绍了 Macula 4.0 配置文件

基于 Macula 4.0 开发的项目,需要关注的配置文件位于 resources目录下:

  • bootstrap.yaml 用于应用程序上下文的引导阶段,优先application.yaml被加载, 提供系统级别的一些参数配置,这里主要是 nacos配置中心参数
  • application.yaml 配置所需服务的公共配置: 如 redis, dubbo ,druid
  • i18n/macula-base/messages_.properties 国际化配置文件
  • nacos配置中心 应用相关配置文件

bootstrap.yaml 配置

spring:
  profiles: 
    active: ${activatedProperties}
  application:
    # 应用分组
    group: MACULA   
    # 应用name , 与nacos配置中心配置文件名称相匹配-dev/staging/prd
    name: macula-xxx-admin
  # nacos 配置中心相关配置  
  cloud:
    nacos:
      config:
        enabled: true
        # 配置文件 后缀格式, 比如.yaml、 .properties  
        file-extension: yaml 
        # 配置中心地址
        server-addr: ${macula.nacos.url}
        # 配置所在空间 比如 Macula
        namespace: ${macula.nacos.namespace}
        # 配置中心 用户验证 
        username: ${macula.nacos.username}
        password: ${macula.nacos.password}
        # 扩展配置
        extension-configs:
          - data-id: ${spring.application.group}.yaml
            refresh: true
---
spring:
  profiles: dev
macula:
  nacos:
    url: https://127.0.0.1
    username: xxx
    password: xxx
    namespace: MACULA-DEV

application.yaml 配置

spring:
  main:
    # 允许覆盖bean 定义, 默认 开启
    allow-bean-definition-overriding: true
  messages:
    # 国际化 文件地址配置
    basename: classpath*:i18n/**/messages,classpath*:i18n/**/validation
  # spring jpa 相关配置
  jpa:
    generate-ddl: false
    show-sql: false
    hibernate:
      ddl-auto: none
      use-new-id-generator-mappings: false
  # Oauth2 认证URL 配置  
  security:
    oauth2:
      resourceserver:
        opaque:
          introspection-uri: ${macula.oauth2.introspection.url}
          introspection-client-id: ${macula.oauth2.introspection.client-id}
          introspection-client-secret: ${macula.oauth2.introspection.client-secret}
  # spring cloud nacos discovery
  cloud:
    nacos:
      discovery:
        server-addr: ${macula.nacos.url}
        namespace: ${macula.nacos.namespace}
        username: ${macula.nacos.username}
        password: ${macula.nacos.password}
########### Server配置 ##################
server:
  port: 8083
  tomcat:
    accept-count: 1000
    max-connections: 10000
    threads:
      max: 800
      min-spare: 100
  servlet:
    encoding:
      force: true
    session:
      cookie:
        name: s-${spring.application.group}-${spring.profiles}
        domain-name-pattern: ^.+?(\\..*?[\\w\\-]+\\.[a-zA-Z]+)$

###########dubbo配置###############
dubbo:
  application:
    name: ${spring.application.name}
  scan:
    # dubbo 服务扫描基准包
    base-packages: xxx.xx..
  protocol:
    # dubbo 协议
    name: dubbo
    # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
    port: -1
  registry:
    # 挂载到 Spring Cloud 注册中心
    address: spring-cloud://localhost
macula:
  ######## 缓存Redis配置 ##############
  redis:
  - name: cache
    singleServerConfig:
      address: ${macula.redis.cache.url}
      password: ${macula.redis.cache.password}
  - name: data
   singleServerConfig:
    address: ${macula.redis.data.url}
    password: ${macula.redis.data.password}
  - name: klock
   singleServerConfig:
    address: ${macula.redis.klock.url}
    password: ${macula.redis.klock.password}
  - name: oauth2
   singleServerConfig:
    address: ${macula.redis.oauth2.url}
    password: ${macula.redis.oauth2.password}    
  ######## 数据源配置,可以多个 ###########
  datasource:
    druid:
      - name: macula-base
        ## JDBC配置
        url: ${macula.datasource.macula-base.url}
        username: ${macula.datasource.macula-base.username}
        password: ${macula.datasource.macula-base.password}
        ## 连接池配置
        max-active: 250
        initial-size: 50
        max-wait: 60000
        min-idle: 10
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: select '1'
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        pool-prepared-statements: true
        max-open-prepared-statements: 50
        max-pool-prepared-statement-per-connection-size: 20
        ## 过滤器配置
        filters: stat,wall
  # 与数据源相对应,可以多个,每个项目都需要修改对应的包名
  jpa:
    repositories:
      - name: macula-base
        entity-packages: org.macula.base
        repository-packages: org.macula.base
        service-package: org.macula.base

i18n/macula-base/messages_.properties

自定义国际化消息配置文件

i18n/macula-base/messages.properties

i18n/macula-base/messages_zh_CN.properties

i18n/macula-base/messages_JAPANESE.properties

nacos 配置中心 应用配置文件

例:

namespace : 配置空间, 用于保存(隔离)不同业务/应用配置

macula-base-demo-dev.yaml

  • macula-base-demo 需对应spring.application.name 配置
  • **dev 则 对应dev环境 , nacos 通过 namespace +**application.name+ 环境 标识 来匹配对应的配置文件
spring:
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: none
macula:
####  redis 相关配置 ##
  redis:
    cache:
      url: redis://macula4-pub-dev-redis.xxx.com.cn:6380
      password: *****
    data:
      url: redis://macula4-pub-dev-redis.xxx.com.cn:6380
      password: *****
    klock:
      url: redis://macula4-pub-dev-redis.xxx.com.cn:6380
      password: *****
    demo:
      url: redis://macula4-pub-dev-redis.xxx.com.cn:6380
      password: *****

####  mysql  相关配置 ##
  datasource:
    macula-base:
      url: jdbc:mysql://127.0.0.1:3306/macula-demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
      username: maculadev
      password: *****
#### oauth2 相关配置 ##
  oauth2:
    introspection:
      url: http://localhost:8081/introspect
      client-id: client
      client-secret: secret
    url: https://goauth-dev.xxx.com.cn
    auth: Basic xxx==
#### 网关 相关配置 ##
  kong: 
    url: http://127.0.0.1:21548
#### 消息中心 相关配置 ##
msg:
  server: https://msg-dev.xxx.com.cn/api/push?mark=more
  appKey: xxx
  appSecret: xxx

#### uim 数据同步 相关配置 ##
uim:
  sync:
    enabled: true
  server: https://uim-dev.xxx.com.cn
  appKey: xxx
  appSecret: xxx