Skip to content

Commit e760806

Browse files
committed
优化项目
1 parent 2f2cfd8 commit e760806

File tree

8 files changed

+40
-14
lines changed

8 files changed

+40
-14
lines changed

src/main/java/com/twjitm/core/bootstrap/Bootstrap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class Bootstrap {
3434

3535
public static void main(String[] args) {
3636
getBuddha();
37+
3738
SystemService.getSystem();
3839
Bootstrap.init();
3940
Bootstrap.startServer();

src/main/java/com/twjitm/core/common/config/global/NettyGameServiceConfig.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public class NettyGameServiceConfig {
7474
* 是否开启生命周期任务
7575
*/
7676
private boolean lifeCycleOpen;
77+
/**
78+
* 是否开启kafka组件
79+
*/
80+
private boolean kakfkaOpen;
7781

7882

7983
public void init() {
@@ -95,7 +99,8 @@ public void init() {
9599
rpcSendProxyThreadSize = Integer.parseInt(properties.getProperty("rpcSendProxyThreadSize"));
96100
rpcOpen = Boolean.parseBoolean(properties.getProperty("rpcOpen"));
97101
zookeeperOpen = Boolean.parseBoolean(properties.getProperty("zookeeperOpen"));
98-
lifeCycleOpen=Boolean.parseBoolean(properties.getProperty("lifeCycleOpen"));
102+
lifeCycleOpen = Boolean.parseBoolean(properties.getProperty("lifeCycleOpen"));
103+
kakfkaOpen=Boolean.parseBoolean(properties.getProperty("kafkaOpen"));
99104
} catch (IOException e) {
100105
e.printStackTrace();
101106
}
@@ -205,4 +210,8 @@ public boolean isLifeCycleOpen() {
205210
public void setLifeCycleOpen(boolean lifeCycleOpen) {
206211
this.lifeCycleOpen = lifeCycleOpen;
207212
}
213+
214+
public boolean isKakfkaOpen() {
215+
return kakfkaOpen;
216+
}
208217
}

src/main/java/com/twjitm/core/common/utils/PackageScaner.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ public static void main(String[] args) {
134134

135135
/**
136136
* get parent class to subclass
137-
* @param parentClass
138-
* @param packagePath
139-
* @return
137+
*
138+
* @param parentClass 父类
139+
* @param packagePath 位置
140+
* @return 子类集和
140141
*/
141142
public static List<Class> getSubClasses(final Class parentClass,
142143
final String packagePath) {

src/main/java/com/twjitm/core/service/rpc/service/HelloWorldImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package com.twjitm.core.service.rpc.service;import com.twjitm.core.common.annotation.NettyRpcServiceAnnotation;import com.twjitm.core.utils.logs.LoggerUtils;import org.apache.log4j.Logger;import org.springframework.stereotype.Repository;/** * @author twjitm - [Created on 2018-08-20 10:26] * @jdk java version "1.8.0_77" * 这个地方一定是注入的是业务对象的接口~~~~~~~~~~~~~~~~ */@NettyRpcServiceAnnotation(IHelloWorld.class)@Repositorypublic class HelloWorldImpl implements IHelloWorld { private Logger logger = LoggerUtils.getLogger(HelloWorldImpl.class); @Override public String getHelloWorld(int number) { StringBuilder builder = new StringBuilder(); for (int i = number; i > 0; i--) { builder.append("helloworld"); builder.append(i); } System.out.println("rpc 远程调用方法成功。。。。。,即将返回给远程客户端调用"); return builder.toString(); }}
1+
package com.twjitm.core.service.rpc.service;import com.twjitm.core.common.annotation.NettyRpcServiceAnnotation;import com.twjitm.core.utils.logs.LoggerUtils;import org.apache.log4j.Logger;import org.springframework.stereotype.Repository;/** * @author twjitm - [Created on 2018-08-20 10:26] * @jdk java version "1.8.0_77" * 这个地方一定是注入的是业务对象的接口~~~~~~~~~~~~~~~~ */@NettyRpcServiceAnnotation(IHelloWorld.class)@Repositorypublic class HelloWorldImpl implements IHelloWorld { private Logger logger = LoggerUtils.getLogger(HelloWorldImpl.class); @Override public String getHelloWorld(int number) { StringBuilder builder = new StringBuilder(); for (int i = number; i > 0; i--) { builder.append("helloworld"); builder.append(i); } System.out.println("rpc 远程调用方法成功。。。。。,即将返回给远程客户端调用"); return builder.toString(); } class Task implements Runnable{ @Override public void run() { } }}

src/main/java/com/twjitm/core/spring/SpringServiceManager.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.twjitm.core.spring;
22

3+
import com.twjitm.core.common.config.global.NettyGameServiceConfig;
34
import org.apache.log4j.Logger;
45
import org.springframework.context.support.ClassPathXmlApplicationContext;
56

7+
import java.util.ArrayList;
8+
import java.util.List;
9+
610
/**
711
* spring service manager
812
*
@@ -13,14 +17,24 @@ public class SpringServiceManager {
1317
static Logger logger = Logger.getLogger(SpringServiceManager.class);
1418

1519
public static void init() {
20+
NettyGameServiceConfig gameServiceConfig=new NettyGameServiceConfig();
21+
gameServiceConfig.init();
1622
logger.info("--------------------init spring bootstrap----------------");
23+
List<String> application = new ArrayList<>();
24+
application.add("classpath:spring/applicationContext.xml");
25+
application.add("classpath:spring/applicationContext-spring.xml");
26+
application.add("classpath:spring/applicationContext-async-task.xml");
27+
if(gameServiceConfig.isKakfkaOpen()){
28+
application.add("classpath:spring/applicationContext-kafka-producer.xml");
29+
application.add("classpath:spring/applicationContext-kafka.consumer.xml");
30+
}
31+
32+
String[] strings = new String[application.size()];
33+
for (int i=0;i<application.size();i++) {
34+
strings[i]=application.get(i);
35+
}
36+
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(strings);
1737

18-
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
19-
"classpath:spring/applicationContext.xml",
20-
"classpath:spring/applicationContext-spring.xml",
21-
"classpath:spring/applicationContext-async-task.xml",
22-
"classpath:spring/applicationContext-kafka-producer.xml",
23-
"classpath:spring/applicationContext-kafka.consumer.xml");
2438

2539
logger.info("--------------------init spring end------------------");
2640
springLoadService = (SpringLoadServiceImpl) applicationContext.getBean("springLoadService");

src/main/resources/bean/game-properties.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#版本号serverVersion=0.0.100#主机地址serverHost=0.0.0.0#端口号serverPort=11010#服务器idserverId=18001#rpc服务的包名字rpcServicePackageName=com.twjitm.core.service.rpc.service#异步线程池核心大小asyncThreadPoolCoreSize=5#异步线程最大大小asyncThreadPoolMaxSize=5#rpc回调超时时间rpcFutureDeleteTimeOut=2000#rpc请求超时时间rpcTimeOut=5000#rpc连接线程池大小rpcConnectThreadSize=5#rpc连接线程池大小rpcSendProxyThreadSize=5#开启rpcrpcOpen=true#是否开启zookeeperzookeeperOpen=false#是否开启周期性检测lifeCycleOpen=false
1+
#版本号serverVersion=0.0.100#主机地址serverHost=0.0.0.0#端口号serverPort=11010#服务器idserverId=18001#rpc服务的包名字rpcServicePackageName=com.twjitm.core.service.rpc.service#异步线程池核心大小asyncThreadPoolCoreSize=5#异步线程最大大小asyncThreadPoolMaxSize=5#rpc回调超时时间rpcFutureDeleteTimeOut=2000#rpc请求超时时间rpcTimeOut=5000#rpc连接线程池大小rpcConnectThreadSize=5#rpc连接线程池大小rpcSendProxyThreadSize=5#开启rpcrpcOpen=true#是否开启zookeeperzookeeperOpen=false#是否开启周期性检测lifeCycleOpen=false#是否开启kafkakafkaOpen=true

src/main/resources/bean/rpc-server-register.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<servers>
33

44
<world>
5-
<server serverId="8001" domain="127.0.0.1" domainPort="11010" ip="127.0.0.1" port="11010" weight="100" maxNumber="1000" rpcPort="11040" rpcClientNumber="3"/>
5+
<server serverId="18001" domain="127.0.0.1" domainPort="11010" ip="127.0.0.1" port="11010" weight="100" maxNumber="1000" rpcPort="11040" rpcClientNumber="3"/>
6+
67
<server serverId="9001" domain="47.93.22.194" domainPort="11010" ip="47.93.22.194" port="11010" weight="100" maxNumber="1000" rpcPort="11040" rpcClientNumber="3"/>
78
</world>
89

src/test/java/com/twjitm/rpc/HelloWorldServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package com.twjitm.rpc;import com.twjitm.TestSpring;import com.twjitm.core.common.enums.NettyGameTypeEnum;import com.twjitm.core.common.service.rpc.client.NettyRpcContextHolder;import com.twjitm.core.common.service.rpc.client.NettyRpcContextHolderObject;import com.twjitm.core.common.service.rpc.service.NettyRpcProxyService;import com.twjitm.core.service.rpc.service.IHelloWorld;import com.twjitm.core.spring.SpringServiceManager;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * @author EGLS0807 - [Created on 2018-08-20 15:16] * @company http://www.g2us.com/ * @jdk java version "1.8.0_77" * * 同步阻塞调用 */public class HelloWorldServiceTest { Logger logger=LoggerFactory.getLogger(HelloWorldServiceTest.class); private NettyRpcProxyService nettyRpcProxyService; public static void main(String[] args) { TestSpring.initSpring(); HelloWorldServiceTest helloServiceTest = new HelloWorldServiceTest(); helloServiceTest.init(); helloServiceTest.helloTest1(); helloServiceTest.setTear(); } private void init() { nettyRpcProxyService=SpringServiceManager.getSpringLoadService().getNettyRpcProxyService(); } private void helloTest1() { IHelloWorld helloWorld = nettyRpcProxyService.createProxy(IHelloWorld.class); int serverId=9001; NettyRpcContextHolderObject rpcContextHolderObject = new NettyRpcContextHolderObject(NettyGameTypeEnum.WORLD, serverId); NettyRpcContextHolder.setContextHolder(rpcContextHolderObject); String result = helloWorld.getHelloWorld(5); logger.info(result); } public void setTear(){ if (nettyRpcProxyService != null) { try { nettyRpcProxyService.shutdown(); } catch (Exception e) { e.printStackTrace(); } } }}
1+
package com.twjitm.rpc;import com.twjitm.TestSpring;import com.twjitm.core.common.enums.NettyGameTypeEnum;import com.twjitm.core.common.service.rpc.client.NettyRpcContextHolder;import com.twjitm.core.common.service.rpc.client.NettyRpcContextHolderObject;import com.twjitm.core.common.service.rpc.service.NettyRpcProxyService;import com.twjitm.core.service.rpc.service.IHelloWorld;import com.twjitm.core.spring.SpringServiceManager;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * @author EGLS0807 - [Created on 2018-08-20 15:16] * @company http://www.g2us.com/ * @jdk java version "1.8.0_77" * * 同步阻塞调用 */public class HelloWorldServiceTest { Logger logger=LoggerFactory.getLogger(HelloWorldServiceTest.class); private NettyRpcProxyService nettyRpcProxyService; public static void main(String[] args) { TestSpring.initSpring(); HelloWorldServiceTest helloServiceTest = new HelloWorldServiceTest(); helloServiceTest.init(); helloServiceTest.helloTest1(); helloServiceTest.setTear(); } private void init() { nettyRpcProxyService=SpringServiceManager.getSpringLoadService().getNettyRpcProxyService(); } private void helloTest1() { IHelloWorld helloWorld = nettyRpcProxyService.createProxy(IHelloWorld.class); int serverId=18001; NettyRpcContextHolderObject rpcContextHolderObject = new NettyRpcContextHolderObject(NettyGameTypeEnum.WORLD, serverId); NettyRpcContextHolder.setContextHolder(rpcContextHolderObject); String result = helloWorld.getHelloWorld(5); logger.info(result); } public void setTear(){ if (nettyRpcProxyService != null) { try { nettyRpcProxyService.shutdown(); } catch (Exception e) { e.printStackTrace(); } } }}

0 commit comments

Comments
 (0)