Skip to content

Commit

Permalink
spring-cloud-commons支持.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Jan 29, 2021
1 parent 978f65d commit 26df3a4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions mybatis-plus-boot-starter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'}"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2011-2021, baomidou ([email protected]).
*
* 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());
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.baomidou.mybatisplus.core.toolkit.Sequence;

import java.net.InetAddress;

/**
* 默认生成器
*
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
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;

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;

/**
Expand Down Expand Up @@ -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);
}

/**
* 有参构造器
*
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 26df3a4

Please sign in to comment.