Authored by tanling

订单号增加机器码

  1 +package com.yohoufo.common.utils;
  2 +
  3 +import org.slf4j.Logger;
  4 +import org.slf4j.LoggerFactory;
  5 +
  6 +import java.net.InetAddress;
  7 +import java.net.NetworkInterface;
  8 +import java.net.SocketException;
  9 +import java.util.Enumeration;
  10 +
  11 +public class IpUtils {
  12 + private static final Logger log = LoggerFactory.getLogger(IpUtils.class);
  13 +
  14 + public static String getHostIp() {
  15 + String ip = null;
  16 + try {
  17 + Enumeration<NetworkInterface> en = NetworkInterface
  18 + .getNetworkInterfaces();
  19 + while (en.hasMoreElements()) {
  20 + NetworkInterface intf = (NetworkInterface) en.nextElement();
  21 + Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
  22 + while (enumIpAddr.hasMoreElements()) {
  23 + InetAddress inetAddress = (InetAddress) enumIpAddr
  24 + .nextElement();
  25 + if (!inetAddress.isLoopbackAddress()
  26 + && !inetAddress.isLinkLocalAddress()
  27 + && inetAddress.isSiteLocalAddress()) {
  28 + ip = inetAddress.getHostAddress();
  29 + }
  30 + }
  31 + }
  32 + } catch (SocketException e) {
  33 + log.error("Fail to get IP address.", e);
  34 + }
  35 + return ip;
  36 + }
  37 +
  38 + public static String getHostName() {
  39 + String hostName = null;
  40 + try {
  41 + Enumeration<NetworkInterface> en = NetworkInterface
  42 + .getNetworkInterfaces();
  43 + while (en.hasMoreElements()) {
  44 + NetworkInterface intf = (NetworkInterface) en.nextElement();
  45 + Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
  46 + while (enumIpAddr.hasMoreElements()) {
  47 + InetAddress inetAddress = (InetAddress) enumIpAddr
  48 + .nextElement();
  49 + if (!inetAddress.isLoopbackAddress()
  50 + && !inetAddress.isLinkLocalAddress()
  51 + && inetAddress.isSiteLocalAddress()) {
  52 + hostName = inetAddress.getHostName();
  53 + }
  54 + }
  55 + }
  56 + } catch (SocketException e) {
  57 + log.error("Fail to get host name.", e);
  58 + }
  59 + return hostName;
  60 + }
  61 +
  62 +}
@@ -6,6 +6,8 @@ import com.yohoufo.order.service.support.codegenerator.bean.CodeBitMeta; @@ -6,6 +6,8 @@ import com.yohoufo.order.service.support.codegenerator.bean.CodeBitMeta;
6 import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta; 6 import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta;
7 import com.yohoufo.order.service.support.codegenerator.converter.IdConverterHelper; 7 import com.yohoufo.order.service.support.codegenerator.converter.IdConverterHelper;
8 import com.yohoufo.order.service.support.codegenerator.populater.AtomicIdPopulator; 8 import com.yohoufo.order.service.support.codegenerator.populater.AtomicIdPopulator;
  9 +import com.yohoufo.order.service.support.codegenerator.populater.MachineIdProvider;
  10 +import com.yohoufo.order.service.support.codegenerator.populater.PropMachineIdProvider;
