From 26df3a4afa5a02690020fbb092a81f94d9570c99 Mon Sep 17 00:00:00 2001 From: nieqiurong Date: Fri, 29 Jan 2021 19:03:48 +0800 Subject: [PATCH] =?UTF-8?q?spring-cloud-commons=E6=94=AF=E6=8C=81.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + mybatis-plus-boot-starter/build.gradle | 1 + .../IdentifierGeneratorAutoConfiguration.java | 63 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../DefaultIdentifierGenerator.java | 11 ++++ .../mybatisplus/core/toolkit/Sequence.java | 45 +++++++++---- 6 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/IdentifierGeneratorAutoConfiguration.java diff --git a/build.gradle b/build.gradle index 0a123e7b85..66b107414c 100644 --- a/build.gradle +++ b/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", diff --git a/mybatis-plus-boot-starter/build.gradle b/mybatis-plus-boot-starter/build.gradle index 79f048f6f0..f95a913787 100644 --- a/mybatis-plus-boot-starter/build.gradle +++ b/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'}" } diff --git a/mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/IdentifierGeneratorAutoConfiguration.java b/mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/IdentifierGeneratorAutoConfiguration.java new file mode 100644 index 0000000000..fd6a7d416e --- /dev/null +++ b/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()); + } + } + + } + +} diff --git a/mybatis-plus-boot-starter/src/main/resources/META-INF/spring.factories b/mybatis-plus-boot-starter/src/main/resources/META-INF/spring.factories index 63422edbb0..2e54e91bd3 100755 --- a/mybatis-plus-boot-starter/src/main/resources/META-INF/spring.factories +++ b/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 diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/DefaultIdentifierGenerator.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/DefaultIdentifierGenerator.java index d54f8581e1..9a03d80dff 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/DefaultIdentifierGenerator.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/DefaultIdentifierGenerator.java @@ -17,6 +17,8 @@ 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); } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Sequence.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Sequence.java index 4e0ff09f15..aa5824c12c 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Sequence.java +++ b/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.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 @@ protected long getMaxWorkerId(long datacenterId, long maxWorkerId) { 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 {