motan loadBalance .

This commit is contained in:
Administrator 2018-12-19 15:16:14 +08:00
parent 324e9673ef
commit 0646f0b184
4 changed files with 115 additions and 22 deletions

View File

@ -17,7 +17,7 @@
package org.dromara.hmily.common.utils;
import java.util.UUID;
import java.util.Random;
/**
* snow flow .
@ -28,30 +28,32 @@ public final class IdWorkerUtils {
private static final IdWorkerUtils ID_WORKER_UTILS = new IdWorkerUtils();
private final long twepoch = 1288834974657L;
private static final Random RANDOM = new Random();
private final long workerIdBits = 5L;
private static final long WORKER_ID_BITS = 5L;
private final long datacenterIdBits = 5L;
private static final long DATACENTERIDBITS = 5L;
private final long maxWorkerId = ~(-1L << workerIdBits);
private static final long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS);
private final long maxDatacenterId = ~(-1L << datacenterIdBits);
private static final long MAX_DATACENTER_ID = ~(-1L << DATACENTERIDBITS);
private final long sequenceBits = 12L;
private static final long SEQUENCE_BITS = 12L;
private final long workerIdShift = sequenceBits;
private static final long WORKER_ID_SHIFT = SEQUENCE_BITS;
private final long datacenterIdShift = sequenceBits + workerIdBits;
private static final long DATACENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private static final long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATACENTERIDBITS;
private final long sequenceMask = ~(-1L << sequenceBits);
private static final long SEQUENCE_MASK = ~(-1L << SEQUENCE_BITS);
private long workerId;
private long datacenterId;
private long idepoch;
private long sequence = 0L;
private long lastTimestamp = -1L;
@ -66,27 +68,28 @@ public final class IdWorkerUtils {
}
private IdWorkerUtils() {
this(RANDOM.nextInt((int) MAX_WORKER_ID), RANDOM.nextInt((int) MAX_DATACENTER_ID), 1288834974657L);
}
private IdWorkerUtils(final long workerId, final long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
private IdWorkerUtils(final long workerId, final long datacenterId, final long idepoch) {
if (workerId > MAX_WORKER_ID || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", MAX_WORKER_ID));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
if (datacenterId > MAX_DATACENTER_ID || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", MAX_DATACENTER_ID));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
this.idepoch = idepoch;
}
private synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
sequence = (sequence + 1) & SEQUENCE_MASK;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
@ -96,7 +99,9 @@ public final class IdWorkerUtils {
lastTimestamp = timestamp;
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence;
return ((timestamp - idepoch) << TIMESTAMP_LEFT_SHIFT)
| (datacenterId << DATACENTER_ID_SHIFT)
| (workerId << WORKER_ID_SHIFT) | sequence;
}
private long tilNextMillis(long lastTimestamp) {
@ -126,6 +131,6 @@ public final class IdWorkerUtils {
* @return the string
*/
public String createUUID() {
return String.valueOf(UUID.randomUUID().hashCode() & 0x7fffffff);
return String.valueOf(ID_WORKER_UTILS.nextId());
}
}

View File

@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hmily.demo.springcloud.account;
import org.mybatis.spring.annotation.MapperScan;
@ -26,7 +27,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* The type Springcloud tcc account application.
* The type SpringCloud tcc account application.
*
* @author xiaoyu
*/

View File

@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dromara.hmily.motan.loadbalance;
import com.google.common.collect.Maps;
import com.weibo.api.motan.cluster.loadbalance.RandomLoadBalance;
import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.core.extension.Activation;
import com.weibo.api.motan.core.extension.SpiMeta;
import com.weibo.api.motan.rpc.Referer;
import com.weibo.api.motan.rpc.Request;
import com.weibo.api.motan.rpc.URL;
import org.dromara.hmily.common.bean.context.HmilyTransactionContext;
import org.dromara.hmily.common.enums.HmilyActionEnum;
import org.dromara.hmily.core.concurrent.threadlocal.HmilyTransactionContextLocal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* The type Motan hmily load balance.
*
* @param <T> the type parameter
* @author xiaoyu(Myth)
*/
@SpiMeta(name = "hmily")
@Activation(key = {MotanConstants.NODE_TYPE_SERVICE, MotanConstants.NODE_TYPE_REFERER})
public class MotanHmilyLoadBalance<T> extends RandomLoadBalance<T> {
private static final Map<String, URL> URL_MAP = Maps.newConcurrentMap();
@Override
protected Referer<T> doSelect(final Request request) {
final Referer<T> referer = super.doSelect(request);
final List<Referer<T>> refererList = getReferers();
final HmilyTransactionContext hmilyTransactionContext = HmilyTransactionContextLocal.getInstance().get();
if (Objects.isNull(hmilyTransactionContext)) {
return referer;
}
final String transId = hmilyTransactionContext.getTransId();
//if try
if (hmilyTransactionContext.getAction() == HmilyActionEnum.TRYING.getCode()) {
URL_MAP.put(transId, referer.getUrl());
return referer;
}
final URL orlUrl = URL_MAP.get(transId);
URL_MAP.remove(transId);
if (Objects.nonNull(orlUrl)) {
for (Referer<T> inv : refererList) {
if (Objects.equals(inv.getUrl(), orlUrl)) {
return inv;
}
}
}
return referer;
}
@Override
protected void doSelectToHolder(final Request request, final List<Referer<T>> refersHolder) {
super.doSelectToHolder(request, refersHolder);
}
}

View File

@ -0,0 +1 @@
org.dromara.hmily.motan.loadbalance.MotanHmilyLoadBalance