浏览代码

spring-cloud-commons支持.

nieqiurong 4 年之前
父节点
当前提交
26df3a4afa

+ 1 - 0
build.gradle

@@ -37,6 +37,7 @@ ext {
         "aspectjrt"                  : "org.aspectj:aspectjrt:1.9.6",
         "cglib"                      : "cglib:cglib:3.3.0",
         "imadcn"                     : "com.imadcn.framework:idworker:1.5.0",
+        "spring-cloud-commons"       : "org.springframework.cloud:spring-cloud-commons:2.2.6.RELEASE",
 
         "javax.servlet-api"          : "javax.servlet:javax.servlet-api:4.0.1",
         "aspectjweaver"              : "org.aspectj:aspectjweaver:1.9.6",

+ 1 - 0
mybatis-plus-boot-starter/build.gradle

@@ -10,6 +10,7 @@ dependencies {
     implementation "${lib['mybatis-thymeleaf']}"
     implementation "${lib.'mybatis-velocity'}"
     implementation "${lib.'mybatis-freemarker'}"
+    implementation "${lib.'spring-cloud-commons'}"
     testImplementation "org.springframework.boot:spring-boot-starter-test"
     testImplementation "${lib.'mybatis-spring-boot-starter'}"
 }

+ 63 - 0
mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/IdentifierGeneratorAutoConfiguration.java

@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.baomidou.mybatisplus.autoconfigure;
+
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
+import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.cloud.commons.util.InetUtils;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author nieqiurong 2021/1/29
+ * @since 3.4.3
+ */
+@Configuration
+@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
+public class IdentifierGeneratorAutoConfiguration {
+
+    @Configuration
+    @ConditionalOnClass(InetUtils.class)
+    public static class InetUtilsAutoConfig {
+
+        private final InetUtils inetUtils;
+
+        private final MybatisPlusProperties properties;
+
+        public InetUtilsAutoConfig(InetUtils inetUtils, MybatisPlusProperties properties) {
+            this.inetUtils = inetUtils;
+            this.properties = properties;
+        }
+
+        @Bean
+        @ConditionalOnMissingBean
+        public IdentifierGenerator identifierGenerator() {
+            GlobalConfig globalConfig = properties.getGlobalConfig();
+            Long workerId = globalConfig.getWorkerId();
+            Long datacenterId = globalConfig.getDatacenterId();
+            if (workerId != null && datacenterId != null) {
+                return new DefaultIdentifierGenerator(workerId, datacenterId);
+            } else {
+                return new DefaultIdentifierGenerator(inetUtils.findFirstNonLoopbackAddress());
+            }
+        }
+
+    }
+
+}

+ 1 - 0
mybatis-plus-boot-starter/src/main/resources/META-INF/spring.factories

@@ -2,5 +2,6 @@
 org.springframework.boot.env.EnvironmentPostProcessor=\
   com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+  com.baomidou.mybatisplus.autoconfigure.IdentifierGeneratorAutoConfiguration,\
   com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
   com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

+ 11 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/DefaultIdentifierGenerator.java

@@ -17,6 +17,8 @@ package com.baomidou.mybatisplus.core.incrementer;
 
 import com.baomidou.mybatisplus.core.toolkit.Sequence;
 
+import java.net.InetAddress;
+
 /**
  * 默认生成器
  *
@@ -28,10 +30,19 @@ public class DefaultIdentifierGenerator implements IdentifierGenerator {
 
     private final Sequence sequence;
 
+    /**
+     * @see #DefaultIdentifierGenerator(InetAddress)
+     * @deprecated 3.4.3
+     */
+    @Deprecated
     public DefaultIdentifierGenerator() {
         this.sequence = new Sequence();
     }
 
+    public DefaultIdentifierGenerator(InetAddress inetAddress) {
+        this.sequence = new Sequence(inetAddress);
+    }
+
     public DefaultIdentifierGenerator(long workerId, long dataCenterId) {
         this.sequence = new Sequence(workerId, dataCenterId);
     }

+ 32 - 13
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Sequence.java

@@ -15,6 +15,7 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.logging.LogFactory;
 
@@ -22,6 +23,7 @@ import java.lang.management.ManagementFactory;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.UnknownHostException;
+import java.util.Optional;
 import java.util.concurrent.ThreadLocalRandom;
 
 /**
@@ -73,11 +75,40 @@ public class Sequence {
      */
     private long lastTimestamp = -1L;
 
+    private InetAddress inetAddress;
+
+    /**
+     * @deprecated 3.4.3
+     */
+    @Deprecated
     public Sequence() {
+        this.inetAddress = getLocalHost();
+        this.datacenterId = getDatacenterId(maxDatacenterId);
+        this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
+    }
+
+    public Sequence(InetAddress inetAddress) {
+        this.inetAddress = inetAddress;
         this.datacenterId = getDatacenterId(maxDatacenterId);
         this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
     }
 
+    private InetAddress getLocalHost() {
+        try {
+            return InetAddress.getLocalHost();
+        } catch (UnknownHostException e) {
+            throw new MybatisPlusException(e);
+        }
+    }
+
+    /**
+     * @return InetAddress
+     * @since 3.4.3
+     */
+    protected InetAddress getInetAddress() {
+        return Optional.ofNullable(this.inetAddress).orElseGet(this::getLocalHost);
+    }
+
     /**
      * 有参构造器
      *
@@ -112,25 +143,13 @@ public class Sequence {
         return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
     }
 
-    /**
-     * 获取 InetAddress
-     *
-     * @return InetAddress
-     * @throws  UnknownHostException
-     * @since 3.4.3
-     */
-    protected InetAddress getInetAddress() throws UnknownHostException {
-        return InetAddress.getLocalHost();
-    }
-
     /**
      * 数据标识id部分
      */
     protected long getDatacenterId(long maxDatacenterId) {
         long id = 0L;
         try {
-            InetAddress ip = this.getInetAddress();
-            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
+            NetworkInterface network = NetworkInterface.getByInetAddress(this.getInetAddress());
             if (network == null) {
                 id = 1L;
             } else {