From 14e31717226ad1bac7ba0b9d8f34ab8bd92d0e8b Mon Sep 17 00:00:00 2001 From: WuXianChaoPin <1029559041@qq.com> Date: Thu, 28 Jun 2018 18:35:34 +0800 Subject: [PATCH] thrift --- .../main/java/core/thrift/FactoryPool.java | 72 ++++++++++++++++++ .../main/java/core/thrift/SocketConfig.java | 74 +------------------ .../core/thrift/TNonblockingSocketPool.java | 8 +- .../main/java/core/thrift/TSocketPool.java | 8 +- .../main/java/core/thrift/TTransportPool.java | 15 +--- sql/new.sql | 2 + web/src/main/resources/config.properties | 5 +- .../test/java/spring/methodreplace/Add.java | 12 +++ .../java/spring/methodreplace/SpringTest.java | 25 +++++++ web/src/test/java/thrift/ThriftTest.java | 8 +- 10 files changed, 133 insertions(+), 96 deletions(-) create mode 100644 core/src/main/java/core/thrift/FactoryPool.java create mode 100644 web/src/test/java/spring/methodreplace/Add.java create mode 100644 web/src/test/java/spring/methodreplace/SpringTest.java diff --git a/core/src/main/java/core/thrift/FactoryPool.java b/core/src/main/java/core/thrift/FactoryPool.java new file mode 100644 index 0000000..4d4d8e2 --- /dev/null +++ b/core/src/main/java/core/thrift/FactoryPool.java @@ -0,0 +1,72 @@ +package core.thrift; + +import org.apache.commons.pool2.impl.GenericObjectPool; +import org.apache.thrift.TServiceClient; +import org.apache.thrift.async.TAsyncClient; +import org.apache.thrift.async.TAsyncClientManager; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.protocol.TProtocolFactory; +import org.apache.thrift.transport.TNonblockingSocket; +import org.apache.thrift.transport.TNonblockingTransport; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Constructor; +import java.util.HashMap; +import java.util.Map; + +@Component +public class FactoryPool { + private GenericObjectPool tNonblockingSocketPool; + + private GenericObjectPool tSocketPool; + + + public FactoryPool(TNonblockingSocketPool pool1, TSocketPool pool2) { + this.tNonblockingSocketPool = new GenericObjectPool<>(pool1); + this.tSocketPool = new GenericObjectPool<>(pool2); + } + + private final Map clientMap = new HashMap<>(); + + public void doExecute(Class c, AsyncClient thrift) throws Exception { + //从池里获取一个Transport对象 + TNonblockingSocket trans = tNonblockingSocketPool.borrowObject(); + thrift.doExecute(client(c, trans)); + //把一个Transport对象归还到池里 + tNonblockingSocketPool.returnObject(trans); + } + + public void doExecute(Class c, ServiceClient thrift) throws Exception{ + TSocket trans=tSocketPool.borrowObject(); + thrift.doExecute(client(c,trans)); + tSocketPool.returnObject(trans); + } + + private interface Thrift { + void doExecute(E client) throws Exception; + } + + public interface AsyncClient extends Thrift{} + + public interface ServiceClient extends Thrift{} + + public C client(Class c, T trans) throws Exception { + if (trans instanceof TNonblockingTransport) { + if (!clientMap.containsKey(c)) { + clientMap.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class)); + } + TProtocolFactory protocol = new TBinaryProtocol.Factory(); + TAsyncClientManager clientManager = new TAsyncClientManager(); + return (C) clientMap.get(c).newInstance(protocol, clientManager, trans); + } else { + if (!clientMap.containsKey(c)) { + clientMap.put(c, c.getConstructor(TProtocol.class)); + } + TBinaryProtocol protocol = new TBinaryProtocol(trans); + return (C) clientMap.get(c).newInstance(protocol); + } + } +} diff --git a/core/src/main/java/core/thrift/SocketConfig.java b/core/src/main/java/core/thrift/SocketConfig.java index 029b00f..a91d270 100644 --- a/core/src/main/java/core/thrift/SocketConfig.java +++ b/core/src/main/java/core/thrift/SocketConfig.java @@ -1,85 +1,17 @@ package core.thrift; -import org.apache.commons.pool2.impl.GenericObjectPool; -import org.apache.thrift.TServiceClient; -import org.apache.thrift.async.TAsyncClient; -import org.apache.thrift.async.TAsyncClientManager; -import org.apache.thrift.protocol.TBinaryProtocol; -import org.apache.thrift.protocol.TProtocol; -import org.apache.thrift.protocol.TProtocolFactory; -import org.apache.thrift.transport.TNonblockingSocket; -import org.apache.thrift.transport.TNonblockingTransport; -import org.apache.thrift.transport.TSocket; -import org.apache.thrift.transport.TTransport; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; -import java.lang.reflect.Constructor; -import java.util.HashMap; -import java.util.Map; - @Component public class SocketConfig { @Value("${thrift.ip}") - private String ip; + public String ip; @Value("${thrift.port}") - private Integer port; + public Integer port; @Value("${thrift.timeout}") - private Integer timeout; - - - private GenericObjectPool tNonblockingSocketPool; - - private GenericObjectPool tSocketPool; - - - private final Map clientMap = new HashMap<>(); - - @PostConstruct - public void init(){ - tNonblockingSocketPool=new GenericObjectPool<>(new TNonblockingSocketPool(ip,port,timeout)); - tSocketPool=new GenericObjectPool<>(new TSocketPool(ip,port,timeout)); - } - - public void doExecute(Class c, AsyncClient thrift) throws Exception { - //从池里获取一个Transport对象 - TNonblockingSocket trans = tNonblockingSocketPool.borrowObject(); - thrift.doExecute(client(c, trans)); - //把一个Transport对象归还到池里 - tNonblockingSocketPool.returnObject(trans); - } - - public void doExecute(Class c,ServiceClient thrift) throws Exception{ - TSocket trans=tSocketPool.borrowObject(); - thrift.doExecute(client(c,trans)); - tSocketPool.returnObject(trans); - } - - private interface Thrift { - void doExecute(E client) throws Exception; - } - - public interface AsyncClient extends Thrift{} - - public interface ServiceClient extends Thrift{} + public Integer timeout; - public C client(Class c, T trans) throws Exception { - if (trans instanceof TNonblockingTransport) { - if (!clientMap.containsKey(c)) { - clientMap.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class)); - } - TProtocolFactory protocol = new TBinaryProtocol.Factory(); - TAsyncClientManager clientManager = new TAsyncClientManager(); - return (C) clientMap.get(c).newInstance(protocol, clientManager, trans); - } else { - if (!clientMap.containsKey(c)) { - clientMap.put(c, c.getConstructor(TProtocol.class)); - } - TBinaryProtocol protocol = new TBinaryProtocol(trans); - return (C) clientMap.get(c).newInstance(protocol); - } - } } diff --git a/core/src/main/java/core/thrift/TNonblockingSocketPool.java b/core/src/main/java/core/thrift/TNonblockingSocketPool.java index 0df2ca7..c78db2a 100644 --- a/core/src/main/java/core/thrift/TNonblockingSocketPool.java +++ b/core/src/main/java/core/thrift/TNonblockingSocketPool.java @@ -2,14 +2,16 @@ package core.thrift; import org.apache.thrift.async.TAsyncClient; import org.apache.thrift.transport.TNonblockingSocket; +import org.springframework.stereotype.Component; import java.net.Socket; +@Component public class TNonblockingSocketPool extends TTransportPool { - public TNonblockingSocketPool(String ip, Integer port, Integer timeout) { - super(ip, port, timeout); + public TNonblockingSocketPool(SocketConfig config) { + super(config); } @Override @@ -19,7 +21,7 @@ public class TNonblockingSocketPool extends TTransportPool{ - public TSocketPool(String ip, Integer port, Integer timeout) { - super(ip, port, timeout); + public TSocketPool(SocketConfig config) { + super(config); } @Override @@ -19,7 +21,7 @@ public class TSocketPool extends TTransportPool{ @Override public TSocket create() throws Exception { - TSocket socket=new TSocket(ip,port,timeout); + TSocket socket=new TSocket(config.ip,config.port,config.timeout); socket.open(); return socket; } diff --git a/core/src/main/java/core/thrift/TTransportPool.java b/core/src/main/java/core/thrift/TTransportPool.java index 9f7e292..cc9c1ad 100644 --- a/core/src/main/java/core/thrift/TTransportPool.java +++ b/core/src/main/java/core/thrift/TTransportPool.java @@ -9,16 +9,10 @@ import java.net.Socket; public abstract class TTransportPool extends BasePooledObjectFactory { - protected String ip; + protected SocketConfig config; - protected Integer port; - - protected Integer timeout; - - public TTransportPool(String ip, Integer port, Integer timeout) { - this.ip = ip; - this.port = port; - this.timeout = timeout; + public TTransportPool(SocketConfig config) { + this.config = config; } @Override @@ -61,9 +55,6 @@ public abstract class TTransportPool extends BasePooled public void destroyObject(PooledObject p) { p.getObject().close(); } - - - } diff --git a/sql/new.sql b/sql/new.sql index e031393..0e0e489 100644 --- a/sql/new.sql +++ b/sql/new.sql @@ -14,5 +14,7 @@ create table menu ) engine=InnoDB ; +--thrift服务器表 +create diff --git a/web/src/main/resources/config.properties b/web/src/main/resources/config.properties index 88fab0b..ae1cef6 100644 --- a/web/src/main/resources/config.properties +++ b/web/src/main/resources/config.properties @@ -9,7 +9,6 @@ hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver druid.maxActive=20 #thrift\u914D\u7F6E -thrift.ip=127.0.0.1 +thrift.ip=sukura.top thrift.port=2233 -thrift.timeout=3000 - +thrift.timeout=3000 \ No newline at end of file diff --git a/web/src/test/java/spring/methodreplace/Add.java b/web/src/test/java/spring/methodreplace/Add.java new file mode 100644 index 0000000..965cbb2 --- /dev/null +++ b/web/src/test/java/spring/methodreplace/Add.java @@ -0,0 +1,12 @@ +package spring.methodreplace; + +import org.springframework.stereotype.Component; + +@Component +public class Add { + + + public void add(){ + System.out.println("Add"); + } +} diff --git a/web/src/test/java/spring/methodreplace/SpringTest.java b/web/src/test/java/spring/methodreplace/SpringTest.java new file mode 100644 index 0000000..c053f94 --- /dev/null +++ b/web/src/test/java/spring/methodreplace/SpringTest.java @@ -0,0 +1,25 @@ +package spring.methodreplace; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.annotation.Resource; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitWebConfig +@ContextConfiguration() +public class SpringTest { + + @Resource + private Add add; + + @Test + public void test1(){ + add.add(); + } + +} + diff --git a/web/src/test/java/thrift/ThriftTest.java b/web/src/test/java/thrift/ThriftTest.java index a334b1f..ad99004 100644 --- a/web/src/test/java/thrift/ThriftTest.java +++ b/web/src/test/java/thrift/ThriftTest.java @@ -1,7 +1,7 @@ package thrift; +import core.thrift.FactoryPool; import core.thrift.MyAsyncMethodCallback; -import core.thrift.SocketConfig; import core.thrift.task.TSDM; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -20,21 +20,21 @@ import javax.annotation.Resource; public class ThriftTest { @Resource - private SocketConfig config; + private FactoryPool factoryPool; private static Logger log = LogManager.getLogger(); @Test public void test1() throws Exception { MyAsyncMethodCallback call=new MyAsyncMethodCallback<>(); - config.doExecute(TSDM.AsyncClient.class,client -> client.qiandao(call)); + factoryPool.doExecute(TSDM.AsyncClient.class,client -> client.qiandao(call)); log.info(call.getResult()); } @Test public void test2() throws Exception{ - config.doExecute(TSDM.Client.class,client -> log.info(client.qiandao())); + factoryPool.doExecute(TSDM.Client.class, client -> log.info(client.qiandao())); } }