9 import com.yohoufo.order.service.support.codegenerator.timer.SimpleTimer; 11 import com.yohoufo.order.service.support.codegenerator.timer.SimpleTimer;
10 import org.slf4j.Logger; 12 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 13 import org.slf4j.LoggerFactory;
@@ -25,11 +27,9 @@ public class OrderCodeGeneratorImpl implements OrderCodeGenerator { @@ -25,11 +27,9 @@ public class OrderCodeGeneratorImpl implements OrderCodeGenerator {
25 27
26 AtomicIdPopulator idPopulator; 28 AtomicIdPopulator idPopulator;
27 29
28 - /**  
29 - * 机器节点: 取值0-7  
30 - */  
31 - @Value("${env.machineId:0}")  
32 - private long machineId; 30 + MachineIdProvider machineIdProvider;
  31 +
  32 + long machineId = 0l;
33 33
34 @PostConstruct 34 @PostConstruct
35 public void init() { 35 public void init() {
@@ -42,6 +42,12 @@ public class OrderCodeGeneratorImpl implements OrderCodeGenerator { @@ -42,6 +42,12 @@ public class OrderCodeGeneratorImpl implements OrderCodeGenerator {
42 idPopulator = new AtomicIdPopulator(); 42 idPopulator = new AtomicIdPopulator();
43 } 43 }
44 44
  45 + if (machineIdProvider == null){
  46 + machineIdProvider = new PropMachineIdProvider();
  47 + }
  48 +
  49 + machineId = machineIdProvider.getMachineId();
  50 +
45 // 3位 0~8 51 // 3位 0~8
46 validateMachineId(this.machineId); 52 validateMachineId(this.machineId);
47 } 53 }
  1 +package com.yohoufo.order.service.support.codegenerator.populater;
  2 +
  3 +public interface MachineIdProvider {
  4 +
  5 + public long getMachineId();
  6 +}
  1 +package com.yohoufo.order.service.support.codegenerator.populater;
  2 +
  3 +import com.yohoufo.common.utils.IpUtils;
  4 +import org.apache.commons.lang3.StringUtils;
  5 +import org.slf4j.Logger;
  6 +import org.slf4j.LoggerFactory;
  7 +import org.springframework.beans.factory.annotation.Value;
  8 +
  9 +import java.util.HashMap;
  10 +import java.util.Map;
  11 +
  12 +public class PropMachineIdProvider implements MachineIdProvider {
  13 +
  14 + protected final Logger log = LoggerFactory.getLogger(this.getClass());
  15 +
  16 + private Map<String, Long> ipsMap = new HashMap<String, Long>();
  17 +
  18 +
  19 + @Value("${ips.machine}")
  20 + private String machineIps;
  21 +
  22 + @Override
  23 + public long getMachineId() {
  24 +
  25 + String ip = IpUtils.getHostIp();
  26 +
  27 + log.info("ip:{}",ip);
  28 + Long machineId = ipsMap.get(ip);
  29 +
  30 + if (machineId == null){
  31 + return 0l;
  32 + }
  33 +
  34 + return machineId.longValue();
  35 + }
  36 +
  37 + public PropMachineIdProvider() {
  38 + init();
  39 + }
  40 +
  41 + private void init(){
  42 + if (StringUtils.isEmpty(machineIps)) {
  43 + return;
  44 + }
  45 +
  46 + String[] ips = machineIps.split(",");
  47 +
  48 + for (int i=0; i < ips.length; i++){
  49 + ipsMap.put(ips[i], (long)i);
  50 + }
  51 + }
  52 +}
@@ -68,6 +68,7 @@ yoho.recovery.zkPath=/yh/config/recovery @@ -68,6 +68,7 @@ yoho.recovery.zkPath=/yh/config/recovery
68 68
69 env.machineId=0 69 env.machineId=0
70 70
  71 +ips.machine=127.0.0.1
71 72
72 wechat.app.partnerid=1218934901 73 wechat.app.partnerid=1218934901
73 wechat.app.partnerkey=b22de5cfd0ded341e0516505f72649a9 74 wechat.app.partnerkey=b22de5cfd0ded341e0516505f72649a9
@@ -26,7 +26,7 @@ web.context=ufo-gateway @@ -26,7 +26,7 @@ web.context=ufo-gateway
26 #\u5bc6\u7801AES\u52a0\u5bc6\u5bc6\u94a5 26 #\u5bc6\u7801AES\u52a0\u5bc6\u5bc6\u94a5
27 password.aes.key=${password.aes.key} 27 password.aes.key=${password.aes.key}
28 uic.service.url=${uic.service.url} 28 uic.service.url=${uic.service.url}
29 -#Ϣ-url 29 +#��Ϣ����-����url
30 inbox.baseurl=${inbox.baseurl} 30 inbox.baseurl=${inbox.baseurl}
31 31
32 #signature encrypt key salt 32 #signature encrypt key salt
@@ -65,4 +65,7 @@ order.seller.earnestMoneyRate=${order.seller.earnestMoneyRate} @@ -65,4 +65,7 @@ order.seller.earnestMoneyRate=${order.seller.earnestMoneyRate}
65 order.seller.payChannelRate=${order.seller.payChannelRate} 65 order.seller.payChannelRate=${order.seller.payChannelRate}
66 erp-gateway.url=${erp-gateway.url} 66 erp-gateway.url=${erp-gateway.url}
67 uic.url=${uic.url} 67 uic.url=${uic.url}
68 -yoho.message.controller.url=${yoho.message.controller.url}  
  68 +yoho.message.controller.url=${yoho.message.controller.url}
  69 +
  70 +
  71 +ips.machine=${ips.machine}