Skip to content

微服务

开始开发


Nacos配置

  1. 在Nacos中创建命名空间。 Nacos命名空间
  2. 在Nacos中添加网关服务、数据库、Redis、文件服务配置。
    • 网关服务配置
      yaml
      spring:
        cloud:
        gateway:
          globalcors:
            cors-configurations:
              '[/**]':
                allowedOriginPatterns: "*"
                allowedHeaders: "*"
                allowedMethods: "*"
                allowCredentials: true
            default-filters:
              - DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow-Credentials Access-Control-Expose-Headers, RETAIN_UNIQUE
              - AddResponseHeader=Access-Control-Expose-Headers, Content-Disposition
      knife4j:
        gateway:
          strategy: discover
          discover:
          enabled: true
          version : openapi3
          excluded-services:
          - gateway
    • 数据库配置
      yaml
      spring:
        flyway:
          baseline-on-migrate: true
          baseline-version: 1.0.0
          enabled: true
          locations: classpath:framework/db/migration/mysql
        datasource:
          type: com.alibaba.druid.pool.DruidDataSource
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://${数据库服务地址}:${数据库服务端口}/${数据库名}?characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
          username: ${数据库用户名}
          password: ${数据库密码}
          druid:
            filter:
              config:
                enabled: true
            min-idle: 3
            initial-size: 10
            max-active: 20
            min-evictable-idle-time-millis: 30000
            pool-prepared-statements: true
            filters: stat,wall,slf4j
            max-wait: 60000
            log-abandoned: true
            stat-view-servlet:
              enabled: true
              url-pattern: /druid/*
              login-username: admin
              login-password: Abc123++
              allow:
            web-stat-filter:
              url-pattern: /*
              exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
              enabled: true
          sql:
            init:
            encoding: utf-8
      mybatis-plus:
        configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    • Redis配置
      yaml
      spring:
        redis:
          host: ${Redis服务地址}
          port: 6379
          database: 3
          timeout: 6000ms
          password: Abc123++
          lettuce:
            pool:
            max-active: 8
            max-idle: 8
            max-wait: -1ms
            min-idle: 0
    • 文件服务配置
      yaml
      iking:
        framework:
          oss:
            enabled: true
            storage-type: AWS_S3
            url: https://${对象存储服务地址}:${对象存储服务端口}
            access-key: ${对象存储服务AccessKey}
            secret-key: ${对象存储服务SecretKey}
            default-bucket-name: ${桶名称}
            region: ${桶所在区域标识}
        spring:
          servlet:
            multipart:
              max-file-size: 4000MB
              max-request-size: 8000MB

网关服务

  1. 进入gateway目录,修改bootstrap.yml的内容,配置开发环境。配置示例如下:
    yaml
    server:
      # 服务端口
      port: ${SRV_PORT:8000}
    
    spring:
      application:
        name: gateway
      cloud:
        nacos:
          discovery:
            # nacos服务地址
            server-addr: ${DISCOVERY_SRV_ADDR:x.x.x.x:8848}
            # 网关服务IP地址
            ip: ${SRV_ADDRESS:x.x.x.x}
            # nacos命名空间编号
            namespace: ${ENV:dev}
            # nacos用户名
            username: nacos
            # nacos密码
            password: nacos
            # nacos服务分组
            group: ${SERVICE_GROUP:DEFAULT_GROUP}
          config:
            server-addr: ${spring.cloud.nacos.discovery.server-addr}
            namespace: ${spring.cloud.nacos.discovery.namespace}
            username: nacos
            password: nacos
            group: ${SERVICE_GROUP:DEFAULT_GROUP}
            extension-configs:
            - refresh: true
              group: DEFAULT_GROUP
              data-id: gateway.yml
            - refresh: true
              group: DEFAULT_GROUP
              data-id: redis.yml
          gateway:
            discovery:
              locator:
                enabled: true
                lower-case-service-id: true
            enabled: true
    
    management:
      server:
        port: -1
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: always
  2. 启动网关服务。 启动网关服务

系统服务

  1. 进入server目录,修改bootstrap.yml的内容,配置开发环境。配置示例如下:
    yaml
    server:
      # 服务端口
      port: ${SRV_PORT:6010}
    
    spring:
      jackson:
        time-zone: Asia/Shanghai
      servlet:
        multipart:
          max-file-size: 4000MB
          max-request-size: 8000MB
      mvc:
        path-match:
          matching-strategy: ANT_PATH_MATCHER
      application:
        name: server
      cloud:
        nacos:
          discovery:
            # nacos服务地址
            server-addr: ${DISCOVERY_SRV_ADDR:x.x.x.x:8848}
            # 系统服务IP地址
            ip: ${SRV_ADDRESS:x.x.x.x}
            # nacos命名空间编号
            namespace: ${ENV:dev}
            # nacos用户名
            username: nacos
            # nacos密码
            password: nacos
            # nacos服务分组
            group: ${SERVICE_GROUP:DEFAULT_GROUP}
          config:
            server-addr: ${spring.cloud.nacos.discovery.server-addr}
            namespace: ${spring.cloud.nacos.discovery.namespace}
            username: ${spring.cloud.nacos.discovery.username}
            password: ${spring.cloud.nacos.discovery.password}
            group: ${spring.cloud.nacos.discovery.group}
            extension-configs:
              - refresh: true
                group: DEFAULT_GROUP
                data-id: redis.yml
              - refresh: true
                group: DEFAULT_GROUP
                data-id: datasource.yml
              - refresh: true
                group: DEFAULT_GROUP
                data-id: oss.yml
    
    iking:
      framework:
        web:
          version: @project.version@
          arch: micro_service
    
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: always
  2. 启动系统服务。 启动系统服务

认证服务

  1. 进入auth目录,修改bootstrap.yml的内容,配置开发环境。配置示例如下:
    yaml
    server:
      # 服务端口
      port: ${SRV_PORT:7010}
    
    spring:
      mvc:
        path-match:
          matching-strategy: ANT_PATH_MATCHER
      application:
        name: auth
      cloud:
        nacos:
          discovery:
            # nacos服务地址
            server-addr: ${DISCOVERY_SRV_ADDR:x.x.x.x:8848}
            # 认证服务IP地址
            ip: ${SRV_ADDRESS:x.x.x.x}
            # nacos命名空间编号
            namespace: ${ENV:dev}
            # nacos用户名
            username: nacos
            # nacos密码
            password: nacos
            # nacos服务分组
            group: ${SERVICE_GROUP:DEFAULT_GROUP}
          config:
            server-addr: ${spring.cloud.nacos.discovery.server-addr}
            namespace: ${spring.cloud.nacos.discovery.namespace}
            username: ${spring.cloud.nacos.discovery.username}
            password: ${spring.cloud.nacos.discovery.password}
            group: ${spring.cloud.nacos.discovery.group}
            extension-configs:
              - refresh: true
                group: DEFAULT_GROUP
                data-id: redis.yml
    
    iking:
      framework:
        web:
          version: @project.version@
          arch: micro_service
    
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: always
  2. 启动认证服务。 启动认证服务

业务服务

  1. 创建业务服务。 创建业务服务
  2. 添加业务服务配置。
    yaml
    server:
      # 服务端口
      port: ${SRV_PORT:XXXX}
    
    spring:
      mvc:
        path-match:
          matching-strategy: ANT_PATH_MATCHER
      application:
        name: biz-demo
      cloud:
        nacos:
          discovery:
            # nacos服务地址
            server-addr: ${DISCOVERY_SRV_ADDR:x.x.x.x:8848}
            # 业务服务IP地址
            ip: ${SRV_ADDRESS:x.x.x.x}
            # nacos命名空间编号
            namespace: ${ENV:dev}
            # nacos用户名
            username: nacos
            # nacos密码
            password: nacos
            # nacos服务分组
            group: ${SERVICE_GROUP:DEFAULT_GROUP}
          config:
            server-addr: ${spring.cloud.nacos.discovery.server-addr}
            namespace: ${spring.cloud.nacos.discovery.namespace}
            username: ${spring.cloud.nacos.discovery.username}
            password: ${spring.cloud.nacos.discovery.password}
            group: ${spring.cloud.nacos.discovery.group}
            extension-configs:
              - refresh: true
                group: DEFAULT_GROUP
                data-id: redis.yml
              - refresh: true
                group: DEFAULT_GROUP
                data-id: datasource.yml
    
    iking:
      framework:
        web:
          version: @project.version@
          arch: micro_service
    
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: always
  3. 添加业务服务基础依赖,其他功能可参考功能说明按需添加依赖。
    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.ikingtech.framework</groupId>
            <artifactId>parent</artifactId>
            <version>1.1.5</version>
            <relativePath/>
        </parent>
    
        <groupId>com.ikingtech.platform</groupId>
        <artifactId>biz-demo</artifactId>
        <description>业务服务</description>
        <packaging>jar</packaging>
    
        <dependencies>
            <dependency>
                <groupId>com.ikingtech.framework</groupId>
               <artifactId>sdk-core</artifactId>
            </dependency>
            <dependency>
                <groupId>com.ikingtech.framework</groupId>
                <artifactId>sdk-web</artifactId>
            </dependency>
            <dependency>
                <groupId>com.ikingtech.framework</groupId>
                <artifactId>sdk-context</artifactId>
            </dependency>
            <dependency>
               <groupId>com.ikingtech.framework</groupId>
               <artifactId>sdk-enums</artifactId>
            </dependency>
            <dependency>
                <groupId>com.ikingtech.framework</groupId>
                <artifactId>sdk-data</artifactId>
            </dependency>
            <dependency>
               <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>com.mysql</groupId>
                <artifactId>mysql-connector-j</artifactId>
            </dependency>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
            </dependency>
            <dependency>
                <groupId>com.oracle.database.nls</groupId>
                <artifactId>orai18n</artifactId>
            </dependency>
            <dependency>
                <groupId>com.oracle.database.jdbc</groupId>
                <artifactId>ojdbc8</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>io.github.coolbeevip</groupId>
                <artifactId>flyway-core</artifactId>
                <version>9.15.2</version>
            </dependency>
            <dependency>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-mysql</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bootstrap</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.ikingtech.framework</groupId>
                <artifactId>sdk-rpc</artifactId>
            </dependency>
            <dependency>
                <groupId>com.ikingtech.framework</groupId>
                <artifactId>sdk-security</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <!--spring boot 默认插件-->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                </plugin>
            </plugins>
        </build>
    </project>
  4. 在业务服务中进行业务功能开发。

安装为本地服务

在项目根目录创建以下脚本并执行,这个过程可能需要花费一定时间,请耐心等待。 ** windows环境

shell
@echo off

rem 本地Maven安装路径
set Path=%Path%;"D:\Program Files\apache-maven-4.0.0\bin"
rem 本地JDK安装路径
set JAVA_HOME=D:\Program Files\Java\jdk-17.0.8
rem 本地仓库路径
set LOCAL_MAVEN_REPO="D:\MavenRepo"

cd %~dp0/framework/parent
call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean install

cd %~dp0/framework/sdk
call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean install

cd %~dp0/rt-spring-boot-starter
call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean install

cd %~dp0/service
call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean install

cd %~dp0/business
call mvn -Dmaven.repo.local=%LOCAL_MAVEN_REPO% clean install

cd %~dp0

call mvn clean
  • Linux环境
    shell
    #!/bin/bash
    pwd=`pwd`
    
    # 本地Maven安装路径
    export PATH=$PATH;/root/apache-maven-4.0.0/bin
    # 本地JDK安装路径
    export JAVA_HOME=/root/Java/jdk-17.0.8
    # 本地仓库路径
    export LOCAL_MAVEN_REPO=/root/MavenRepo
    
    cd $PWD/framework/parent
    mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO clean install
    
    cd $PWD/framework/sdk
    mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO clean install
    
    cd $PWD/rt-spring-boot-starter
    mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO% clean install
    
    cd $PWD/service
    mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO clean install
    
    cd $PWD/business
    mvn -Dmaven.repo.local=$LOCAL_MAVEN_REPO clean install
    
    cd $PWD
    
    mvn clean