Skip to content

Commit

Permalink
抽象工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjianguo committed Aug 4, 2016
1 parent 1d11dfd commit 7a51fb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/github/kristofa/brave/IPConversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.kristofa.brave;

/**
* Created by chenjg on 16/8/4.
*/
public class IPConversion {

public static int convertToInt(String ipAddr){
String[] p4 = ipAddr.split("\\.");
int ipInt = 0;
int part = Integer.valueOf(p4[0]);
ipInt = ipInt | (part << 24);
part = Integer.valueOf(p4[1]);
ipInt = ipInt | (part << 16);
part = Integer.valueOf(p4[2]);
ipInt = ipInt | (part << 8);
part = Integer.valueOf(p4[3]);
ipInt = ipInt | (part);
return ipInt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.RpcContext;
import com.github.kristofa.brave.ClientRequestAdapter;
import com.github.kristofa.brave.IdConversion;
import com.github.kristofa.brave.KeyValueAnnotation;
import com.github.kristofa.brave.SpanId;
import com.github.kristofa.brave.*;
import com.github.kristofa.brave.internal.Nullable;
import com.twitter.zipkin.gen.Endpoint;

Expand Down Expand Up @@ -53,21 +50,9 @@ public Collection<KeyValueAnnotation> requestAnnotations() {
public Endpoint serverAddress() {
InetSocketAddress inetSocketAddress = RpcContext.getContext().getRemoteAddress();
String application = RpcContext.getContext().getUrl().getParameter("application");

return Endpoint.create(application,ip2Int(RpcContext.getContext().getUrl().getIp()),inetSocketAddress.getPort());
String ipAddr = RpcContext.getContext().getUrl().getIp();
return Endpoint.create(application, IPConversion.convertToInt(ipAddr),inetSocketAddress.getPort());
}

public int ip2Int(String ip) {
String[] p4 = ip.split("\\.");
int ipInt = 0;
int part = Integer.valueOf(p4[0]);
ipInt = ipInt | (part << 24);
part = Integer.valueOf(p4[1]);
ipInt = ipInt | (part << 16);
part = Integer.valueOf(p4[2]);
ipInt = ipInt | (part << 8);
part = Integer.valueOf(p4[3]);
ipInt = ipInt | (part);
return ipInt;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Collection<KeyValueAnnotation> requestAnnotations() {
String application = RpcContext.getContext().getUrl().getParameter("application");
String ipAddr = RpcContext.getContext().getUrl().getIp();
InetSocketAddress inetSocketAddress = RpcContext.getContext().getLocalAddress();
serverTracer.setServerReceived(ip2Int(ipAddr),inetSocketAddress.getPort(),application);
serverTracer.setServerReceived(IPConversion.convertToInt(ipAddr),inetSocketAddress.getPort(),application);

InetSocketAddress socketAddress = RpcContext.getContext().getLocalAddress();
if (socketAddress != null) {
Expand All @@ -77,17 +77,5 @@ static SpanId getSpanId(String traceId, String spanId, String parentSpanId) {
.parentId(parentSpanId == null ? null : convertToLong(parentSpanId)).build();
}

public int ip2Int(String ip) {
String[] p4 = ip.split("\\.");
int ipInt = 0;
int part = Integer.valueOf(p4[0]);
ipInt = ipInt | (part << 24);
part = Integer.valueOf(p4[1]);
ipInt = ipInt | (part << 16);
part = Integer.valueOf(p4[2]);
ipInt = ipInt | (part << 8);
part = Integer.valueOf(p4[3]);
ipInt = ipInt | (part);
return ipInt;
}

}

0 comments on commit 7a51fb7

Please sign in to comment.