Authored by tanling

订单号增加机器码

package com.yohoufo.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class IpUtils {
private static final Logger log = LoggerFactory.getLogger(IpUtils.class);
public static String getHostIp() {
String ip = null;
try {
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr
.nextElement();
if (!inetAddress.isLoopbackAddress()
&& !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
ip = inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
log.error("Fail to get IP address.", e);
}
return ip;
}
public static String getHostName() {
String hostName = null;
try {
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr
.nextElement();
if (!inetAddress.isLoopbackAddress()
&& !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
hostName = inetAddress.getHostName();
}
}
}
} catch (SocketException e) {
log.error("Fail to get host name.", e);
}
return hostName;
}
}
... ...
... ... @@ -6,6 +6,8 @@ import com.yohoufo.order.service.support.codegenerator.bean.CodeBitMeta;
import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta;
import com.yohoufo.order.service.support.codegenerator.converter.IdConverterHelper;
import com.yohoufo.order.service.support.codegenerator.populater.AtomicIdPopulator;
import com.yohoufo.order.service.support.codegenerator.populater.MachineIdProvider;
import com.yohoufo.order.service.support.codegenerator.populater.PropMachineIdProvider;
import com.yohoufo.order.service.support.codegenerator.timer.SimpleTimer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -25,11 +27,9 @@ public class OrderCodeGeneratorImpl implements OrderCodeGenerator {
AtomicIdPopulator idPopulator;
/**
* 机器节点: 取值0-7
*/
@Value("${env.machineId:0}")
private long machineId;
MachineIdProvider machineIdProvider;
long machineId = 0l;
@PostConstruct
public void init() {
... ... @@ -42,6 +42,12 @@ public class OrderCodeGeneratorImpl implements OrderCodeGenerator {
idPopulator = new AtomicIdPopulator();
}
if (machineIdProvider == null){
machineIdProvider = new PropMachineIdProvider();
}
machineId = machineIdProvider.getMachineId();
// 3位 0~8
validateMachineId(this.machineId);
}
... ...
package com.yohoufo.order.service.support.codegenerator.populater;
public interface MachineIdProvider {
public long getMachineId();
}
... ...
package com.yohoufo.order.service.support.codegenerator.populater;
import com.yohoufo.common.utils.IpUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import java.util.HashMap;
import java.util.Map;
public class PropMachineIdProvider implements MachineIdProvider {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
private Map<String, Long> ipsMap = new HashMap<String, Long>();
@Value("${ips.machine}")
private String machineIps;
@Override
public long getMachineId() {
String ip = IpUtils.getHostIp();
log.info("ip:{}",ip);
Long machineId = ipsMap.get(ip);
if (machineId == null){
return 0l;
}
return machineId.longValue();
}
public PropMachineIdProvider() {
init();
}
private void init(){
if (StringUtils.isEmpty(machineIps)) {
return;
}
String[] ips = machineIps.split(",");
for (int i=0; i < ips.length; i++){
ipsMap.put(ips[i], (long)i);
}
}
}
... ...
... ... @@ -68,6 +68,7 @@ yoho.recovery.zkPath=/yh/config/recovery
env.machineId=0
ips.machine=127.0.0.1
wechat.app.partnerid=1218934901
wechat.app.partnerkey=b22de5cfd0ded341e0516505f72649a9
... ...
... ... @@ -26,7 +26,7 @@ web.context=ufo-gateway
#\u5bc6\u7801AES\u52a0\u5bc6\u5bc6\u94a5
password.aes.key=${password.aes.key}
uic.service.url=${uic.service.url}
#Ϣ-url
#��Ϣ����-����url
inbox.baseurl=${inbox.baseurl}
#signature encrypt key salt
... ... @@ -65,4 +65,7 @@ order.seller.earnestMoneyRate=${order.seller.earnestMoneyRate}
order.seller.payChannelRate=${order.seller.payChannelRate}
erp-gateway.url=${erp-gateway.url}
uic.url=${uic.url}
yoho.message.controller.url=${yoho.message.controller.url}
\ No newline at end of file
yoho.message.controller.url=${yoho.message.controller.url}
ips.machine=${ips.machine}
\ No newline at end of file
... ...