From 757dded14434121f6d8ace4ebd2af28c0150c93b Mon Sep 17 00:00:00 2001 From: shaowenchang Date: Fri, 5 Jan 2018 09:58:20 +0800 Subject: [PATCH 01/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9zbus=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6channel=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/jboot/core/mq/zbus/JbootZbusmqConfig.java | 10 +++++++++- .../io/jboot/core/mq/zbus/JbootZbusmqImpl.java | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqConfig.java b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqConfig.java index d81edf4a..3ba562f5 100644 --- a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqConfig.java +++ b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqConfig.java @@ -23,7 +23,7 @@ public class JbootZbusmqConfig { private String queue; private String broker; - + private String channel; public String getQueue() { return queue; @@ -40,4 +40,12 @@ public class JbootZbusmqConfig { public void setBroker(String broker) { this.broker = broker; } + + public String getChannel() { + return channel; + } + + public void setChannel(String channel) { + this.channel = channel; + } } diff --git a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java index 38a9438f..0bb3b840 100644 --- a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java +++ b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java @@ -22,7 +22,6 @@ import io.jboot.core.mq.Jbootmq; import io.jboot.core.mq.JbootmqBase; import io.jboot.utils.StringUtils; import io.zbus.mq.*; - import java.io.IOException; import java.util.Map; @@ -32,11 +31,12 @@ public class JbootZbusmqImpl extends JbootmqBase implements Jbootmq, MessageHand private static final Log LOG = Log.getLog(JbootZbusmqImpl.class); private Broker broker; + protected JbootZbusmqConfig zbusmqConfig = Jboot.config(JbootZbusmqConfig.class); + public JbootZbusmqImpl() { initChannels(); - JbootZbusmqConfig zbusmqConfig = Jboot.config(JbootZbusmqConfig.class); broker = new Broker(zbusmqConfig.getBroker()); for (String channel : channels) { @@ -74,6 +74,17 @@ public class JbootZbusmqImpl extends JbootmqBase implements Jbootmq, MessageHand } } + @Override + protected void initChannels() { + String channelString = zbusmqConfig.getChannel(); + if (StringUtils.isBlank(channelString)) { + LOG.warn("jboot.mq.channel is blank or null, please config mq channels when you use."); + return; + } + this.channels.clear(); + this.channels.addAll(StringUtils.splitToSet(channelString, ",")); + } + @Override public void enqueue(Object message, String toChannel) { publish(message, toChannel); -- Gitee From d03a95dfba5c879a6ee6646d72e9fb2cb2d849e7 Mon Sep 17 00:00:00 2001 From: shaojava Date: Mon, 8 Jan 2018 21:53:38 +0800 Subject: [PATCH 02/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../jboot/core/mq/zbus/JbootZbusmqImpl.java | 5 ++- src/test/java/mqzbus/ZbusMqServerDemo.java | 40 +++++++++++++------ 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 70ad5060..3df55624 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.jboot jboot - 1.2.6 + 1.2.7 jar jboot diff --git a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java index 38a9438f..400ca151 100644 --- a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java +++ b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java @@ -43,8 +43,9 @@ public class JbootZbusmqImpl extends JbootmqBase implements Jbootmq, MessageHand ConsumerConfig config = new ConsumerConfig(broker); config.setTopic(channel); config.setMessageHandler(this); - ConsumeGroup group = ConsumeGroup.createTempBroadcastGroup(); - config.setConsumeGroup(group); + //ConsumeGroup group = ConsumeGroup.createTempBroadcastGroup(); + //config.setConsumeGroup(group); + config.setConsumeGroup(channel); Consumer consumer = new Consumer(config); diff --git a/src/test/java/mqzbus/ZbusMqServerDemo.java b/src/test/java/mqzbus/ZbusMqServerDemo.java index d99d1c46..37394446 100644 --- a/src/test/java/mqzbus/ZbusMqServerDemo.java +++ b/src/test/java/mqzbus/ZbusMqServerDemo.java @@ -16,30 +16,44 @@ package mqzbus; import io.jboot.Jboot; +import io.jboot.core.mq.JbootmqMessageListener; +import io.jboot.web.controller.JbootController; +import io.jboot.web.controller.annotation.RequestMapping; +@RequestMapping("/mqs") +public class ZbusMqServerDemo extends JbootController { -public class ZbusMqServerDemo { - + private static int count; + private static String message; public static void main(String[] args) throws InterruptedException { - Jboot.setBootArg("jboot.mq.type", "zbus"); - Jboot.setBootArg("jboot.mq.channel", "myChannel,myChannel1,myChannel2"); - Jboot.setBootArg("jboot.mq.zbus.broker", "127.0.0.1:15555"); + Jboot.setBootArg("jboot.mq.channel", "CpMQ"); - Jboot.run(args); + Jboot.me().getMq().addMessageListener(new JbootmqMessageListener() { + @Override + public void onMessage(String channel, Object message) { + System.out.println("listener:" + message + " channel:" + channel); + ZbusMqServerDemo.message = (String) message; + } + }); + Jboot.run(args); System.out.println("ZbusMqServerDemo started..."); + count = 0; + } + public void index() { + String msg = "message "+(count++); + Jboot.me().getMq().publish(msg, "CpMQ"); + renderText(msg); + } - int i = 0; - for (; ; ) { - Jboot.me().getMq().publish("message" + (i++), "myChannel1"); - Thread.sleep(1000); - } - - + public void add() { + String msg = "message "+(count++); + Jboot.me().getMq().publish(msg, "CpMQ"); + renderText(msg); } } -- Gitee From 8e11c7487ea285bf8ffb3c09d4226312bb8104f7 Mon Sep 17 00:00:00 2001 From: shaojava Date: Mon, 8 Jan 2018 23:07:33 +0800 Subject: [PATCH 03/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jboot/core/mq/zbus/JbootZbusmqConfig.java | 11 +-- .../jboot/core/mq/zbus/JbootZbusmqImpl.java | 89 ++++++++++--------- src/test/java/mqzbus/ZbusMqServerDemo.java | 5 +- 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqConfig.java b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqConfig.java index 3ba562f5..4047e66c 100644 --- a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqConfig.java +++ b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqConfig.java @@ -15,6 +15,7 @@ */ package io.jboot.core.mq.zbus; +import io.jboot.Jboot; import io.jboot.config.annotation.PropertyConfig; @@ -23,7 +24,7 @@ public class JbootZbusmqConfig { private String queue; private String broker; - private String channel; + private String mode = JbootZbusmqImpl.MODE.PRODUCER.getValue(); public String getQueue() { return queue; @@ -41,11 +42,11 @@ public class JbootZbusmqConfig { this.broker = broker; } - public String getChannel() { - return channel; + public String getMode() { + return mode; } - public void setChannel(String channel) { - this.channel = channel; + public void setMode(String mode) { + this.mode = mode; } } diff --git a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java index 76624c59..71853b7a 100644 --- a/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java +++ b/src/main/java/io/jboot/core/mq/zbus/JbootZbusmqImpl.java @@ -39,51 +39,42 @@ public class JbootZbusmqImpl extends JbootmqBase implements Jbootmq, MessageHand broker = new Broker(zbusmqConfig.getBroker()); - for (String channel : channels) { - ConsumerConfig config = new ConsumerConfig(broker); - config.setTopic(channel); - config.setMessageHandler(this); - //ConsumeGroup group = ConsumeGroup.createTempBroadcastGroup(); - //config.setConsumeGroup(group); - config.setConsumeGroup(channel); - Consumer consumer = new Consumer(config); - - - try { - consumer.start(); - } catch (IOException e) { - e.printStackTrace(); + if(zbusmqConfig.getMode().equals(MODE.CONSUMER.getValue())){ + for (String channel : channels) { + ConsumerConfig config = new ConsumerConfig(broker); + config.setTopic(channel); + config.setMessageHandler(this); + //ConsumeGroup group = ConsumeGroup.createTempBroadcastGroup(); + //config.setConsumeGroup(group); + config.setConsumeGroup(channel); + Consumer consumer = new Consumer(config); + + + try { + consumer.start(); + } catch (IOException e) { + e.printStackTrace(); + } } - } - - String queueString = zbusmqConfig.getQueue(); - if (StringUtils.isBlank(queueString)) { - return; - } - String[] queues = queueString.split(","); - for (String channel : queues) { - ConsumerConfig config = new ConsumerConfig(broker); - config.setTopic(channel); - config.setMessageHandler(this); - Consumer consumer = new Consumer(config); - try { - consumer.start(); - } catch (IOException e) { - e.printStackTrace(); + String queueString = zbusmqConfig.getQueue(); + if (StringUtils.isBlank(queueString)) { + return; } - } - } - @Override - protected void initChannels() { - String channelString = zbusmqConfig.getChannel(); - if (StringUtils.isBlank(channelString)) { - LOG.warn("jboot.mq.channel is blank or null, please config mq channels when you use."); - return; + String[] queues = queueString.split(","); + for (String channel : queues) { + ConsumerConfig config = new ConsumerConfig(broker); + config.setTopic(channel); + config.setMessageHandler(this); + Consumer consumer = new Consumer(config); + try { + consumer.start(); + } catch (IOException e) { + e.printStackTrace(); + } + } } - this.channels.clear(); - this.channels.addAll(StringUtils.splitToSet(channelString, ",")); } @Override @@ -129,4 +120,22 @@ public class JbootZbusmqImpl extends JbootmqBase implements Jbootmq, MessageHand public void handle(Message message, MqClient mqClient) throws IOException { notifyListeners(message.getTopic(), Jboot.me().getSerializer().deserialize(message.getBody())); } + + /** + * MQ模式:消费 生成 + */ + public static enum MODE { + + CONSUMER("consumer"), PRODUCER("producer"); + + private final String value; + + MODE(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } } diff --git a/src/test/java/mqzbus/ZbusMqServerDemo.java b/src/test/java/mqzbus/ZbusMqServerDemo.java index 37394446..1125c6b6 100644 --- a/src/test/java/mqzbus/ZbusMqServerDemo.java +++ b/src/test/java/mqzbus/ZbusMqServerDemo.java @@ -29,16 +29,17 @@ public class ZbusMqServerDemo extends JbootController { public static void main(String[] args) throws InterruptedException { Jboot.setBootArg("jboot.mq.type", "zbus"); + Jboot.setBootArg("jboot.mq.zbus.mode", "producer"); Jboot.setBootArg("jboot.mq.zbus.broker", "127.0.0.1:15555"); Jboot.setBootArg("jboot.mq.channel", "CpMQ"); - Jboot.me().getMq().addMessageListener(new JbootmqMessageListener() { + /*Jboot.me().getMq().addMessageListener(new JbootmqMessageListener() { @Override public void onMessage(String channel, Object message) { System.out.println("listener:" + message + " channel:" + channel); ZbusMqServerDemo.message = (String) message; } - }); + });*/ Jboot.run(args); System.out.println("ZbusMqServerDemo started..."); -- Gitee From 921a3a6bf753a8082b9dedcdb98509477f9f93cb Mon Sep 17 00:00:00 2001 From: shaojava Date: Thu, 11 Jan 2018 23:40:51 +0800 Subject: [PATCH 04/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/RedisTest.java | 86 ++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/src/test/java/RedisTest.java b/src/test/java/RedisTest.java index 55f260d6..a70df487 100644 --- a/src/test/java/RedisTest.java +++ b/src/test/java/RedisTest.java @@ -1,4 +1,9 @@ +import io.jboot.Jboot; +import io.jboot.component.redis.JbootRedis; import org.junit.Test; +import service.User; + +import java.util.Set; public class RedisTest { @@ -6,31 +11,64 @@ public class RedisTest { @Test public void testRedis() { -// User user = new User(); -// user.setId(22); -// user.setName("张三历史"); -// -// Jboot.setBootArg("jboot.redis.host", "127.0.0.1"); -//// Jboot.setBootArg("jboot.redis.password", "123456"); -// -// JbootRedis redis = Jboot.me().getRedis(); -// redis.set("mykey", ""); -// -// redis.set("user",user); -// -// redis.lpush("list", 1,2,3,4,5,6); -// -// System.out.println(redis.get("mykey").toString()); -// -// System.out.println(redis.lrange("list", 0, -1).size()); -// -// System.out.println(redis.blpop(10000, "list")); -// -// -// User redisUser = redis.get("user"); -// System.out.println(redisUser.getId()); -// System.out.println(redisUser.getName()); + User user = new User(); + user.setId(22); + user.setName("张三历史"); + + Jboot.setBootArg("jboot.redis.host", "127.0.0.1"); + //Jboot.setBootArg("jboot.redis.password", "123456"); + + JbootRedis redis = Jboot.me().getRedis(); + redis.set("mykey", ""); + + redis.set("user",user); + + redis.lpush("list", 1,2,3,4,5,6); + + + System.out.println(redis.get("mykey").toString()); + + System.out.println(redis.lrange("list", 0, -1).size()); + + System.out.println(redis.blpop(10000, "list")); + + + User redisUser = redis.get("user"); + System.out.println(redisUser.getId()); + System.out.println(redisUser.getName()); + + System.out.println("==SoretedSet=="); + // 清空数据 + //System.out.println(jedis.flushDB()); + redis.zadd("xiyoubang", 18, "唐僧"); + redis.zadd("xiyoubang", 500, "孙悟空"); + redis.zadd("xiyoubang", 0, "孙悟空!"); + redis.zadd("xiyoubang", 1200, "猪八戒"); + redis.zadd("xiyoubang", 1000, "沙悟净"); + redis.zadd("xiyoubang", 900, "白龙马"); + //返回有序集 key 中,指定区间内的成员。(从小到大) + Set setValues = redis.zrange("xiyoubang", 0, -1); + System.out.println(setValues); + //返回有序集 key 中,指定区间内的成员。(从大到小) + Set setValues2 = redis.zrevrange("xiyoubang", 0, -1); + System.out.println(setValues2); + // 元素个数 + System.out.println(redis.zcard("xiyoubang")); + // 元素下标 + System.out.println(redis.zscore("xiyoubang", "孙悟空")); + // 删除元素 + System.out.println(redis.zrem("xiyoubang", "孙悟空")); + //score在0-100的总数 + System.out.println(redis.zcount("xiyoubang", 0, 1000)); + //score + 50 + System.out.println(redis.zincrby("xiyoubang", 50, "猪八戒")); + //score在0-100的值 + System.out.println(redis.zrangeByScore("xiyoubang", 0, 1000)); + //score排名 + System.out.println(redis.zrank("xiyoubang", "猪八戒")); + // 整个集合值 + System.out.println(redis.zrange("xiyoubang", 0, 0)); } -- Gitee From ccd203e93e8e75c5de6cb5626471e7e1c3075a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B5=E6=96=87=E9=95=BF?= Date: Fri, 12 Jan 2018 23:10:36 +0800 Subject: [PATCH 05/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/swagger/SwaggerStarter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/swagger/SwaggerStarter.java b/src/test/java/swagger/SwaggerStarter.java index 01b743c1..b99a7506 100644 --- a/src/test/java/swagger/SwaggerStarter.java +++ b/src/test/java/swagger/SwaggerStarter.java @@ -34,7 +34,7 @@ public class SwaggerStarter { //jboot端口号配置 - Jboot.setBootArg("jboot.server.port", "8080"); + Jboot.setBootArg("jboot.server.port", "8060"); Jboot.setBootArg("jboot.swagger.path", "/swaggerui"); Jboot.setBootArg("jboot.swagger.title", "Jboot API 测试"); @@ -44,7 +44,7 @@ public class SwaggerStarter { Jboot.setBootArg("jboot.swagger.contactEmail", "fuhai999@gmail.com"); Jboot.setBootArg("jboot.swagger.contactName", "fuhai999"); Jboot.setBootArg("jboot.swagger.contactUrl", "http://jboot.io"); - Jboot.setBootArg("jboot.swagger.host", "127.0.0.1:8080"); + Jboot.setBootArg("jboot.swagger.host", "127.0.0.1:8060"); Jboot.run(args); -- Gitee From 104b417bbb2e5dc54d943dab2ad8e5852113d8c2 Mon Sep 17 00:00:00 2001 From: shaojava Date: Mon, 15 Jan 2018 21:56:46 +0800 Subject: [PATCH 06/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/jboot/config/JbootConfigConfig.java | 12 +++++++++++ .../io/jboot/config/JbootConfigManager.java | 20 +++++++++++++++---- .../config/client/ConfigRemoteReader.java | 9 +++++++-- .../java/distributedconfig/ConfigClient.java | 4 ++-- .../java/distributedconfig/ConfigServer.java | 18 +++++++++++++++-- src/test/java/distributedconfig/MyConfig.java | 2 +- 6 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/jboot/config/JbootConfigConfig.java b/src/main/java/io/jboot/config/JbootConfigConfig.java index 6801b00b..067f7d10 100644 --- a/src/main/java/io/jboot/config/JbootConfigConfig.java +++ b/src/main/java/io/jboot/config/JbootConfigConfig.java @@ -48,6 +48,10 @@ public class JbootConfigConfig { */ private String exclude; + /** + * 应用名 区分配置文件 + */ + private String appNaem; public boolean isRemoteEnable() { return remoteEnable; @@ -88,4 +92,12 @@ public class JbootConfigConfig { public void setExclude(String exclude) { this.exclude = exclude; } + + public String getAppNaem() { + return appNaem; + } + + public void setAppNaem(String appNaem) { + this.appNaem = appNaem; + } } diff --git a/src/main/java/io/jboot/config/JbootConfigManager.java b/src/main/java/io/jboot/config/JbootConfigManager.java index a319b401..281b26db 100644 --- a/src/main/java/io/jboot/config/JbootConfigManager.java +++ b/src/main/java/io/jboot/config/JbootConfigManager.java @@ -322,7 +322,7 @@ public class JbootConfigManager { private void initConfigRemoteReader() { - configRemoteReader = new ConfigRemoteReader(config.getRemoteUrl(), 5) { + configRemoteReader = new ConfigRemoteReader(config.getRemoteUrl(), config.getAppNaem(), 5) { @Override public void onChange(String key, String oldValue, String value) { /** @@ -356,6 +356,18 @@ public class JbootConfigManager { configRemoteReader.start(); } + private String getKeyName(String file){ + int index = file.indexOf('.'); + file = file.substring(0,index); + index = file.indexOf('-'); + if(index<0) + { + return "jboot"; + }else{ + file = file.substring(index+1); + return file; + } + } private void initConfigFileScanner() { configFileScanner = new ConfigFileScanner(config.getPath(), 5) { @@ -363,13 +375,13 @@ public class JbootConfigManager { public void onChange(String action, String file) { switch (action) { case ConfigFileScanner.ACTION_ADD: - propInfoMap.put(HashKit.md5(file), new PropInfoMap.PropInfo(new File(file))); + propInfoMap.put(getKeyName(file), new PropInfoMap.PropInfo(new File(file))); break; case ConfigFileScanner.ACTION_DELETE: - propInfoMap.remove(HashKit.md5(file)); + propInfoMap.remove(getKeyName(file)); break; case ConfigFileScanner.ACTION_UPDATE: - propInfoMap.put(HashKit.md5(file), new PropInfoMap.PropInfo(new File(file))); + propInfoMap.put(getKeyName(file), new PropInfoMap.PropInfo(new File(file))); break; } } diff --git a/src/main/java/io/jboot/config/client/ConfigRemoteReader.java b/src/main/java/io/jboot/config/client/ConfigRemoteReader.java index ec68cad7..b843df43 100755 --- a/src/main/java/io/jboot/config/client/ConfigRemoteReader.java +++ b/src/main/java/io/jboot/config/client/ConfigRemoteReader.java @@ -39,6 +39,7 @@ public abstract class ConfigRemoteReader { private Timer timer; private TimerTask task; private String url; + private String name; private int interval; private boolean running = false; @@ -52,8 +53,9 @@ public abstract class ConfigRemoteReader { private final JbootHttpImpl http = new JbootHttpImpl(); - public ConfigRemoteReader(String url, int interval) { + public ConfigRemoteReader(String url, String name, int interval) { this.url = url; + this.name = name; this.interval = interval; initRemoteProps(); @@ -69,7 +71,7 @@ public abstract class ConfigRemoteReader { * 初始化远程配置信息 */ private void initRemoteProps() { - String jsonString = httpGet(url); + String jsonString = httpGet(url+"/"+name); if (StringUtils.isBlank(jsonString)) { LogKit.error("can not get remote config info,plase check url : " + url); @@ -222,6 +224,9 @@ public abstract class ConfigRemoteReader { PropInfoMap.PropInfo localPropInfo = remotePropInfoMap.get(key); remotePropInfoMap.put(key, newPropInfo); + if(localPropInfo==null) + continue; + for (Object newKey : newPropInfo.getProperties().keySet()) { String localValue = localPropInfo.getString(newKey); String remoteValue = newPropInfo.getString(newKey); diff --git a/src/test/java/distributedconfig/ConfigClient.java b/src/test/java/distributedconfig/ConfigClient.java index 6a543671..279df04e 100644 --- a/src/test/java/distributedconfig/ConfigClient.java +++ b/src/test/java/distributedconfig/ConfigClient.java @@ -19,11 +19,11 @@ public class ConfigClient { //jboot端口号配置 - Jboot.setBootArg("jboot.server.port", "8088"); + //Jboot.setBootArg("jboot.server.port", "8088"); Jboot.setBootArg("jboot.config.remoteEnable", "true"); Jboot.setBootArg("jboot.config.remoteUrl", "http://127.0.0.1:8080/jboot/config"); - + Jboot.setBootArg("jboot.config.appNaem", "mos"); MyConfig config = Jboot.config(MyConfig.class); diff --git a/src/test/java/distributedconfig/ConfigServer.java b/src/test/java/distributedconfig/ConfigServer.java index 3a086aaf..a0e9d0ef 100644 --- a/src/test/java/distributedconfig/ConfigServer.java +++ b/src/test/java/distributedconfig/ConfigServer.java @@ -18,9 +18,23 @@ public class ConfigServer { public static void main(String[] args) { Jboot.setBootArg("jboot.config.serverEnable", "true"); - Jboot.setBootArg("jboot.config.path", "/Users/michael/Desktop/test"); - + //Jboot.setBootArg("jboot.config.path", "/Users/michael/Desktop/test"); + Jboot.setBootArg("jboot.config.path", "C://config"); Jboot.run(args); + //System.out.println(getKeyName("jboot-dmos.properties")); + //System.out.println(getKeyName("jboot.properties")); + } + + private static String getKeyName(String file){ + int index = file.indexOf('.'); + file = file.substring(0,index); + index = file.indexOf('-'); + if(index<0) + { + return "jboot"; + }else{ + return file.substring(index+1); + } } } diff --git a/src/test/java/distributedconfig/MyConfig.java b/src/test/java/distributedconfig/MyConfig.java index 5a6e70e3..f529cae0 100644 --- a/src/test/java/distributedconfig/MyConfig.java +++ b/src/test/java/distributedconfig/MyConfig.java @@ -7,7 +7,7 @@ import io.jboot.config.annotation.PropertyConfig; * @version V1.0 * @Package distributedconfig */ -@PropertyConfig(prefix = "my") +@PropertyConfig(prefix = "jboot.mos") public class MyConfig { private String name = "defalutName"; -- Gitee From 60e1bf1ffa76b3ebc4ce82a0c346488705ca7f68 Mon Sep 17 00:00:00 2001 From: shaojava Date: Mon, 15 Jan 2018 22:02:18 +0800 Subject: [PATCH 07/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/jboot/config/JbootConfigConfig.java | 17 +++++++++-------- .../io/jboot/config/JbootConfigManager.java | 2 +- .../java/distributedconfig/ConfigClient.java | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/jboot/config/JbootConfigConfig.java b/src/main/java/io/jboot/config/JbootConfigConfig.java index 067f7d10..2351a66e 100644 --- a/src/main/java/io/jboot/config/JbootConfigConfig.java +++ b/src/main/java/io/jboot/config/JbootConfigConfig.java @@ -51,7 +51,15 @@ public class JbootConfigConfig { /** * 应用名 区分配置文件 */ - private String appNaem; + private String appName; + + public String getAppName() { + return appName; + } + + public void setAppName(String appName) { + this.appName = appName; + } public boolean isRemoteEnable() { return remoteEnable; @@ -93,11 +101,4 @@ public class JbootConfigConfig { this.exclude = exclude; } - public String getAppNaem() { - return appNaem; - } - - public void setAppNaem(String appNaem) { - this.appNaem = appNaem; - } } diff --git a/src/main/java/io/jboot/config/JbootConfigManager.java b/src/main/java/io/jboot/config/JbootConfigManager.java index 281b26db..5a3690a1 100644 --- a/src/main/java/io/jboot/config/JbootConfigManager.java +++ b/src/main/java/io/jboot/config/JbootConfigManager.java @@ -322,7 +322,7 @@ public class JbootConfigManager { private void initConfigRemoteReader() { - configRemoteReader = new ConfigRemoteReader(config.getRemoteUrl(), config.getAppNaem(), 5) { + configRemoteReader = new ConfigRemoteReader(config.getRemoteUrl(), config.getAppName(), 5) { @Override public void onChange(String key, String oldValue, String value) { /** diff --git a/src/test/java/distributedconfig/ConfigClient.java b/src/test/java/distributedconfig/ConfigClient.java index 279df04e..c7393e62 100644 --- a/src/test/java/distributedconfig/ConfigClient.java +++ b/src/test/java/distributedconfig/ConfigClient.java @@ -23,7 +23,7 @@ public class ConfigClient { Jboot.setBootArg("jboot.config.remoteEnable", "true"); Jboot.setBootArg("jboot.config.remoteUrl", "http://127.0.0.1:8080/jboot/config"); - Jboot.setBootArg("jboot.config.appNaem", "mos"); + Jboot.setBootArg("jboot.config.appName", "mos"); MyConfig config = Jboot.config(MyConfig.class); -- Gitee From a3716f1a8053913861e53f9f322a0d9f61522000 Mon Sep 17 00:00:00 2001 From: shaojava Date: Mon, 15 Jan 2018 23:04:25 +0800 Subject: [PATCH 08/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/io/jboot/config/JbootConfigConfig.java | 2 +- src/test/java/distributedconfig/ConfigClient.java | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 2169b3fb..4ac62c6d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.jboot jboot - 1.2.8 + 1.2.9 jar jboot diff --git a/src/main/java/io/jboot/config/JbootConfigConfig.java b/src/main/java/io/jboot/config/JbootConfigConfig.java index 2351a66e..b8b7c139 100644 --- a/src/main/java/io/jboot/config/JbootConfigConfig.java +++ b/src/main/java/io/jboot/config/JbootConfigConfig.java @@ -51,7 +51,7 @@ public class JbootConfigConfig { /** * 应用名 区分配置文件 */ - private String appName; + private String appName=""; public String getAppName() { return appName; diff --git a/src/test/java/distributedconfig/ConfigClient.java b/src/test/java/distributedconfig/ConfigClient.java index c7393e62..2a877191 100644 --- a/src/test/java/distributedconfig/ConfigClient.java +++ b/src/test/java/distributedconfig/ConfigClient.java @@ -27,14 +27,15 @@ public class ConfigClient { MyConfig config = Jboot.config(MyConfig.class); - for (int i = 0; i < 1000000; i++) { + /*for (int i = 0; i < 1000000; i++) { System.out.println("------myname:" + config.getName()); try { Thread.sleep(1000 * 2); } catch (InterruptedException e) { e.printStackTrace(); } - } + }*/ + Jboot.run(args); } -- Gitee From 570291eeabd5e1d8d2e012250d292afd38518403 Mon Sep 17 00:00:00 2001 From: shaowenchang Date: Mon, 15 Jan 2018 23:56:19 +0800 Subject: [PATCH 09/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/HttpTest.java | 17 ++++++++++------- src/test/java/RedisTest.java | 8 +++++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/test/java/HttpTest.java b/src/test/java/HttpTest.java index a84c2bb9..80c3cbec 100644 --- a/src/test/java/HttpTest.java +++ b/src/test/java/HttpTest.java @@ -3,6 +3,9 @@ import io.jboot.core.http.JbootHttpRequest; import io.jboot.core.http.JbootHttpResponse; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + public class HttpTest { @@ -31,13 +34,13 @@ public class HttpTest { @Test public void testHttpPost() { -// Map params = new HashMap<>(); -// params.put("key1", "value1"); -// params.put("key2", "value2"); -// -// -// String html = Jboot.httpPost("http://www.oschina.net/", params); -// System.out.println(html); + Map params = new HashMap<>(); + params.put("key1", "value1"); + params.put("key2", "value2"); + + + String html = Jboot.httpPost("http://www.oschina.net/", params); + System.out.println(html); } @Test diff --git a/src/test/java/RedisTest.java b/src/test/java/RedisTest.java index a70df487..8fdce687 100644 --- a/src/test/java/RedisTest.java +++ b/src/test/java/RedisTest.java @@ -15,7 +15,7 @@ public class RedisTest { user.setId(22); user.setName("张三历史"); - Jboot.setBootArg("jboot.redis.host", "127.0.0.1"); + Jboot.setBootArg("jboot.redis.host", "192.168.19.1"); //Jboot.setBootArg("jboot.redis.password", "123456"); JbootRedis redis = Jboot.me().getRedis(); @@ -70,6 +70,12 @@ public class RedisTest { // 整个集合值 System.out.println(redis.zrange("xiyoubang", 0, 0)); + long score = System.currentTimeMillis()/1000; + int interval = 5*60; + //setValues = redis.zrange("CKP",score-interval,score); + setValues = redis.zrangeByScore("CKP",0,score-interval); + System.out.println(setValues.size()); + System.out.println(setValues); } -- Gitee From fb2fabaca44bca0b6a621be1cada5a36477da200 Mon Sep 17 00:00:00 2001 From: shaojava Date: Tue, 16 Jan 2018 00:14:41 +0800 Subject: [PATCH 10/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/distributedconfig/ConfigClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/distributedconfig/ConfigClient.java b/src/test/java/distributedconfig/ConfigClient.java index 2a877191..544bff1f 100644 --- a/src/test/java/distributedconfig/ConfigClient.java +++ b/src/test/java/distributedconfig/ConfigClient.java @@ -23,7 +23,7 @@ public class ConfigClient { Jboot.setBootArg("jboot.config.remoteEnable", "true"); Jboot.setBootArg("jboot.config.remoteUrl", "http://127.0.0.1:8080/jboot/config"); - Jboot.setBootArg("jboot.config.appName", "mos"); + //Jboot.setBootArg("jboot.config.appName", "mos"); MyConfig config = Jboot.config(MyConfig.class); -- Gitee From bf6b7d398c9d93308303d2e7dce9a4b12bf1841f Mon Sep 17 00:00:00 2001 From: shaojava Date: Tue, 16 Jan 2018 22:16:02 +0800 Subject: [PATCH 11/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8A=A0=E5=85=A5Sprin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/jboot/web/SpringBeanFactory.java | 22 +++++ src/test/java/spring/AppProviderConfig.java | 94 +++++++++++++++++++ src/test/java/spring/CodeConfig.java | 36 +++++++ src/test/java/spring/ControllerTest.java | 78 +++++++++++++++ src/test/resources/applicationContext.xml | 6 ++ 5 files changed, 236 insertions(+) create mode 100644 src/main/java/io/jboot/web/SpringBeanFactory.java create mode 100644 src/test/java/spring/AppProviderConfig.java create mode 100644 src/test/java/spring/CodeConfig.java create mode 100644 src/test/java/spring/ControllerTest.java create mode 100644 src/test/resources/applicationContext.xml diff --git a/src/main/java/io/jboot/web/SpringBeanFactory.java b/src/main/java/io/jboot/web/SpringBeanFactory.java new file mode 100644 index 00000000..d8d3ab1d --- /dev/null +++ b/src/main/java/io/jboot/web/SpringBeanFactory.java @@ -0,0 +1,22 @@ +package io.jboot.web; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +public class SpringBeanFactory implements ApplicationContextAware { + private static ApplicationContext context = null; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + context = applicationContext; + } + + public static Object getBean(String name){ + return context.getBean(name); + } + + public static Object getBean(Class name){ + return context.getBean(name); + } +} \ No newline at end of file diff --git a/src/test/java/spring/AppProviderConfig.java b/src/test/java/spring/AppProviderConfig.java new file mode 100644 index 00000000..6a0b5e7f --- /dev/null +++ b/src/test/java/spring/AppProviderConfig.java @@ -0,0 +1,94 @@ +package com.lastb7.dubbo.app; + +import com.jfinal.config.*; +import com.jfinal.core.JFinal; +import com.jfinal.kit.PropKit; +import com.jfinal.plugin.activerecord.ActiveRecordPlugin; +import com.jfinal.plugin.druid.DruidPlugin; +import com.jfinal.plugin.druid.DruidStatViewHandler; +import com.jfinal.template.Engine; +import com.lastb7.dubbo.common.model._MappingKit; +import com.lastb7.dubbo.plugin.spring.SpringPlugin; + +/** + * 本 demo 仅表达最为粗浅的 jfinal 用法,更为有价值的实用的企业级用法 + * 详见 JFinal 俱乐部: http://jfinal.com/club + *

+ * API引导式配置 + */ +public class AppProviderConfig extends JFinalConfig { + + /** + * 运行此 main 方法可以启动项目,此main方法可以放置在任意的Class类定义中,不一定要放于此 + *

+ * 使用本方法启动过第一次以后,会在开发工具的 debug、run config 中自动生成 + * 一条启动配置,可对该自动生成的配置再添加额外的配置项,例如 VM argument 可配置为: + * -XX:PermSize=64M -XX:MaxPermSize=256M + */ + public static void main(String[] args) { + + /** + * 特别注意:IDEA 之下建议的启动方式,仅比 eclipse 之下少了最后一个参数 + */ + JFinal.start("JFinalDubboDemoProvider/src/main/webapp", 8082, "/"); + } + + /** + * 配置常量 + */ + public void configConstant(Constants me) { + // 加载少量必要配置,随后可用PropKit.get(...)获取值 + PropKit.use("a_little_config.txt"); + me.setDevMode(PropKit.getBoolean("devMode", false)); + } + + /** + * 配置路由 + */ + public void configRoute(Routes me) { + } + + public void configEngine(Engine me) { + + } + + /** + * 配置插件 + */ + public void configPlugin(Plugins me) { + // 启动Spring插件 + me.add(new SpringPlugin("classpath:applicationContext.xml")); + + // 配置C3p0数据库连接池插件 + DruidPlugin druidPlugin = new DruidPlugin( + PropKit.get("jdbcUrl"), + PropKit.get("user"), + PropKit.get("password").trim(), + PropKit.get("driverClassName")); + me.add(druidPlugin); + + // 配置ActiveRecord插件 + ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin); + // 所有映射在 MappingKit 中自动化搞定 + _MappingKit.mapping(arp); + me.add(arp); + } + + public static DruidPlugin createDruidPlugin() { + return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim()); + } + + /** + * 配置全局拦截器 + */ + public void configInterceptor(Interceptors me) { + + } + + /** + * 配置处理器 + */ + public void configHandler(Handlers me) { + me.add(new DruidStatViewHandler("/druid")); + } +} diff --git a/src/test/java/spring/CodeConfig.java b/src/test/java/spring/CodeConfig.java new file mode 100644 index 00000000..9e8fbb71 --- /dev/null +++ b/src/test/java/spring/CodeConfig.java @@ -0,0 +1,36 @@ +package com.example.disconf.demo.config; + +import org.springframework.stereotype.Service; + +import com.baidu.disconf.client.common.annotations.DisconfFile; +import com.baidu.disconf.client.common.annotations.DisconfFileItem; + +/** + * Created by knightliao on 15/1/7. + */ +@Service +@DisconfFile(filename = "code.properties", targetDirPath = "disconf") +public class CodeConfig { + + private String codeError = ""; + + private boolean routerRateLimitEnable; + + @DisconfFileItem(name = "syserror.paramtype", associateField = "codeError") + public String getCodeError() { + return codeError; + } + + @DisconfFileItem(name = "proxy.routerRateEnable", associateField = "routerRateLimitEnable") + public boolean isRouterRateLimitEnable() { + return routerRateLimitEnable; + } + + public void setRouterRateLimitEnable(boolean routerRateLimitEnable) { + this.routerRateLimitEnable = routerRateLimitEnable; + } + + public void setCodeError(String codeError) { + this.codeError = codeError; + } +} diff --git a/src/test/java/spring/ControllerTest.java b/src/test/java/spring/ControllerTest.java new file mode 100644 index 00000000..22557a7b --- /dev/null +++ b/src/test/java/spring/ControllerTest.java @@ -0,0 +1,78 @@ +import com.jfinal.core.Controller; +import io.jboot.Jboot; +import io.jboot.aop.annotation.Bean; +import io.jboot.core.cache.annotation.Cacheable; +import io.jboot.web.controller.annotation.RequestMapping; + +import javax.inject.Inject; +import javax.inject.Singleton; + +@RequestMapping("/test") +public class ControllerTest extends Controller { + + + public static void main(String[] args) { + +// Jboot.setBootArg("jboot.hystrix.url", "/hystrix.stream"); +// Jboot.setBootArg("jboot.cache.type", "redis"); +// Jboot.setBootArg("jboot.metrics.url", "/metrics.abc"); +// Jboot.setBootArg("jboot.cache.redis.host", "127.0.0.1"); + + Jboot.run(args); + + + } + + + public void json() { + setAttr("aaa", "bbb"); + renderJson(); + } + + + @Inject + ServiceInter serviceTest; + + public void index() { + + renderText("hello" + serviceTest.hello("")); + + } + + + public void directive() { + render("/test.html"); + } + + public void directive1() { + render("/test1.html"); + } + + + @Singleton + @Bean + public static class ServiceTest implements ServiceInter { + + + @Override + @Cacheable(name = "aaa", key = "#(\"key:\" + aaa)") + public String hello(String aaa) { + System.out.println("hello invoked"); + return "hello " + aaa; + } + + @Override + public String test1() { + return null; + } + } + + + public static interface ServiceInter { + public String hello(String aaa); + + public String test1(); + } + + +} diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml new file mode 100644 index 00000000..142def25 --- /dev/null +++ b/src/test/resources/applicationContext.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file -- Gitee From 7898d287b331fbb637bda63bbd4fdc11e8159d68 Mon Sep 17 00:00:00 2001 From: shaojava Date: Tue, 16 Jan 2018 22:16:40 +0800 Subject: [PATCH 12/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8A=A0=E5=85=A5Sprin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 19 +++++ .../io/jboot/web/JbootControllerManager.java | 57 ++++++++++++- src/test/java/ControllerTest.java | 2 - src/test/java/spring/AppProviderConfig.java | 82 ++----------------- src/test/java/spring/CodeConfig.java | 22 +---- src/test/java/spring/ControllerTest.java | 69 ++++------------ src/test/java/spring/Inject.java | 51 ++++++++++++ src/test/java/spring/IocInterceptor.java | 59 +++++++++++++ src/test/java/spring/SpringPlugin.java | 59 +++++++++++++ src/test/resources/applicationContext.xml | 17 +++- 10 files changed, 286 insertions(+), 151 deletions(-) create mode 100644 src/test/java/spring/Inject.java create mode 100644 src/test/java/spring/IocInterceptor.java create mode 100644 src/test/java/spring/SpringPlugin.java diff --git a/pom.xml b/pom.xml index 5c9fd684..2286a93f 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,25 @@ + + + org.springframework + spring-aop + 4.2.4.RELEASE + + + + org.springframework + spring-aspects + 4.2.4.RELEASE + + + + + org.springframework + spring-context + 4.2.4.RELEASE + com.jfinal diff --git a/src/main/java/io/jboot/web/JbootControllerManager.java b/src/main/java/io/jboot/web/JbootControllerManager.java index f6033d0a..70030061 100644 --- a/src/main/java/io/jboot/web/JbootControllerManager.java +++ b/src/main/java/io/jboot/web/JbootControllerManager.java @@ -20,7 +20,10 @@ import com.google.common.collect.HashBiMap; import com.jfinal.core.Controller; import com.jfinal.core.ControllerFactory; import io.jboot.Jboot; +import org.springframework.beans.factory.annotation.Autowired; +import javax.annotation.Resource; +import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; @@ -46,17 +49,27 @@ public class JbootControllerManager extends ControllerFactory { } }; + /*public Controller getController(Class controllerClass) throws InstantiationException, IllegalAccessException { + Controller ret = buffers.get().get(controllerClass); + if (ret == null) { + ret = controllerClass.newInstance(); + Jboot.injectMembers(ret); + buffers.get().put(controllerClass, ret); + } + return ret; + }*/ + public Controller getController(Class controllerClass) throws InstantiationException, IllegalAccessException { Controller ret = buffers.get().get(controllerClass); if (ret == null) { ret = controllerClass.newInstance(); Jboot.injectMembers(ret); + autoInject(ret); buffers.get().put(controllerClass, ret); } return ret; } - private BiMap> controllerMapping = HashBiMap.create(); public Class getControllerByPath(String path) { @@ -71,5 +84,47 @@ public class JbootControllerManager extends ControllerFactory { controllerMapping.put(path, controllerClass); } + /** + * 自动注入 + * @param ret controller + */ + public void autoInject(Controller ret) { + Field[] fields = ret.getClass().getDeclaredFields(); + for (Field field : fields) { + Object bean = null; + if (field.isAnnotationPresent(Autowired.class)) { + try { + bean = SpringBeanFactory.getBean(field.getName()); + } catch (Exception e) { + } + if (null == bean) { + try { + bean = SpringBeanFactory.getBean(field.getType()); + } catch (Exception e) { + } + } + if (null == bean) { + Autowired annotation = field.getAnnotation(Autowired.class); + if (annotation.required()) { + throw new RuntimeException(String.format( + "Error creating bean with name '%s': Injection of autowired dependencies failed.", + field.getName())); + } + } + } else if (field.isAnnotationPresent(Resource.class)) { + Resource annotation = field.getAnnotation(Resource.class); + bean = SpringBeanFactory.getBean(annotation.name().trim().length() == 0 ? field.getName() : annotation.name()); + } else + continue; + try { + if (bean != null) { + field.setAccessible(true); + field.set(ret, bean); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } } \ No newline at end of file diff --git a/src/test/java/ControllerTest.java b/src/test/java/ControllerTest.java index 607f41c6..22557a7b 100644 --- a/src/test/java/ControllerTest.java +++ b/src/test/java/ControllerTest.java @@ -7,7 +7,6 @@ import io.jboot.web.controller.annotation.RequestMapping; import javax.inject.Inject; import javax.inject.Singleton; - @RequestMapping("/test") public class ControllerTest extends Controller { @@ -19,7 +18,6 @@ public class ControllerTest extends Controller { // Jboot.setBootArg("jboot.metrics.url", "/metrics.abc"); // Jboot.setBootArg("jboot.cache.redis.host", "127.0.0.1"); - Jboot.run(args); diff --git a/src/test/java/spring/AppProviderConfig.java b/src/test/java/spring/AppProviderConfig.java index 6a0b5e7f..352a106c 100644 --- a/src/test/java/spring/AppProviderConfig.java +++ b/src/test/java/spring/AppProviderConfig.java @@ -1,4 +1,4 @@ -package com.lastb7.dubbo.app; +package spring; import com.jfinal.config.*; import com.jfinal.core.JFinal; @@ -7,8 +7,7 @@ import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.druid.DruidPlugin; import com.jfinal.plugin.druid.DruidStatViewHandler; import com.jfinal.template.Engine; -import com.lastb7.dubbo.common.model._MappingKit; -import com.lastb7.dubbo.plugin.spring.SpringPlugin; +import io.jboot.web.JbootAppConfig; /** * 本 demo 仅表达最为粗浅的 jfinal 用法,更为有价值的实用的企业级用法 @@ -16,79 +15,16 @@ import com.lastb7.dubbo.plugin.spring.SpringPlugin; *

* API引导式配置 */ -public class AppProviderConfig extends JFinalConfig { - - /** - * 运行此 main 方法可以启动项目,此main方法可以放置在任意的Class类定义中,不一定要放于此 - *

- * 使用本方法启动过第一次以后,会在开发工具的 debug、run config 中自动生成 - * 一条启动配置,可对该自动生成的配置再添加额外的配置项,例如 VM argument 可配置为: - * -XX:PermSize=64M -XX:MaxPermSize=256M - */ - public static void main(String[] args) { - - /** - * 特别注意:IDEA 之下建议的启动方式,仅比 eclipse 之下少了最后一个参数 - */ - JFinal.start("JFinalDubboDemoProvider/src/main/webapp", 8082, "/"); - } - - /** - * 配置常量 - */ - public void configConstant(Constants me) { - // 加载少量必要配置,随后可用PropKit.get(...)获取值 - PropKit.use("a_little_config.txt"); - me.setDevMode(PropKit.getBoolean("devMode", false)); - } - - /** - * 配置路由 - */ - public void configRoute(Routes me) { - } - - public void configEngine(Engine me) { - - } - +public class AppProviderConfig extends JbootAppConfig { + /** * 配置插件 */ + @Override public void configPlugin(Plugins me) { + super.configPlugin(me); // 启动Spring插件 - me.add(new SpringPlugin("classpath:applicationContext.xml")); - - // 配置C3p0数据库连接池插件 - DruidPlugin druidPlugin = new DruidPlugin( - PropKit.get("jdbcUrl"), - PropKit.get("user"), - PropKit.get("password").trim(), - PropKit.get("driverClassName")); - me.add(druidPlugin); - - // 配置ActiveRecord插件 - ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin); - // 所有映射在 MappingKit 中自动化搞定 - _MappingKit.mapping(arp); - me.add(arp); - } - - public static DruidPlugin createDruidPlugin() { - return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim()); - } - - /** - * 配置全局拦截器 - */ - public void configInterceptor(Interceptors me) { - + me.add(new SpringPlugin()); } - - /** - * 配置处理器 - */ - public void configHandler(Handlers me) { - me.add(new DruidStatViewHandler("/druid")); - } -} + +} \ No newline at end of file diff --git a/src/test/java/spring/CodeConfig.java b/src/test/java/spring/CodeConfig.java index 9e8fbb71..5e08f28b 100644 --- a/src/test/java/spring/CodeConfig.java +++ b/src/test/java/spring/CodeConfig.java @@ -1,36 +1,20 @@ -package com.example.disconf.demo.config; +package spring; import org.springframework.stereotype.Service; -import com.baidu.disconf.client.common.annotations.DisconfFile; -import com.baidu.disconf.client.common.annotations.DisconfFileItem; - /** * Created by knightliao on 15/1/7. */ @Service -@DisconfFile(filename = "code.properties", targetDirPath = "disconf") public class CodeConfig { - private String codeError = ""; - - private boolean routerRateLimitEnable; + private String codeError = "tsetset"; - @DisconfFileItem(name = "syserror.paramtype", associateField = "codeError") public String getCodeError() { return codeError; } - @DisconfFileItem(name = "proxy.routerRateEnable", associateField = "routerRateLimitEnable") - public boolean isRouterRateLimitEnable() { - return routerRateLimitEnable; - } - - public void setRouterRateLimitEnable(boolean routerRateLimitEnable) { - this.routerRateLimitEnable = routerRateLimitEnable; - } - public void setCodeError(String codeError) { this.codeError = codeError; } -} +} \ No newline at end of file diff --git a/src/test/java/spring/ControllerTest.java b/src/test/java/spring/ControllerTest.java index 22557a7b..5c0438a1 100644 --- a/src/test/java/spring/ControllerTest.java +++ b/src/test/java/spring/ControllerTest.java @@ -1,15 +1,22 @@ +package spring; + import com.jfinal.core.Controller; import io.jboot.Jboot; import io.jboot.aop.annotation.Bean; import io.jboot.core.cache.annotation.Cacheable; +import io.jboot.web.controller.JbootController; import io.jboot.web.controller.annotation.RequestMapping; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.inject.Inject; import javax.inject.Singleton; -@RequestMapping("/test") -public class ControllerTest extends Controller { +@RequestMapping("/spring") +public class ControllerTest extends JbootController { + @Autowired + private CodeConfig codeConfig; public static void main(String[] args) { @@ -18,61 +25,15 @@ public class ControllerTest extends Controller { // Jboot.setBootArg("jboot.metrics.url", "/metrics.abc"); // Jboot.setBootArg("jboot.cache.redis.host", "127.0.0.1"); - Jboot.run(args); - - - } - - - public void json() { - setAttr("aaa", "bbb"); - renderJson(); - } - - - @Inject - ServiceInter serviceTest; - - public void index() { - - renderText("hello" + serviceTest.hello("")); - - } + String name = AppProviderConfig.class.getName(); + Jboot.setBootArg("jboot.jfinalConfig",name); + Jboot.run(args); - public void directive() { - render("/test.html"); - } - - public void directive1() { - render("/test1.html"); - } - - - @Singleton - @Bean - public static class ServiceTest implements ServiceInter { - - - @Override - @Cacheable(name = "aaa", key = "#(\"key:\" + aaa)") - public String hello(String aaa) { - System.out.println("hello invoked"); - return "hello " + aaa; - } - - @Override - public String test1() { - return null; - } } - - public static interface ServiceInter { - public String hello(String aaa); - - public String test1(); + public void index(){ + String codeError = codeConfig.getCodeError(); + renderText("hello world."+codeError); } - - } diff --git a/src/test/java/spring/Inject.java b/src/test/java/spring/Inject.java new file mode 100644 index 00000000..98e23ecf --- /dev/null +++ b/src/test/java/spring/Inject.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package spring; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Inject. + */ +public class Inject { + + private Inject() {} + + @Inherited + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.FIELD}) + public static @interface BY_TYPE {} + + @Inherited + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.FIELD}) + public static @interface BY_NAME {} + + /* + @Inherited + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.FIELD}) + public static @interface IGNORE {} + */ +} + + + diff --git a/src/test/java/spring/IocInterceptor.java b/src/test/java/spring/IocInterceptor.java new file mode 100644 index 00000000..9c9f0113 --- /dev/null +++ b/src/test/java/spring/IocInterceptor.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package spring; + +import com.jfinal.aop.Interceptor; +import com.jfinal.aop.Invocation; +import org.springframework.context.ApplicationContext; + +import java.lang.reflect.Field; + +/** + * jfinal2.0后移除了SpringPlugin,修改代码支持SpringPlugin + * IocInterceptor. + */ +public class IocInterceptor implements Interceptor { + + static ApplicationContext ctx; + + // ActionInvocation 改为2.0的Invocation + public void intercept(Invocation ai) { + // 修改了这行 获取fields + //重点也在这 追随2.0的AOP 不限于Controller 也可以在Service层注入 + Field[] fields = ai.getMethod().getDeclaringClass().getDeclaredFields(); + for (Field field : fields) { + Object bean; + if (field.isAnnotationPresent(Inject.BY_NAME.class)) + bean = ctx.getBean(field.getName()); + else if (field.isAnnotationPresent(Inject.BY_TYPE.class)) + bean = ctx.getBean(field.getType()); + else + continue; + + try { + if (bean != null) { + field.setAccessible(true); + field.set(ai.getTarget(), bean); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + ai.invoke(); + } +} diff --git a/src/test/java/spring/SpringPlugin.java b/src/test/java/spring/SpringPlugin.java new file mode 100644 index 00000000..2fb69e80 --- /dev/null +++ b/src/test/java/spring/SpringPlugin.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package spring; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.FileSystemXmlApplicationContext; +import com.jfinal.kit.PathKit; +import com.jfinal.plugin.IPlugin; + +/** + * SpringPlugin. + */ +public class SpringPlugin implements IPlugin { + + private String[] configurations; + private ApplicationContext ctx; + + /** + * Use configuration under the path of WebRoot/WEB-INF. + */ + public SpringPlugin() { + } + + public SpringPlugin(String... configurations) { + this.configurations = configurations; + } + + public SpringPlugin(ApplicationContext ctx) { + this.ctx = ctx; + } + + public boolean start() { + if (ctx != null) + IocInterceptor.ctx = ctx; + else if (configurations != null) + IocInterceptor.ctx = new FileSystemXmlApplicationContext(configurations); + else + IocInterceptor.ctx = new FileSystemXmlApplicationContext(PathKit.getWebRootPath() + "/applicationContext.xml"); + return true; + } + + public boolean stop() { + return true; + } +} diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml index 142def25..a1e377bd 100644 --- a/src/test/resources/applicationContext.xml +++ b/src/test/resources/applicationContext.xml @@ -1,6 +1,19 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.1.xsd + http://www.springframework.org/schema/aop + http://www.springframework.org/schema/aop/spring-aop-3.0.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd"> + + + + + + + \ No newline at end of file -- Gitee From ee7ec32304bef235b4e4d393c95a4a2909c97b73 Mon Sep 17 00:00:00 2001 From: shaojava Date: Tue, 16 Jan 2018 22:56:55 +0800 Subject: [PATCH 13/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/jboot/aop/jfinal}/SpringPlugin.java | 24 ++++---- src/test/java/spring/Inject.java | 51 ---------------- src/test/java/spring/IocInterceptor.java | 59 ------------------- 3 files changed, 13 insertions(+), 121 deletions(-) rename src/{test/java/spring => main/java/io/jboot/aop/jfinal}/SpringPlugin.java (76%) delete mode 100644 src/test/java/spring/Inject.java delete mode 100644 src/test/java/spring/IocInterceptor.java diff --git a/src/test/java/spring/SpringPlugin.java b/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java similarity index 76% rename from src/test/java/spring/SpringPlugin.java rename to src/main/java/io/jboot/aop/jfinal/SpringPlugin.java index 2fb69e80..6456c31b 100644 --- a/src/test/java/spring/SpringPlugin.java +++ b/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java @@ -27,7 +27,7 @@ import com.jfinal.plugin.IPlugin; public class SpringPlugin implements IPlugin { private String[] configurations; - private ApplicationContext ctx; + private static ApplicationContext ctx; /** * Use configuration under the path of WebRoot/WEB-INF. @@ -38,22 +38,24 @@ public class SpringPlugin implements IPlugin { public SpringPlugin(String... configurations) { this.configurations = configurations; } - - public SpringPlugin(ApplicationContext ctx) { - this.ctx = ctx; - } - + public boolean start() { - if (ctx != null) - IocInterceptor.ctx = ctx; - else if (configurations != null) - IocInterceptor.ctx = new FileSystemXmlApplicationContext(configurations); + if (configurations != null) + ctx = new FileSystemXmlApplicationContext(configurations); else - IocInterceptor.ctx = new FileSystemXmlApplicationContext(PathKit.getWebRootPath() + "/applicationContext.xml"); + ctx = new FileSystemXmlApplicationContext(PathKit.getWebRootPath() + "/applicationContext.xml"); return true; } public boolean stop() { return true; } + + public static Object getBean(String name){ + return ctx.getBean(name); + } + + public static Object getBean(Class name){ + return ctx.getBean(name); + } } diff --git a/src/test/java/spring/Inject.java b/src/test/java/spring/Inject.java deleted file mode 100644 index 98e23ecf..00000000 --- a/src/test/java/spring/Inject.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package spring; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Inject. - */ -public class Inject { - - private Inject() {} - - @Inherited - @Retention(RetentionPolicy.RUNTIME) - @Target({ElementType.FIELD}) - public static @interface BY_TYPE {} - - @Inherited - @Retention(RetentionPolicy.RUNTIME) - @Target({ElementType.FIELD}) - public static @interface BY_NAME {} - - /* - @Inherited - @Retention(RetentionPolicy.RUNTIME) - @Target({ElementType.FIELD}) - public static @interface IGNORE {} - */ -} - - - diff --git a/src/test/java/spring/IocInterceptor.java b/src/test/java/spring/IocInterceptor.java deleted file mode 100644 index 9c9f0113..00000000 --- a/src/test/java/spring/IocInterceptor.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package spring; - -import com.jfinal.aop.Interceptor; -import com.jfinal.aop.Invocation; -import org.springframework.context.ApplicationContext; - -import java.lang.reflect.Field; - -/** - * jfinal2.0后移除了SpringPlugin,修改代码支持SpringPlugin - * IocInterceptor. - */ -public class IocInterceptor implements Interceptor { - - static ApplicationContext ctx; - - // ActionInvocation 改为2.0的Invocation - public void intercept(Invocation ai) { - // 修改了这行 获取fields - //重点也在这 追随2.0的AOP 不限于Controller 也可以在Service层注入 - Field[] fields = ai.getMethod().getDeclaringClass().getDeclaredFields(); - for (Field field : fields) { - Object bean; - if (field.isAnnotationPresent(Inject.BY_NAME.class)) - bean = ctx.getBean(field.getName()); - else if (field.isAnnotationPresent(Inject.BY_TYPE.class)) - bean = ctx.getBean(field.getType()); - else - continue; - - try { - if (bean != null) { - field.setAccessible(true); - field.set(ai.getTarget(), bean); - } - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - ai.invoke(); - } -} -- Gitee From 6dbdec960b8be901b8ad17ca096839966c67c216 Mon Sep 17 00:00:00 2001 From: shaojava Date: Tue, 16 Jan 2018 22:57:10 +0800 Subject: [PATCH 14/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/jboot/aop/jfinal/SpringPlugin.java | 16 +++++++++----- .../io/jboot/web/JbootControllerManager.java | 7 +++--- .../java/io/jboot/web/SpringBeanFactory.java | 22 ------------------- src/test/java/spring/AppProviderConfig.java | 1 + src/test/java/spring/CodeConfig.java | 2 +- src/test/resources/applicationContext.xml | 2 +- 6 files changed, 17 insertions(+), 33 deletions(-) delete mode 100644 src/main/java/io/jboot/web/SpringBeanFactory.java diff --git a/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java b/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java index 6456c31b..4c98d022 100644 --- a/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java +++ b/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package spring; +package io.jboot.aop.jfinal; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.FileSystemXmlApplicationContext; import com.jfinal.kit.PathKit; import com.jfinal.plugin.IPlugin; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.FileSystemXmlApplicationContext; /** * SpringPlugin. @@ -27,7 +27,7 @@ import com.jfinal.plugin.IPlugin; public class SpringPlugin implements IPlugin { private String[] configurations; - private static ApplicationContext ctx; + private static ApplicationContext ctx=null; /** * Use configuration under the path of WebRoot/WEB-INF. @@ -52,10 +52,14 @@ public class SpringPlugin implements IPlugin { } public static Object getBean(String name){ - return ctx.getBean(name); + if(ctx!=null) + return ctx.getBean(name); + return null; } public static Object getBean(Class name){ - return ctx.getBean(name); + if(ctx!=null) + return ctx.getBean(name); + return null; } } diff --git a/src/main/java/io/jboot/web/JbootControllerManager.java b/src/main/java/io/jboot/web/JbootControllerManager.java index 70030061..64d88233 100644 --- a/src/main/java/io/jboot/web/JbootControllerManager.java +++ b/src/main/java/io/jboot/web/JbootControllerManager.java @@ -20,6 +20,7 @@ import com.google.common.collect.HashBiMap; import com.jfinal.core.Controller; import com.jfinal.core.ControllerFactory; import io.jboot.Jboot; +import io.jboot.aop.jfinal.SpringPlugin; import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Resource; @@ -94,12 +95,12 @@ public class JbootControllerManager extends ControllerFactory { Object bean = null; if (field.isAnnotationPresent(Autowired.class)) { try { - bean = SpringBeanFactory.getBean(field.getName()); + bean = SpringPlugin.getBean(field.getName()); } catch (Exception e) { } if (null == bean) { try { - bean = SpringBeanFactory.getBean(field.getType()); + bean = SpringPlugin.getBean(field.getType()); } catch (Exception e) { } } @@ -113,7 +114,7 @@ public class JbootControllerManager extends ControllerFactory { } } else if (field.isAnnotationPresent(Resource.class)) { Resource annotation = field.getAnnotation(Resource.class); - bean = SpringBeanFactory.getBean(annotation.name().trim().length() == 0 ? field.getName() : annotation.name()); + bean = SpringPlugin.getBean(annotation.name().trim().length() == 0 ? field.getName() : annotation.name()); } else continue; try { diff --git a/src/main/java/io/jboot/web/SpringBeanFactory.java b/src/main/java/io/jboot/web/SpringBeanFactory.java deleted file mode 100644 index d8d3ab1d..00000000 --- a/src/main/java/io/jboot/web/SpringBeanFactory.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.jboot.web; - -import org.springframework.beans.BeansException; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; - -public class SpringBeanFactory implements ApplicationContextAware { - private static ApplicationContext context = null; - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - context = applicationContext; - } - - public static Object getBean(String name){ - return context.getBean(name); - } - - public static Object getBean(Class name){ - return context.getBean(name); - } -} \ No newline at end of file diff --git a/src/test/java/spring/AppProviderConfig.java b/src/test/java/spring/AppProviderConfig.java index 352a106c..c5458e05 100644 --- a/src/test/java/spring/AppProviderConfig.java +++ b/src/test/java/spring/AppProviderConfig.java @@ -7,6 +7,7 @@ import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.druid.DruidPlugin; import com.jfinal.plugin.druid.DruidStatViewHandler; import com.jfinal.template.Engine; +import io.jboot.aop.jfinal.SpringPlugin; import io.jboot.web.JbootAppConfig; /** diff --git a/src/test/java/spring/CodeConfig.java b/src/test/java/spring/CodeConfig.java index 5e08f28b..6663277e 100644 --- a/src/test/java/spring/CodeConfig.java +++ b/src/test/java/spring/CodeConfig.java @@ -8,7 +8,7 @@ import org.springframework.stereotype.Service; @Service public class CodeConfig { - private String codeError = "tsetset"; + private String codeError = "270049"; public String getCodeError() { return codeError; diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml index a1e377bd..8d3cd0b3 100644 --- a/src/test/resources/applicationContext.xml +++ b/src/test/resources/applicationContext.xml @@ -13,7 +13,7 @@ - + \ No newline at end of file -- Gitee From c287926ff2c081c7669d727c9dc14833b16e3eb4 Mon Sep 17 00:00:00 2001 From: shaojava Date: Wed, 17 Jan 2018 22:05:50 +0800 Subject: [PATCH 15/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E9=85=8D=E7=BD=AE=E9=9A=94=E7=A6=BB=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/jboot/config/JbootConfigConfig.java | 3 +- .../io/jboot/config/JbootConfigManager.java | 6 ++- .../config/client/ConfigRemoteReader.java | 12 +++--- .../java/distributedconfig/ConfigClient.java | 11 +++-- .../java/distributedconfig/ConfigClient1.java | 43 +++++++++++++++++++ .../java/distributedconfig/ConfigServer.java | 18 +------- src/test/java/distributedconfig/MyConfig.java | 2 +- .../java/distributedconfig/MyConfig1.java | 22 ++++++++++ .../resources/config/jboot-mos.properties | 3 ++ src/test/resources/config/jboot.properties | 7 +++ 10 files changed, 96 insertions(+), 31 deletions(-) create mode 100644 src/test/java/distributedconfig/ConfigClient1.java create mode 100644 src/test/java/distributedconfig/MyConfig1.java create mode 100644 src/test/resources/config/jboot-mos.properties create mode 100644 src/test/resources/config/jboot.properties diff --git a/src/main/java/io/jboot/config/JbootConfigConfig.java b/src/main/java/io/jboot/config/JbootConfigConfig.java index b8b7c139..9428f7a6 100644 --- a/src/main/java/io/jboot/config/JbootConfigConfig.java +++ b/src/main/java/io/jboot/config/JbootConfigConfig.java @@ -51,7 +51,7 @@ public class JbootConfigConfig { /** * 应用名 区分配置文件 */ - private String appName=""; + private String appName="jboot"; public String getAppName() { return appName; @@ -100,5 +100,4 @@ public class JbootConfigConfig { public void setExclude(String exclude) { this.exclude = exclude; } - } diff --git a/src/main/java/io/jboot/config/JbootConfigManager.java b/src/main/java/io/jboot/config/JbootConfigManager.java index 5a3690a1..0c36f870 100644 --- a/src/main/java/io/jboot/config/JbootConfigManager.java +++ b/src/main/java/io/jboot/config/JbootConfigManager.java @@ -291,7 +291,6 @@ public class JbootConfigManager { return s; } - if (type == Integer.class || type == int.class) { return Integer.parseInt(s); } else if (type == Long.class || type == long.class) { @@ -324,7 +323,10 @@ public class JbootConfigManager { private void initConfigRemoteReader() { configRemoteReader = new ConfigRemoteReader(config.getRemoteUrl(), config.getAppName(), 5) { @Override - public void onChange(String key, String oldValue, String value) { + public void onChange(String appName,String key, String oldValue, String value) { + + if(!appName.equals(name)) + return; /** * 过滤掉系统启动参数设置 */ diff --git a/src/main/java/io/jboot/config/client/ConfigRemoteReader.java b/src/main/java/io/jboot/config/client/ConfigRemoteReader.java index b843df43..91de0037 100755 --- a/src/main/java/io/jboot/config/client/ConfigRemoteReader.java +++ b/src/main/java/io/jboot/config/client/ConfigRemoteReader.java @@ -39,7 +39,7 @@ public abstract class ConfigRemoteReader { private Timer timer; private TimerTask task; private String url; - private String name; + protected String name; private int interval; private boolean running = false; @@ -104,7 +104,7 @@ public abstract class ConfigRemoteReader { } } - public abstract void onChange(String key, String oldValue, String newValue); + public abstract void onChange(String appName, String key, String oldValue, String newValue); private int scanFailTimes = 0; @@ -232,16 +232,16 @@ public abstract class ConfigRemoteReader { String remoteValue = newPropInfo.getString(newKey); remoteProperties.put(newKey.toString(), remoteValue); if (localValue == null && StringUtils.isNotBlank(remoteValue)) { - onChange(newKey.toString(), null, remoteValue); + onChange(key, newKey.toString(), null, remoteValue); } else if (!localValue.equals(remoteValue)) { - onChange(newKey.toString(), localValue, remoteValue); + onChange(key, newKey.toString(), localValue, remoteValue); } } for (Object localKey : localPropInfo.getProperties().keySet()) { if (newPropInfo.getString(localKey) == null) { remoteProperties.remove(localKey); - onChange(localKey.toString(), localPropInfo.getString(localKey), null); + onChange(key, localKey.toString(), localPropInfo.getString(localKey), null); } } } @@ -254,7 +254,7 @@ public abstract class ConfigRemoteReader { PropInfoMap.PropInfo propInfo = remotePropInfoMap.get(deleteId); for (Object key : propInfo.getProperties().keySet()) { remoteProperties.remove(key); - onChange(key.toString(), propInfo.getString(key), null); + onChange(deleteId, key.toString(), propInfo.getString(key), null); } } } diff --git a/src/test/java/distributedconfig/ConfigClient.java b/src/test/java/distributedconfig/ConfigClient.java index 544bff1f..c67d253f 100644 --- a/src/test/java/distributedconfig/ConfigClient.java +++ b/src/test/java/distributedconfig/ConfigClient.java @@ -23,19 +23,22 @@ public class ConfigClient { Jboot.setBootArg("jboot.config.remoteEnable", "true"); Jboot.setBootArg("jboot.config.remoteUrl", "http://127.0.0.1:8080/jboot/config"); - //Jboot.setBootArg("jboot.config.appName", "mos"); + //Jboot.setBootArg("jboot.config.appName", "mos") + //不加配置中心应用名 默认使用jboot.properties配置文件 + + Jboot.run(args); MyConfig config = Jboot.config(MyConfig.class); - /*for (int i = 0; i < 1000000; i++) { + for (int i = 0; i < 1000000; i++) { System.out.println("------myname:" + config.getName()); try { Thread.sleep(1000 * 2); } catch (InterruptedException e) { e.printStackTrace(); } - }*/ - Jboot.run(args); + } + } diff --git a/src/test/java/distributedconfig/ConfigClient1.java b/src/test/java/distributedconfig/ConfigClient1.java new file mode 100644 index 00000000..2f93b43b --- /dev/null +++ b/src/test/java/distributedconfig/ConfigClient1.java @@ -0,0 +1,43 @@ +package distributedconfig; + +import io.jboot.Jboot; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + * @package config + */ +public class ConfigClient1 { + + + /** + * 启动后,通过 http://127.0.0.1:8080/jboot/config 来查看数据 + * + * @param args + */ + public static void main(String[] args) { + + + //jboot端口号配置 + //Jboot.setBootArg("jboot.server.port", "8087"); + + Jboot.setBootArg("jboot.config.remoteEnable", "true"); + Jboot.setBootArg("jboot.config.remoteUrl", "http://127.0.0.1:8080/jboot/config"); + Jboot.setBootArg("jboot.config.appName", "mos"); + + Jboot.run(args); + + MyConfig1 config = Jboot.config(MyConfig1.class); + + for (int i = 0; i < 1000000; i++) { + System.out.println("------myname1:" + config.getName()); + try { + Thread.sleep(1000 * 2); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + +} diff --git a/src/test/java/distributedconfig/ConfigServer.java b/src/test/java/distributedconfig/ConfigServer.java index a0e9d0ef..b2cad814 100644 --- a/src/test/java/distributedconfig/ConfigServer.java +++ b/src/test/java/distributedconfig/ConfigServer.java @@ -19,22 +19,8 @@ public class ConfigServer { Jboot.setBootArg("jboot.config.serverEnable", "true"); //Jboot.setBootArg("jboot.config.path", "/Users/michael/Desktop/test"); - Jboot.setBootArg("jboot.config.path", "C://config"); + Jboot.setBootArg("jboot.config.path", "C://config");//config在resources目录下 复制到C盘 即可 Jboot.run(args); - //System.out.println(getKeyName("jboot-dmos.properties")); - //System.out.println(getKeyName("jboot.properties")); } - - private static String getKeyName(String file){ - int index = file.indexOf('.'); - file = file.substring(0,index); - index = file.indexOf('-'); - if(index<0) - { - return "jboot"; - }else{ - return file.substring(index+1); - } - } -} +} \ No newline at end of file diff --git a/src/test/java/distributedconfig/MyConfig.java b/src/test/java/distributedconfig/MyConfig.java index f529cae0..5a6e70e3 100644 --- a/src/test/java/distributedconfig/MyConfig.java +++ b/src/test/java/distributedconfig/MyConfig.java @@ -7,7 +7,7 @@ import io.jboot.config.annotation.PropertyConfig; * @version V1.0 * @Package distributedconfig */ -@PropertyConfig(prefix = "jboot.mos") +@PropertyConfig(prefix = "my") public class MyConfig { private String name = "defalutName"; diff --git a/src/test/java/distributedconfig/MyConfig1.java b/src/test/java/distributedconfig/MyConfig1.java new file mode 100644 index 00000000..6ea95e27 --- /dev/null +++ b/src/test/java/distributedconfig/MyConfig1.java @@ -0,0 +1,22 @@ +package distributedconfig; + +import io.jboot.config.annotation.PropertyConfig; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + * @Package distributedconfig + */ +@PropertyConfig(prefix = "my") +public class MyConfig1 { + + private String name = "defalutName"; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/src/test/resources/config/jboot-mos.properties b/src/test/resources/config/jboot-mos.properties new file mode 100644 index 00000000..7a4f1bc9 --- /dev/null +++ b/src/test/resources/config/jboot-mos.properties @@ -0,0 +1,3 @@ +jboot.mode=dev +mos.my.name=mos1 +jboot.server.port=8087 \ No newline at end of file diff --git a/src/test/resources/config/jboot.properties b/src/test/resources/config/jboot.properties new file mode 100644 index 00000000..3a1173e6 --- /dev/null +++ b/src/test/resources/config/jboot.properties @@ -0,0 +1,7 @@ +#jboot.datasource.type=mysql +#jboot.datasource.url=jdbc:mysql://localhost/jfinal_demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull +#jboot.datasource.user=root +#jboot.datasource.password=root + +my.name=jock2 +jboot.server.port=8088 \ No newline at end of file -- Gitee From 6361c3937ef81c0243bc4d3e39e8b20ce7df06e5 Mon Sep 17 00:00:00 2001 From: shaojava Date: Wed, 17 Jan 2018 22:27:20 +0800 Subject: [PATCH 16/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/jboot/config/JbootConfigManager.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/jboot/config/JbootConfigManager.java b/src/main/java/io/jboot/config/JbootConfigManager.java index 0c36f870..e00abc34 100644 --- a/src/main/java/io/jboot/config/JbootConfigManager.java +++ b/src/main/java/io/jboot/config/JbootConfigManager.java @@ -358,10 +358,12 @@ public class JbootConfigManager { configRemoteReader.start(); } - private String getKeyName(String file){ - int index = file.indexOf('.'); + private static String getKeyName(String file){ + File fileio = new File(file); + file = fileio.getName(); + int index = file.lastIndexOf('.'); file = file.substring(0,index); - index = file.indexOf('-'); + index = file.lastIndexOf('-'); if(index<0) { return "jboot"; -- Gitee From 1276a96612967d7fdd6f6b626f2c3d7cd1a494db Mon Sep 17 00:00:00 2001 From: shaojava Date: Wed, 17 Jan 2018 23:21:49 +0800 Subject: [PATCH 17/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=A7=BB=E9=99=A4sprin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/jboot/aop/jfinal/SpringPlugin.java | 65 ------------------- src/test/java/spring/AppProviderConfig.java | 31 --------- src/test/java/spring/CodeConfig.java | 20 ------ src/test/java/spring/ControllerTest.java | 39 ----------- src/test/resources/applicationContext.xml | 19 ------ 5 files changed, 174 deletions(-) delete mode 100644 src/main/java/io/jboot/aop/jfinal/SpringPlugin.java delete mode 100644 src/test/java/spring/AppProviderConfig.java delete mode 100644 src/test/java/spring/CodeConfig.java delete mode 100644 src/test/java/spring/ControllerTest.java delete mode 100644 src/test/resources/applicationContext.xml diff --git a/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java b/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java deleted file mode 100644 index 4c98d022..00000000 --- a/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.jboot.aop.jfinal; - -import com.jfinal.kit.PathKit; -import com.jfinal.plugin.IPlugin; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.FileSystemXmlApplicationContext; - -/** - * SpringPlugin. - */ -public class SpringPlugin implements IPlugin { - - private String[] configurations; - private static ApplicationContext ctx=null; - - /** - * Use configuration under the path of WebRoot/WEB-INF. - */ - public SpringPlugin() { - } - - public SpringPlugin(String... configurations) { - this.configurations = configurations; - } - - public boolean start() { - if (configurations != null) - ctx = new FileSystemXmlApplicationContext(configurations); - else - ctx = new FileSystemXmlApplicationContext(PathKit.getWebRootPath() + "/applicationContext.xml"); - return true; - } - - public boolean stop() { - return true; - } - - public static Object getBean(String name){ - if(ctx!=null) - return ctx.getBean(name); - return null; - } - - public static Object getBean(Class name){ - if(ctx!=null) - return ctx.getBean(name); - return null; - } -} diff --git a/src/test/java/spring/AppProviderConfig.java b/src/test/java/spring/AppProviderConfig.java deleted file mode 100644 index c5458e05..00000000 --- a/src/test/java/spring/AppProviderConfig.java +++ /dev/null @@ -1,31 +0,0 @@ -package spring; - -import com.jfinal.config.*; -import com.jfinal.core.JFinal; -import com.jfinal.kit.PropKit; -import com.jfinal.plugin.activerecord.ActiveRecordPlugin; -import com.jfinal.plugin.druid.DruidPlugin; -import com.jfinal.plugin.druid.DruidStatViewHandler; -import com.jfinal.template.Engine; -import io.jboot.aop.jfinal.SpringPlugin; -import io.jboot.web.JbootAppConfig; - -/** - * 本 demo 仅表达最为粗浅的 jfinal 用法,更为有价值的实用的企业级用法 - * 详见 JFinal 俱乐部: http://jfinal.com/club - *

- * API引导式配置 - */ -public class AppProviderConfig extends JbootAppConfig { - - /** - * 配置插件 - */ - @Override - public void configPlugin(Plugins me) { - super.configPlugin(me); - // 启动Spring插件 - me.add(new SpringPlugin()); - } - -} \ No newline at end of file diff --git a/src/test/java/spring/CodeConfig.java b/src/test/java/spring/CodeConfig.java deleted file mode 100644 index 6663277e..00000000 --- a/src/test/java/spring/CodeConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package spring; - -import org.springframework.stereotype.Service; - -/** - * Created by knightliao on 15/1/7. - */ -@Service -public class CodeConfig { - - private String codeError = "270049"; - - public String getCodeError() { - return codeError; - } - - public void setCodeError(String codeError) { - this.codeError = codeError; - } -} \ No newline at end of file diff --git a/src/test/java/spring/ControllerTest.java b/src/test/java/spring/ControllerTest.java deleted file mode 100644 index 5c0438a1..00000000 --- a/src/test/java/spring/ControllerTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package spring; - -import com.jfinal.core.Controller; -import io.jboot.Jboot; -import io.jboot.aop.annotation.Bean; -import io.jboot.core.cache.annotation.Cacheable; -import io.jboot.web.controller.JbootController; -import io.jboot.web.controller.annotation.RequestMapping; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import javax.inject.Inject; -import javax.inject.Singleton; - -@RequestMapping("/spring") -public class ControllerTest extends JbootController { - - @Autowired - private CodeConfig codeConfig; - - public static void main(String[] args) { - -// Jboot.setBootArg("jboot.hystrix.url", "/hystrix.stream"); -// Jboot.setBootArg("jboot.cache.type", "redis"); -// Jboot.setBootArg("jboot.metrics.url", "/metrics.abc"); -// Jboot.setBootArg("jboot.cache.redis.host", "127.0.0.1"); - - String name = AppProviderConfig.class.getName(); - - Jboot.setBootArg("jboot.jfinalConfig",name); - Jboot.run(args); - - } - - public void index(){ - String codeError = codeConfig.getCodeError(); - renderText("hello world."+codeError); - } -} diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml deleted file mode 100644 index 8d3cd0b3..00000000 --- a/src/test/resources/applicationContext.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - \ No newline at end of file -- Gitee From d043b0e42279a4c45f4806933bb468001b16896b Mon Sep 17 00:00:00 2001 From: shaojava Date: Wed, 17 Jan 2018 23:22:15 +0800 Subject: [PATCH 18/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 19 ------- .../io/jboot/web/JbootControllerManager.java | 56 ------------------- 2 files changed, 75 deletions(-) diff --git a/pom.xml b/pom.xml index 2286a93f..5c9fd684 100644 --- a/pom.xml +++ b/pom.xml @@ -45,25 +45,6 @@ - - - org.springframework - spring-aop - 4.2.4.RELEASE - - - - org.springframework - spring-aspects - 4.2.4.RELEASE - - - - - org.springframework - spring-context - 4.2.4.RELEASE - com.jfinal diff --git a/src/main/java/io/jboot/web/JbootControllerManager.java b/src/main/java/io/jboot/web/JbootControllerManager.java index 64d88233..b8443cee 100644 --- a/src/main/java/io/jboot/web/JbootControllerManager.java +++ b/src/main/java/io/jboot/web/JbootControllerManager.java @@ -20,8 +20,6 @@ import com.google.common.collect.HashBiMap; import com.jfinal.core.Controller; import com.jfinal.core.ControllerFactory; import io.jboot.Jboot; -import io.jboot.aop.jfinal.SpringPlugin; -import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Resource; import java.lang.reflect.Field; @@ -50,22 +48,11 @@ public class JbootControllerManager extends ControllerFactory { } }; - /*public Controller getController(Class controllerClass) throws InstantiationException, IllegalAccessException { - Controller ret = buffers.get().get(controllerClass); - if (ret == null) { - ret = controllerClass.newInstance(); - Jboot.injectMembers(ret); - buffers.get().put(controllerClass, ret); - } - return ret; - }*/ - public Controller getController(Class controllerClass) throws InstantiationException, IllegalAccessException { Controller ret = buffers.get().get(controllerClass); if (ret == null) { ret = controllerClass.newInstance(); Jboot.injectMembers(ret); - autoInject(ret); buffers.get().put(controllerClass, ret); } return ret; @@ -85,47 +72,4 @@ public class JbootControllerManager extends ControllerFactory { controllerMapping.put(path, controllerClass); } - /** - * 自动注入 - * @param ret controller - */ - public void autoInject(Controller ret) { - Field[] fields = ret.getClass().getDeclaredFields(); - for (Field field : fields) { - Object bean = null; - if (field.isAnnotationPresent(Autowired.class)) { - try { - bean = SpringPlugin.getBean(field.getName()); - } catch (Exception e) { - } - if (null == bean) { - try { - bean = SpringPlugin.getBean(field.getType()); - } catch (Exception e) { - } - } - if (null == bean) { - Autowired annotation = field.getAnnotation(Autowired.class); - if (annotation.required()) { - throw new RuntimeException(String.format( - "Error creating bean with name '%s': Injection of autowired dependencies failed.", - field.getName())); - } - } - } else if (field.isAnnotationPresent(Resource.class)) { - Resource annotation = field.getAnnotation(Resource.class); - bean = SpringPlugin.getBean(annotation.name().trim().length() == 0 ? field.getName() : annotation.name()); - } else - continue; - try { - if (bean != null) { - field.setAccessible(true); - field.set(ret, bean); - } - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - } \ No newline at end of file -- Gitee From 280471066a29588777825584126d6b3fad0d2c34 Mon Sep 17 00:00:00 2001 From: shaojava Date: Thu, 18 Jan 2018 00:05:31 +0800 Subject: [PATCH 19/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E9=BB=98=E8=AE=A4=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/jboot/config/server/JbootConfigController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/jboot/config/server/JbootConfigController.java b/src/main/java/io/jboot/config/server/JbootConfigController.java index 6a2636e5..d5726f1a 100644 --- a/src/main/java/io/jboot/config/server/JbootConfigController.java +++ b/src/main/java/io/jboot/config/server/JbootConfigController.java @@ -33,7 +33,7 @@ import java.util.List; * 配置文件的Controller,用于给其他应用提供分布式配置读取功能 */ @Clear -@RequestMapping("/jboot/config") +@RequestMapping("/mos/config") @Before(JbootConfigInterceptor.class) public class JbootConfigController extends JbootController { -- Gitee From 8024139eaf6eccb363554c9bb7fbec5766a44411 Mon Sep 17 00:00:00 2001 From: shaojava Date: Thu, 18 Jan 2018 00:11:09 +0800 Subject: [PATCH 20/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/jboot/JbootConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/jboot/JbootConfig.java b/src/main/java/io/jboot/JbootConfig.java index a1a6cf81..d46fdd28 100644 --- a/src/main/java/io/jboot/JbootConfig.java +++ b/src/main/java/io/jboot/JbootConfig.java @@ -22,7 +22,7 @@ import io.jboot.web.JbootAppConfig; public class JbootConfig { - private String version = "1.2.8"; + private String version = "1.2.9"; private String mode = Jboot.MODE.DEV.getValue(); private boolean bannerEnable = true; private String bannerFile = "banner.txt"; -- Gitee From 0ade56aee90be45b5c4eec73f320a646c248c602 Mon Sep 17 00:00:00 2001 From: shaojava Date: Fri, 19 Jan 2018 00:08:27 +0800 Subject: [PATCH 21/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/mqzbus/ZbusMqClientDemo.java | 5 ++--- src/test/java/mqzbus/ZbusMqServerDemo.java | 8 +++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/test/java/mqzbus/ZbusMqClientDemo.java b/src/test/java/mqzbus/ZbusMqClientDemo.java index 6a260b0c..e7ee4d24 100644 --- a/src/test/java/mqzbus/ZbusMqClientDemo.java +++ b/src/test/java/mqzbus/ZbusMqClientDemo.java @@ -39,11 +39,10 @@ public class ZbusMqClientDemo extends JbootController { //RPC配置 Jboot.setBootArg("jboot.mq.type", "zbus"); - Jboot.setBootArg("jboot.mq.channel", "myChannel,myChannel1,myChannel2"); - + Jboot.setBootArg("jboot.mq.channel", "CpMQ,RefreshCpMQ"); + Jboot.setBootArg("jboot.mq.zbus.mode", "consumer"); Jboot.setBootArg("jboot.mq.zbus.broker", "127.0.0.1:15555"); - Jboot.me().getMq().addMessageListener(new JbootmqMessageListener() { @Override public void onMessage(String channel, Object message) { diff --git a/src/test/java/mqzbus/ZbusMqServerDemo.java b/src/test/java/mqzbus/ZbusMqServerDemo.java index 1125c6b6..e0e011a6 100644 --- a/src/test/java/mqzbus/ZbusMqServerDemo.java +++ b/src/test/java/mqzbus/ZbusMqServerDemo.java @@ -31,7 +31,7 @@ public class ZbusMqServerDemo extends JbootController { Jboot.setBootArg("jboot.mq.type", "zbus"); Jboot.setBootArg("jboot.mq.zbus.mode", "producer"); Jboot.setBootArg("jboot.mq.zbus.broker", "127.0.0.1:15555"); - Jboot.setBootArg("jboot.mq.channel", "CpMQ"); + Jboot.setBootArg("jboot.mq.channel", "CpMQ,RefreshCpMQ"); /*Jboot.me().getMq().addMessageListener(new JbootmqMessageListener() { @Override @@ -57,4 +57,10 @@ public class ZbusMqServerDemo extends JbootController { Jboot.me().getMq().publish(msg, "CpMQ"); renderText(msg); } + + public void ref() { + String msg = "message "+(count++); + Jboot.me().getMq().publish(msg, "RefreshCpMQ"); + renderText(msg); + } } -- Gitee From fbd0213ae9197a781be851b35857436fcd74e0a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B5=E6=96=87=E9=95=BF?= Date: Mon, 22 Jan 2018 10:27:18 +0800 Subject: [PATCH 22/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Metrics=E5=88=B0Influxd?= =?UTF-8?q?b=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 +- .../reporter/influxdb/InfluxdbReporter.java | 32 ++++++++- .../JbootMetricsInfluxdbReporterConfig.java | 71 +++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java diff --git a/pom.xml b/pom.xml index 5c9fd684..3523078d 100644 --- a/pom.xml +++ b/pom.xml @@ -471,7 +471,12 @@ provided - + + + com.github.davidb + metrics-influxdb + 0.9.3 + diff --git a/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java b/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java index 552830fc..b374a2ba 100644 --- a/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java +++ b/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java @@ -15,18 +15,48 @@ */ package io.jboot.component.metric.reporter.influxdb; +import com.codahale.metrics.MetricFilter; import com.codahale.metrics.MetricRegistry; +import io.jboot.Jboot; import io.jboot.component.metric.JbootMetricReporter; +import java.util.concurrent.TimeUnit; + +import io.jboot.utils.StringUtils; +import metrics_influxdb.HttpInfluxdbProtocol; + /** * @author Michael Yang 杨福海 (fuhai999@gmail.com) * @version V1.0 - * @Package io.jboot.component.metrics.reporter.jmx + * @Package io.jboot.component.metrics.reporter.influxdb */ public class InfluxdbReporter implements JbootMetricReporter { @Override public void report(MetricRegistry metricRegistry) { + JbootMetricsInfluxdbReporterConfig config = Jboot.config(JbootMetricsInfluxdbReporterConfig.class); + + if (StringUtils.isBlank(config.getHost())) { + throw new NullPointerException("influxdb reporter host must not be null, please config jboot.metrics.reporter.influxdb.host in you properties."); + } + if (config.getPort() == null) { + throw new NullPointerException("influxdb reporter port must not be null, please config jboot.metrics.reporter.influxdb.port in you properties."); + } + if (config.getUsername() == null) { + throw new NullPointerException("influxdb reporter username must not be null, please config jboot.metrics.reporter.influxdb.username in you properties."); + } + if (config.getDatabase() == null) { + throw new NullPointerException("influxdb reporter database must not be null, please config jboot.metrics.reporter.influxdb.database in you properties."); + } + + metrics_influxdb.InfluxdbReporter.forRegistry(metricRegistry) + .protocol(new HttpInfluxdbProtocol("http",config.getHost(), config.getPort(), config.getUsername(), config.getPassword(), config.getDatabase())) + .convertRatesTo(TimeUnit.SECONDS) + .convertDurationsTo(TimeUnit.MILLISECONDS) + .filter(MetricFilter.ALL) + .skipIdleMetrics(false) + .build(); } + } diff --git a/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java b/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java new file mode 100644 index 00000000..9bbe4f6a --- /dev/null +++ b/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java @@ -0,0 +1,71 @@ +/** + * Copyright (c) 2015-2018, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jboot.component.metric.reporter.influxdb; + +import io.jboot.config.annotation.PropertyConfig; + +@PropertyConfig(prefix = "jboot.metrics.reporter.influxdb") +public class JbootMetricsInfluxdbReporterConfig { + + private String host; + private Integer port = 8083; + private String username; + private String password=""; + private String database; + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getDatabase() { + return database; + } + + public void setDatabase(String database) { + this.database = database; + } +} + + + -- Gitee From d128e03d57c2a154ff20314631e0c2b997ecb27c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B5=E6=96=87=E9=95=BF?= Date: Mon, 22 Jan 2018 10:39:54 +0800 Subject: [PATCH 23/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B7=BB=E5=8A=A0autho?= =?UTF-8?q?r=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metric/reporter/influxdb/InfluxdbReporter.java | 8 ++------ .../influxdb/JbootMetricsInfluxdbReporterConfig.java | 6 ++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java b/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java index c25ddb81..3e41544c 100644 --- a/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java +++ b/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java @@ -26,13 +26,9 @@ import io.jboot.utils.StringUtils; import metrics_influxdb.HttpInfluxdbProtocol; /** - * @author Michael Yang 杨福海 (fuhai999@gmail.com) - * @version V1.0 -<<<<<<< HEAD + * @author shaojava 邵文长 (shaojava@sina.com) + * @version V1.1 * @Package io.jboot.component.metrics.reporter.influxdb -======= - * @Package io.jboot.component.metric.reporter.jmx ->>>>>>> 0bdc9d31f1a45a9fe2457493867b60028b82512a */ public class InfluxdbReporter implements JbootMetricReporter { diff --git a/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java b/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java index 9bbe4f6a..862a1f0b 100644 --- a/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java +++ b/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java @@ -17,6 +17,12 @@ package io.jboot.component.metric.reporter.influxdb; import io.jboot.config.annotation.PropertyConfig; + +/** + * @author shaojava 邵文长 (shaojava@sina.com) + * @version V1.1 + * @Package io.jboot.component.metrics.reporter.influxdb + */ @PropertyConfig(prefix = "jboot.metrics.reporter.influxdb") public class JbootMetricsInfluxdbReporterConfig { -- Gitee From 7ef17613518669549d117a5f63963e189a74c901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B5=E6=96=87=E9=95=BF?= Date: Wed, 24 Jan 2018 17:33:02 +0800 Subject: [PATCH 24/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../influxdb/JbootMetricsInfluxdbReporterConfig.java | 4 ++-- src/test/java/MetricsTest.java | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java b/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java index 862a1f0b..3ac0e044 100644 --- a/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java +++ b/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java @@ -23,11 +23,11 @@ import io.jboot.config.annotation.PropertyConfig; * @version V1.1 * @Package io.jboot.component.metrics.reporter.influxdb */ -@PropertyConfig(prefix = "jboot.metrics.reporter.influxdb") +@PropertyConfig(prefix = "jboot.metric.reporter.influxdb") public class JbootMetricsInfluxdbReporterConfig { private String host; - private Integer port = 8083; + private Integer port = 8086; private String username; private String password=""; private String database; diff --git a/src/test/java/MetricsTest.java b/src/test/java/MetricsTest.java index a503ca78..08303ef6 100644 --- a/src/test/java/MetricsTest.java +++ b/src/test/java/MetricsTest.java @@ -4,11 +4,15 @@ public class MetricsTest { public static void main(String[] args) { - Jboot.setBootArg("jboot.metrics.url", "/metrics.html/*"); - Jboot.setBootArg("jboot.metrics.jmxReporter", true); - Jboot.run(args); - + Jboot.setBootArg("jboot.metric.url", "/metrics.html/*"); + Jboot.setBootArg("jboot.metric.reporter", "influxdb"); + Jboot.setBootArg("jboot.metric.reporter.influxdb.host", "192.168.12.204"); + Jboot.setBootArg("jboot.metric.reporter.influxdb.port", "8086"); + Jboot.setBootArg("jboot.metric.reporter.influxdb.username", "jmeter"); + Jboot.setBootArg("jboot.metric.reporter.influxdb.password", "jmeter"); + Jboot.setBootArg("jboot.metric.reporter.influxdb.database", "jmeter"); + Jboot.run(args); } -- Gitee From 9f2f2c8bea228e2288070102bcad3b5e108bde6a Mon Sep 17 00:00:00 2001 From: shaojava Date: Wed, 24 Jan 2018 23:26:30 +0800 Subject: [PATCH 25/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9Metrics=E5=88=B0Influxd?= =?UTF-8?q?b=E7=9B=91=E6=8E=A7=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reporter/influxdb/InfluxdbReporter.java | 4 +- src/test/java/MetricsTest.java | 65 ++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java b/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java index 3e41544c..6950ea0c 100644 --- a/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java +++ b/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java @@ -17,6 +17,7 @@ package io.jboot.component.metric.reporter.influxdb; import com.codahale.metrics.MetricFilter; import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.ScheduledReporter; import io.jboot.Jboot; import io.jboot.component.metric.JbootMetricReporter; @@ -50,13 +51,14 @@ public class InfluxdbReporter implements JbootMetricReporter { throw new NullPointerException("influxdb reporter database must not be null, please config jboot.metrics.reporter.influxdb.database in you properties."); } - metrics_influxdb.InfluxdbReporter.forRegistry(metricRegistry) + final ScheduledReporter reporter = metrics_influxdb.InfluxdbReporter.forRegistry(metricRegistry) .protocol(new HttpInfluxdbProtocol("http",config.getHost(), config.getPort(), config.getUsername(), config.getPassword(), config.getDatabase())) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .filter(MetricFilter.ALL) .skipIdleMetrics(false) .build(); + reporter.start(10, TimeUnit.SECONDS); } } diff --git a/src/test/java/MetricsTest.java b/src/test/java/MetricsTest.java index 08303ef6..6679dded 100644 --- a/src/test/java/MetricsTest.java +++ b/src/test/java/MetricsTest.java @@ -1,12 +1,23 @@ +import com.codahale.metrics.Gauge; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import com.jfinal.core.Controller; import io.jboot.Jboot; +import io.jboot.web.controller.JbootController; +import io.jboot.web.controller.annotation.RequestMapping; -public class MetricsTest { +import java.util.Queue; +import java.util.Random; +import java.util.concurrent.LinkedBlockingDeque; +@RequestMapping("/") +public class MetricsTest extends JbootController{ public static void main(String[] args) { Jboot.setBootArg("jboot.metric.url", "/metrics.html/*"); Jboot.setBootArg("jboot.metric.reporter", "influxdb"); - Jboot.setBootArg("jboot.metric.reporter.influxdb.host", "192.168.12.204"); + //Jboot.setBootArg("jboot.metric.reporter.influxdb.host", "192.168.12.204"); + Jboot.setBootArg("jboot.metric.reporter.influxdb.host", "192.168.0.110"); Jboot.setBootArg("jboot.metric.reporter.influxdb.port", "8086"); Jboot.setBootArg("jboot.metric.reporter.influxdb.username", "jmeter"); Jboot.setBootArg("jboot.metric.reporter.influxdb.password", "jmeter"); @@ -15,6 +26,56 @@ public class MetricsTest { Jboot.run(args); } + //计数器 + public void counter(){ + //Jboot.me().getMetric().counter("cpu").inc();//递增 + Jboot.me().getMetric().counter("cp").inc();//递减 + renderText("counter"); + } + + //度量一系列事件发生的速率(rate),例如TPS。Meters会统计最近1分钟,5分钟,15分钟,还有全部时间的速率 + public void meter(){ + //度量某个时间段的平均处理次数 tps + Jboot.me().getMetric().meter("mt").mark(); + renderText("meter"); + } + + private static Queue queue = new LinkedBlockingDeque(); + + //统计瞬时状态的数据信息 + public void gauges(){ + //实例化一个Gauge + Gauge gauge = new Gauge() { + @Override + public Integer getValue() { + return queue.size(); + } + }; + Jboot.me().getMetric().register(MetricRegistry.name(MetricsTest.class, "pending-job", "size"),gauge); + renderText("gauges"); + } + + //统计数据的分布情况。比如最小值,最大值,中间值,还有中位数,75百分位, 90百分位, 95百分位, 98百分位, 99百分位, 和 99.9百分位的值 + public void histogram(){ + Jboot.me().getMetric().histogram("cpu").update(random.nextInt(100000)); + renderText("histogram"); + } + + public static Random random = new Random(); + + //是 Histogram 和 Meter 的结合, histogram 某部分代码/调用的耗时, meter统计TPS + public void timer(){ + try { + Timer.Context ctx = Jboot.me().getMetric().timer("timer").time(); + Thread.sleep(random.nextInt(1000)); + ctx.stop(); + renderText("timer"); + }catch (Exception ex){ + + } + } + + //一般情况下,当我们需要统计某个函数被调用的频率(TPS),会使用Meters。当我们需要统计某个函数的执行耗时时,会使用Histograms。当我们既要统计TPS又要统计耗时时,我们会使用Timers。 } -- Gitee From 3b5df7729bbbbe6462fd8ce249a055123e7f9dc4 Mon Sep 17 00:00:00 2001 From: shaojava Date: Fri, 26 Jan 2018 23:45:10 +0800 Subject: [PATCH 26/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hystrix/JbootHystrixCommand.java | 26 ++++++++++++++++--- src/test/java/aop/AopDemo.java | 2 +- src/test/java/hystrix/ClientDemo.java | 18 ++++++++++++- src/test/java/hystrix/ServerDemo.java | 3 ++- src/test/java/opentracing/ClientDemo.java | 2 +- src/test/java/service/CategoryService.java | 2 +- .../java/service/CategoryServiceImpl.java | 3 ++- .../service/NamedCategoryServiceImpl.java | 2 +- src/test/java/service/UserServiceImpl.java | 4 +-- 9 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/jboot/component/hystrix/JbootHystrixCommand.java b/src/main/java/io/jboot/component/hystrix/JbootHystrixCommand.java index 6467ff34..960fcf70 100644 --- a/src/main/java/io/jboot/component/hystrix/JbootHystrixCommand.java +++ b/src/main/java/io/jboot/component/hystrix/JbootHystrixCommand.java @@ -15,9 +15,7 @@ */ package io.jboot.component.hystrix; -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import com.netflix.hystrix.HystrixThreadPoolKey; +import com.netflix.hystrix.*; public abstract class JbootHystrixCommand extends HystrixCommand { @@ -27,7 +25,27 @@ public abstract class JbootHystrixCommand extends HystrixCommand { } public JbootHystrixCommand(String key, int executionIsolationThreadTimeoutInMilliseconds) { - super(HystrixCommandGroupKey.Factory.asKey(key), executionIsolationThreadTimeoutInMilliseconds); + //super(HystrixCommandGroupKey.Factory.asKey(key), executionIsolationThreadTimeoutInMilliseconds); + super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(key)) + .andCommandKey(HystrixCommandKey.Factory.asKey(key)) + .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(key+"ThreadPool")) + // 重写断路器基本策略属性 + .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() + .withExecutionTimeoutInMilliseconds(executionIsolationThreadTimeoutInMilliseconds))); +// // 断路器是否开启,默认:true +// .withCircuitBreakerEnabled(true) +// // 统计时间滚动窗口,以毫秒为单位,默认:10秒 +// .withMetricsRollingStatisticalWindowInMilliseconds(10000) +// // 断路器在整个统计时间内是否开启的阀值,也就是10秒钟内至少请求10次,断路器才发挥起作用 +// .withCircuitBreakerRequestVolumeThreshold(10) +// // 断路器默认工作时间,默认:5秒,断路器中断请求5秒后会进入半打开状态,放部分流量过去重试 +// .withCircuitBreakerSleepWindowInMilliseconds(5000) +// // 当出错率超过30%后断路器启动,默认:50% +// .withCircuitBreakerErrorThresholdPercentage(30) +// // 是否开启Hystrix超时机制,这里禁用Hystrix的超时,使用dubbo的超时 +// .withExecutionTimeoutEnabled(false)) +// // 线程池配置,可考虑获取Dubbo提供者的线程数来配置断路器的线程数,若使用Hystrix的线程数则应大于Dubbo服务提供者的线程数,保证管道匹配 +// .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(DEFAULT_THREADPOOL_CORE_SIZE))); } public JbootHystrixCommand(HystrixCommandGroupKey group) { diff --git a/src/test/java/aop/AopDemo.java b/src/test/java/aop/AopDemo.java index 5dbf1f73..cb035b45 100644 --- a/src/test/java/aop/AopDemo.java +++ b/src/test/java/aop/AopDemo.java @@ -43,7 +43,7 @@ public class AopDemo extends JbootController { public void index() { - renderHtml("service:" + service.hello("") + "
nameservice:" + nameservice.hello("")); + renderHtml("service:" + service.hello1("") + "
nameservice:" + nameservice.hello1("")); } diff --git a/src/test/java/hystrix/ClientDemo.java b/src/test/java/hystrix/ClientDemo.java index ba842f16..0aaebc71 100644 --- a/src/test/java/hystrix/ClientDemo.java +++ b/src/test/java/hystrix/ClientDemo.java @@ -16,9 +16,11 @@ package hystrix; import io.jboot.Jboot; +import io.jboot.component.hystrix.annotation.EnableHystrixCommand; import io.jboot.core.rpc.Jbootrpc; import io.jboot.web.controller.JbootController; import io.jboot.web.controller.annotation.RequestMapping; +import service.CategoryService; import service.UserService; @@ -42,6 +44,8 @@ public class ClientDemo extends JbootController { Jboot.setBootArg("jboot.rpc.type", "motan"); Jboot.setBootArg("jboot.rpc.callMode", "redirect");//直连模式,默认为注册中心 Jboot.setBootArg("jboot.rpc.directUrl", "localhost:8002");//直连模式的url地址 + //Jboot.setBootArg("jboot.rpc.hystrixKeys", "hello");//注册服务 + Jboot.setBootArg("jboot.rpc.hystrixKeys", "UserService:hello;CategoryService:hello1");//注册服务 //hystrix配置 Jboot.setBootArg("jboot.hystrix.url", "/hystrix.html");//配置 Hystrix Dashboard 的监控路径 @@ -63,9 +67,21 @@ public class ClientDemo extends JbootController { System.out.println(service.hello("海哥" + i)); } - renderText("ok"); } + public void test() { + Jbootrpc jbootrpc = Jboot.me().getRpc(); + + long time = System.currentTimeMillis(); + CategoryService service = jbootrpc.serviceObtain(CategoryService.class, "jboot", "1.0"); + System.out.println("obtain:" + (System.currentTimeMillis() - time) + "---" + service); + + for (int i = 0; i < 10; i++) { + // 使用服务 + System.out.println(service.hello1("海哥" + i)); + } + renderText("test"); + } } diff --git a/src/test/java/hystrix/ServerDemo.java b/src/test/java/hystrix/ServerDemo.java index 4d5e6c3a..7147c8bf 100644 --- a/src/test/java/hystrix/ServerDemo.java +++ b/src/test/java/hystrix/ServerDemo.java @@ -32,7 +32,8 @@ public class ServerDemo { Jboot.setBootArg("jboot.rpc.type", "motan"); Jboot.setBootArg("jboot.rpc.callMode", "redirect");//直连模式,默认为注册中心 Jboot.setBootArg("jboot.rpc.directUrl", "localhost:8002");//直连模式的url地址 - + //Jboot.setBootArg("jboot.rpc.registryType", "zookeeper");//注册服务 + //Jboot.setBootArg("jboot.rpc.registryAddress", "127.0.0.1");//注册服务 Jboot.run(args); diff --git a/src/test/java/opentracing/ClientDemo.java b/src/test/java/opentracing/ClientDemo.java index b1a455f7..f7571582 100644 --- a/src/test/java/opentracing/ClientDemo.java +++ b/src/test/java/opentracing/ClientDemo.java @@ -64,7 +64,7 @@ public class ClientDemo extends JbootController { System.out.println(service.hello("海哥")); CategoryService service1 = Jboot.service(CategoryService.class); - System.out.println(service1.hello("海哥")); + System.out.println(service1.hello1("海哥")); renderText("ok"); diff --git a/src/test/java/service/CategoryService.java b/src/test/java/service/CategoryService.java index 07363bbd..4255d6d7 100644 --- a/src/test/java/service/CategoryService.java +++ b/src/test/java/service/CategoryService.java @@ -19,6 +19,6 @@ package service; public interface CategoryService { - public String hello(String text); + public String hello1(String text); } diff --git a/src/test/java/service/CategoryServiceImpl.java b/src/test/java/service/CategoryServiceImpl.java index ce3fe872..de0fc985 100644 --- a/src/test/java/service/CategoryServiceImpl.java +++ b/src/test/java/service/CategoryServiceImpl.java @@ -17,11 +17,12 @@ package service; import io.jboot.aop.annotation.Bean; +import io.jboot.component.hystrix.annotation.EnableHystrixCommand; @Bean public class CategoryServiceImpl implements CategoryService { @Override - public String hello(String text) { + public String hello1(String text) { return "CategoryServiceImpl say hello " + text; } diff --git a/src/test/java/service/NamedCategoryServiceImpl.java b/src/test/java/service/NamedCategoryServiceImpl.java index 45f84d53..27aa228e 100644 --- a/src/test/java/service/NamedCategoryServiceImpl.java +++ b/src/test/java/service/NamedCategoryServiceImpl.java @@ -20,7 +20,7 @@ import io.jboot.aop.annotation.Bean; @Bean(name = "myCategory") public class NamedCategoryServiceImpl implements CategoryService { @Override - public String hello(String text) { + public String hello1(String text) { return "NamedCategoryServiceImpl say hello " + text; } diff --git a/src/test/java/service/UserServiceImpl.java b/src/test/java/service/UserServiceImpl.java index f50b13a4..5ef85995 100644 --- a/src/test/java/service/UserServiceImpl.java +++ b/src/test/java/service/UserServiceImpl.java @@ -17,6 +17,7 @@ package service; import io.jboot.Jboot; +import io.jboot.component.hystrix.annotation.EnableHystrixCommand; import io.jboot.exception.JbootException; public class UserServiceImpl implements UserService { @@ -26,8 +27,7 @@ public class UserServiceImpl implements UserService { public String hello(String name) { System.out.println("UserServiceImpl hello invoked!!!"); - - return Jboot.service(CategoryService.class).hello(name); + return "UserServiceImpl hello invoked!!! "+name; } -- Gitee From 586dd02972b22b326796de4da66d72ce007aa8c5 Mon Sep 17 00:00:00 2001 From: shaojava Date: Thu, 1 Feb 2018 22:12:43 +0800 Subject: [PATCH 27/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B7=BB=E5=8A=A0Sprin?= =?UTF-8?q?gPlugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 19 ++++++ .../io/jboot/aop/jfinal/SpringPlugin.java | 65 +++++++++++++++++++ .../io/jboot/web/JbootControllerManager.java | 56 ++++++++++++++++ src/test/java/spring/AppProviderConfig.java | 31 +++++++++ src/test/resources/applicationContext.xml | 6 ++ 5 files changed, 177 insertions(+) create mode 100644 src/main/java/io/jboot/aop/jfinal/SpringPlugin.java create mode 100644 src/test/java/spring/AppProviderConfig.java create mode 100644 src/test/resources/applicationContext.xml diff --git a/pom.xml b/pom.xml index 2a6536d2..831b066e 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,25 @@ + + + org.springframework + spring-aop + 4.2.4.RELEASE + + + + org.springframework + spring-aspects + 4.2.4.RELEASE + + + + + org.springframework + spring-context + 4.2.4.RELEASE + com.jfinal diff --git a/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java b/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java new file mode 100644 index 00000000..4c98d022 --- /dev/null +++ b/src/main/java/io/jboot/aop/jfinal/SpringPlugin.java @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.jboot.aop.jfinal; + +import com.jfinal.kit.PathKit; +import com.jfinal.plugin.IPlugin; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.FileSystemXmlApplicationContext; + +/** + * SpringPlugin. + */ +public class SpringPlugin implements IPlugin { + + private String[] configurations; + private static ApplicationContext ctx=null; + + /** + * Use configuration under the path of WebRoot/WEB-INF. + */ + public SpringPlugin() { + } + + public SpringPlugin(String... configurations) { + this.configurations = configurations; + } + + public boolean start() { + if (configurations != null) + ctx = new FileSystemXmlApplicationContext(configurations); + else + ctx = new FileSystemXmlApplicationContext(PathKit.getWebRootPath() + "/applicationContext.xml"); + return true; + } + + public boolean stop() { + return true; + } + + public static Object getBean(String name){ + if(ctx!=null) + return ctx.getBean(name); + return null; + } + + public static Object getBean(Class name){ + if(ctx!=null) + return ctx.getBean(name); + return null; + } +} diff --git a/src/main/java/io/jboot/web/JbootControllerManager.java b/src/main/java/io/jboot/web/JbootControllerManager.java index b8443cee..64d88233 100644 --- a/src/main/java/io/jboot/web/JbootControllerManager.java +++ b/src/main/java/io/jboot/web/JbootControllerManager.java @@ -20,6 +20,8 @@ import com.google.common.collect.HashBiMap; import com.jfinal.core.Controller; import com.jfinal.core.ControllerFactory; import io.jboot.Jboot; +import io.jboot.aop.jfinal.SpringPlugin; +import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Resource; import java.lang.reflect.Field; @@ -48,11 +50,22 @@ public class JbootControllerManager extends ControllerFactory { } }; + /*public Controller getController(Class controllerClass) throws InstantiationException, IllegalAccessException { + Controller ret = buffers.get().get(controllerClass); + if (ret == null) { + ret = controllerClass.newInstance(); + Jboot.injectMembers(ret); + buffers.get().put(controllerClass, ret); + } + return ret; + }*/ + public Controller getController(Class controllerClass) throws InstantiationException, IllegalAccessException { Controller ret = buffers.get().get(controllerClass); if (ret == null) { ret = controllerClass.newInstance(); Jboot.injectMembers(ret); + autoInject(ret); buffers.get().put(controllerClass, ret); } return ret; @@ -72,4 +85,47 @@ public class JbootControllerManager extends ControllerFactory { controllerMapping.put(path, controllerClass); } + /** + * 自动注入 + * @param ret controller + */ + public void autoInject(Controller ret) { + Field[] fields = ret.getClass().getDeclaredFields(); + for (Field field : fields) { + Object bean = null; + if (field.isAnnotationPresent(Autowired.class)) { + try { + bean = SpringPlugin.getBean(field.getName()); + } catch (Exception e) { + } + if (null == bean) { + try { + bean = SpringPlugin.getBean(field.getType()); + } catch (Exception e) { + } + } + if (null == bean) { + Autowired annotation = field.getAnnotation(Autowired.class); + if (annotation.required()) { + throw new RuntimeException(String.format( + "Error creating bean with name '%s': Injection of autowired dependencies failed.", + field.getName())); + } + } + } else if (field.isAnnotationPresent(Resource.class)) { + Resource annotation = field.getAnnotation(Resource.class); + bean = SpringPlugin.getBean(annotation.name().trim().length() == 0 ? field.getName() : annotation.name()); + } else + continue; + try { + if (bean != null) { + field.setAccessible(true); + field.set(ret, bean); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + } \ No newline at end of file diff --git a/src/test/java/spring/AppProviderConfig.java b/src/test/java/spring/AppProviderConfig.java new file mode 100644 index 00000000..c5458e05 --- /dev/null +++ b/src/test/java/spring/AppProviderConfig.java @@ -0,0 +1,31 @@ +package spring; + +import com.jfinal.config.*; +import com.jfinal.core.JFinal; +import com.jfinal.kit.PropKit; +import com.jfinal.plugin.activerecord.ActiveRecordPlugin; +import com.jfinal.plugin.druid.DruidPlugin; +import com.jfinal.plugin.druid.DruidStatViewHandler; +import com.jfinal.template.Engine; +import io.jboot.aop.jfinal.SpringPlugin; +import io.jboot.web.JbootAppConfig; + +/** + * 本 demo 仅表达最为粗浅的 jfinal 用法,更为有价值的实用的企业级用法 + * 详见 JFinal 俱乐部: http://jfinal.com/club + *

+ * API引导式配置 + */ +public class AppProviderConfig extends JbootAppConfig { + + /** + * 配置插件 + */ + @Override + public void configPlugin(Plugins me) { + super.configPlugin(me); + // 启动Spring插件 + me.add(new SpringPlugin()); + } + +} \ No newline at end of file diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml new file mode 100644 index 00000000..142def25 --- /dev/null +++ b/src/test/resources/applicationContext.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file -- Gitee From b0ef888774c97a156fa248d4a68222d031dd732d Mon Sep 17 00:00:00 2001 From: shaojava Date: Thu, 1 Feb 2018 22:33:18 +0800 Subject: [PATCH 28/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/spring/CodeConfig.java | 20 ++++++++++++ src/test/java/spring/ControllerTest.java | 39 +++++++++++++++++++++++ src/test/resources/applicationContext.xml | 17 ++++++++-- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/test/java/spring/CodeConfig.java create mode 100644 src/test/java/spring/ControllerTest.java diff --git a/src/test/java/spring/CodeConfig.java b/src/test/java/spring/CodeConfig.java new file mode 100644 index 00000000..6663277e --- /dev/null +++ b/src/test/java/spring/CodeConfig.java @@ -0,0 +1,20 @@ +package spring; + +import org.springframework.stereotype.Service; + +/** + * Created by knightliao on 15/1/7. + */ +@Service +public class CodeConfig { + + private String codeError = "270049"; + + public String getCodeError() { + return codeError; + } + + public void setCodeError(String codeError) { + this.codeError = codeError; + } +} \ No newline at end of file diff --git a/src/test/java/spring/ControllerTest.java b/src/test/java/spring/ControllerTest.java new file mode 100644 index 00000000..b635b2a4 --- /dev/null +++ b/src/test/java/spring/ControllerTest.java @@ -0,0 +1,39 @@ +package spring; + +import com.jfinal.core.Controller; +import io.jboot.Jboot; +import io.jboot.aop.annotation.Bean; +import io.jboot.core.cache.annotation.Cacheable; +import io.jboot.web.controller.JbootController; +import io.jboot.web.controller.annotation.RequestMapping; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import javax.inject.Inject; +import javax.inject.Singleton; + +@RequestMapping("/spring") +public class ControllerTest extends JbootController { + + @Autowired + private CodeConfig codeConfig; + + public static void main(String[] args) { + +// Jboot.setBootArg("jboot.hystrix.url", "/hystrix.stream"); +// Jboot.setBootArg("jboot.cache.type", "redis"); +// Jboot.setBootArg("jboot.metrics.url", "/metrics.abc"); +// Jboot.setBootArg("jboot.cache.redis.host", "127.0.0.1"); + + String name = AppProviderConfig.class.getName(); + + Jboot.setBootArg("jboot.jfinalConfig",name); + Jboot.run(args); + + } + + public void index(){ + String codeError = codeConfig.getCodeError(); + renderText("hello world."+codeError); + } +} \ No newline at end of file diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml index 142def25..8d3cd0b3 100644 --- a/src/test/resources/applicationContext.xml +++ b/src/test/resources/applicationContext.xml @@ -1,6 +1,19 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.1.xsd + http://www.springframework.org/schema/aop + http://www.springframework.org/schema/aop/spring-aop-3.0.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd"> + + + + + + + \ No newline at end of file -- Gitee From 22ba6315df17ebf5d528939400ab0788a438b3b1 Mon Sep 17 00:00:00 2001 From: shaowenchang Date: Sat, 3 Feb 2018 10:10:35 +0800 Subject: [PATCH 29/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/limitation/LimitationDemo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/limitation/LimitationDemo.java b/src/test/java/limitation/LimitationDemo.java index 1db9f599..f0107567 100644 --- a/src/test/java/limitation/LimitationDemo.java +++ b/src/test/java/limitation/LimitationDemo.java @@ -81,7 +81,7 @@ public class LimitationDemo extends JbootController { /** * 每个IP地址,每5秒钟只能访问一次 */ - @EnableIpRateLimit(rate = 0.2) + @EnableIpRateLimit(rate = 0.2, limitAction = LimitAction.JSON, limitContent = "被限制啦") public void ip() { renderText("ip() render ok"); } -- Gitee From 7dbccdb4a9294afe9573a20a5085f037fd017473 Mon Sep 17 00:00:00 2001 From: shaojava Date: Sun, 4 Feb 2018 12:21:33 +0800 Subject: [PATCH 30/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/spring/AppProviderConfig.java | 31 -------------- src/test/java/spring/ConsumerStarter.java | 37 ++++++++++++++++ src/test/java/spring/Starter.java | 29 +++++++++++++ src/test/resources/spring-context.xml | 15 +++++++ src/test/resources/spring-dubbo-consumer.xml | 33 +++++++++++++++ src/test/resources/spring-dubbo-provider.xml | 44 ++++++++++++++++++++ 6 files changed, 158 insertions(+), 31 deletions(-) delete mode 100644 src/test/java/spring/AppProviderConfig.java create mode 100644 src/test/java/spring/ConsumerStarter.java create mode 100644 src/test/java/spring/Starter.java create mode 100644 src/test/resources/spring-context.xml create mode 100644 src/test/resources/spring-dubbo-consumer.xml create mode 100644 src/test/resources/spring-dubbo-provider.xml diff --git a/src/test/java/spring/AppProviderConfig.java b/src/test/java/spring/AppProviderConfig.java deleted file mode 100644 index c5458e05..00000000 --- a/src/test/java/spring/AppProviderConfig.java +++ /dev/null @@ -1,31 +0,0 @@ -package spring; - -import com.jfinal.config.*; -import com.jfinal.core.JFinal; -import com.jfinal.kit.PropKit; -import com.jfinal.plugin.activerecord.ActiveRecordPlugin; -import com.jfinal.plugin.druid.DruidPlugin; -import com.jfinal.plugin.druid.DruidStatViewHandler; -import com.jfinal.template.Engine; -import io.jboot.aop.jfinal.SpringPlugin; -import io.jboot.web.JbootAppConfig; - -/** - * 本 demo 仅表达最为粗浅的 jfinal 用法,更为有价值的实用的企业级用法 - * 详见 JFinal 俱乐部: http://jfinal.com/club - *

- * API引导式配置 - */ -public class AppProviderConfig extends JbootAppConfig { - - /** - * 配置插件 - */ - @Override - public void configPlugin(Plugins me) { - super.configPlugin(me); - // 启动Spring插件 - me.add(new SpringPlugin()); - } - -} \ No newline at end of file diff --git a/src/test/java/spring/ConsumerStarter.java b/src/test/java/spring/ConsumerStarter.java new file mode 100644 index 00000000..b1622f23 --- /dev/null +++ b/src/test/java/spring/ConsumerStarter.java @@ -0,0 +1,37 @@ +package spring; + +import io.jboot.Jboot; +import io.jboot.aop.jfinal.JfinalPlugins; +import io.jboot.aop.jfinal.SpringPlugin; +import io.jboot.server.listener.JbootAppListenerBase; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.config.IniSecurityManagerFactory; +import org.apache.shiro.mgt.SecurityManager; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + * @Title: (请输入文件名称) + * @Description: (用一句话描述该文件做什么) + * @Package shiro + */ +public class Starter extends JbootAppListenerBase { + + @Override + public void onJFinalStarted() { + IniSecurityManagerFactory factory= new IniSecurityManagerFactory("classpath:shiro.ini"); + SecurityManager securityManager = factory.getInstance(); + SecurityUtils.setSecurityManager(securityManager); + } + + @Override + public void onJfinalPluginConfig(JfinalPlugins plugins) { + // 启动Spring插件 + plugins.add(new SpringPlugin()); + } + + public static void main(String[] args) { + + Jboot.run(args); + } +} diff --git a/src/test/java/spring/Starter.java b/src/test/java/spring/Starter.java new file mode 100644 index 00000000..008e7b83 --- /dev/null +++ b/src/test/java/spring/Starter.java @@ -0,0 +1,29 @@ +package shiro; + +import io.jboot.Jboot; +import io.jboot.server.listener.JbootAppListenerBase; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.config.IniSecurityManagerFactory; +import org.apache.shiro.mgt.SecurityManager; + +/** + * @author Michael Yang 杨福海 (fuhai999@gmail.com) + * @version V1.0 + * @Title: (请输入文件名称) + * @Description: (用一句话描述该文件做什么) + * @Package shiro + */ +public class Starter extends JbootAppListenerBase { + + @Override + public void onJFinalStarted() { + IniSecurityManagerFactory factory= new IniSecurityManagerFactory("classpath:shiro.ini"); + SecurityManager securityManager = factory.getInstance(); + SecurityUtils.setSecurityManager(SecurityUtils.getSecurityManager()); + } + + public static void main(String[] args) { + + Jboot.run(args); + } +} diff --git a/src/test/resources/spring-context.xml b/src/test/resources/spring-context.xml new file mode 100644 index 00000000..0565da72 --- /dev/null +++ b/src/test/resources/spring-context.xml @@ -0,0 +1,15 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/spring-dubbo-consumer.xml b/src/test/resources/spring-dubbo-consumer.xml new file mode 100644 index 00000000..f2fac3aa --- /dev/null +++ b/src/test/resources/spring-dubbo-consumer.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/spring-dubbo-provider.xml b/src/test/resources/spring-dubbo-provider.xml new file mode 100644 index 00000000..c1afea64 --- /dev/null +++ b/src/test/resources/spring-dubbo-provider.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- Gitee From 82e4f16ed2d1ea74e46dfe162bd62199bc86e7d1 Mon Sep 17 00:00:00 2001 From: shaojava Date: Sun, 4 Feb 2018 12:21:58 +0800 Subject: [PATCH 31/31] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 19 ++++++++++++++--- .../java/service/CategoryServiceImpl.java | 2 ++ src/test/java/service/UserService.java | 7 ++++++- src/test/java/service/UserServiceImpl.java | 7 ++++--- src/test/java/shiro/Starter.java | 2 +- src/test/java/spring/ConsumerStarter.java | 7 ++++--- src/test/java/spring/ControllerTest.java | 14 ------------- src/test/java/spring/Starter.java | 12 +++++++++-- src/test/resources/applicationContext.xml | 5 ++++- src/test/resources/spring-context.xml | 17 ++++++++++----- src/test/resources/spring-dubbo-consumer.xml | 15 +++---------- src/test/resources/spring-dubbo-provider.xml | 21 +++---------------- 12 files changed, 65 insertions(+), 63 deletions(-) diff --git a/pom.xml b/pom.xml index eca7765b..6bd88186 100644 --- a/pom.xml +++ b/pom.xml @@ -352,7 +352,7 @@ com.alibaba dubbo - 2.6.0 + 2.8.5-SNAPSHOT org.springframework @@ -373,6 +373,19 @@ + + + org.apache.tomcat.embed + tomcat-embed-core + 8.0.11 + + + + org.apache.tomcat.embed + tomcat-embed-logging-juli + 8.0.11 + + org.jboss.resteasy @@ -412,9 +425,9 @@ - com.101tec + com.alibaba zkclient - 0.10 + 0.8.1 diff --git a/src/test/java/service/CategoryServiceImpl.java b/src/test/java/service/CategoryServiceImpl.java index de0fc985..b826d263 100644 --- a/src/test/java/service/CategoryServiceImpl.java +++ b/src/test/java/service/CategoryServiceImpl.java @@ -18,8 +18,10 @@ package service; import io.jboot.aop.annotation.Bean; import io.jboot.component.hystrix.annotation.EnableHystrixCommand; +import org.springframework.stereotype.Service; @Bean +@Service("dubboService") public class CategoryServiceImpl implements CategoryService { @Override public String hello1(String text) { diff --git a/src/test/java/service/UserService.java b/src/test/java/service/UserService.java index 5550c484..a8cdc168 100644 --- a/src/test/java/service/UserService.java +++ b/src/test/java/service/UserService.java @@ -15,15 +15,20 @@ */ package service; +import javax.ws.rs.Path; +@Path("user") public interface UserService { - + @Path("hello") public String hello(String hello); + @Path("findUserById") public String findUserById(String userId); + @Path("saveUser") public boolean saveUser(User user); + @Path("exception") public String exception(String id); } diff --git a/src/test/java/service/UserServiceImpl.java b/src/test/java/service/UserServiceImpl.java index 5ef85995..851c2cd6 100644 --- a/src/test/java/service/UserServiceImpl.java +++ b/src/test/java/service/UserServiceImpl.java @@ -19,9 +19,12 @@ package service; import io.jboot.Jboot; import io.jboot.component.hystrix.annotation.EnableHystrixCommand; import io.jboot.exception.JbootException; +import org.springframework.stereotype.Service; -public class UserServiceImpl implements UserService { +import javax.ws.rs.Path; +@Service("restService") +public class UserServiceImpl implements UserService { @Override public String hello(String name) { @@ -30,8 +33,6 @@ public class UserServiceImpl implements UserService { return "UserServiceImpl hello invoked!!! "+name; } - - @Override public String findUserById(String userId) { return "get user:" + userId; diff --git a/src/test/java/shiro/Starter.java b/src/test/java/shiro/Starter.java index 008e7b83..6528f213 100644 --- a/src/test/java/shiro/Starter.java +++ b/src/test/java/shiro/Starter.java @@ -19,7 +19,7 @@ public class Starter extends JbootAppListenerBase { public void onJFinalStarted() { IniSecurityManagerFactory factory= new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); - SecurityUtils.setSecurityManager(SecurityUtils.getSecurityManager()); + SecurityUtils.setSecurityManager(securityManager); } public static void main(String[] args) { diff --git a/src/test/java/spring/ConsumerStarter.java b/src/test/java/spring/ConsumerStarter.java index b1622f23..ac6ed4d0 100644 --- a/src/test/java/spring/ConsumerStarter.java +++ b/src/test/java/spring/ConsumerStarter.java @@ -15,7 +15,7 @@ import org.apache.shiro.mgt.SecurityManager; * @Description: (用一句话描述该文件做什么) * @Package shiro */ -public class Starter extends JbootAppListenerBase { +public class ConsumerStarter extends JbootAppListenerBase { @Override public void onJFinalStarted() { @@ -27,11 +27,12 @@ public class Starter extends JbootAppListenerBase { @Override public void onJfinalPluginConfig(JfinalPlugins plugins) { // 启动Spring插件 - plugins.add(new SpringPlugin()); + //plugins.add(new SpringPlugin("classpath:spring-context.xml")); } public static void main(String[] args) { - + //jboot端口号配置 + Jboot.setBootArg("jboot.server.port", "8088"); Jboot.run(args); } } diff --git a/src/test/java/spring/ControllerTest.java b/src/test/java/spring/ControllerTest.java index b635b2a4..865cc055 100644 --- a/src/test/java/spring/ControllerTest.java +++ b/src/test/java/spring/ControllerTest.java @@ -18,20 +18,6 @@ public class ControllerTest extends JbootController { @Autowired private CodeConfig codeConfig; - public static void main(String[] args) { - -// Jboot.setBootArg("jboot.hystrix.url", "/hystrix.stream"); -// Jboot.setBootArg("jboot.cache.type", "redis"); -// Jboot.setBootArg("jboot.metrics.url", "/metrics.abc"); -// Jboot.setBootArg("jboot.cache.redis.host", "127.0.0.1"); - - String name = AppProviderConfig.class.getName(); - - Jboot.setBootArg("jboot.jfinalConfig",name); - Jboot.run(args); - - } - public void index(){ String codeError = codeConfig.getCodeError(); renderText("hello world."+codeError); diff --git a/src/test/java/spring/Starter.java b/src/test/java/spring/Starter.java index 008e7b83..b1622f23 100644 --- a/src/test/java/spring/Starter.java +++ b/src/test/java/spring/Starter.java @@ -1,6 +1,8 @@ -package shiro; +package spring; import io.jboot.Jboot; +import io.jboot.aop.jfinal.JfinalPlugins; +import io.jboot.aop.jfinal.SpringPlugin; import io.jboot.server.listener.JbootAppListenerBase; import org.apache.shiro.SecurityUtils; import org.apache.shiro.config.IniSecurityManagerFactory; @@ -19,7 +21,13 @@ public class Starter extends JbootAppListenerBase { public void onJFinalStarted() { IniSecurityManagerFactory factory= new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); - SecurityUtils.setSecurityManager(SecurityUtils.getSecurityManager()); + SecurityUtils.setSecurityManager(securityManager); + } + + @Override + public void onJfinalPluginConfig(JfinalPlugins plugins) { + // 启动Spring插件 + plugins.add(new SpringPlugin()); } public static void main(String[] args) { diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml index 8d3cd0b3..e58d1a60 100644 --- a/src/test/resources/applicationContext.xml +++ b/src/test/resources/applicationContext.xml @@ -9,11 +9,14 @@ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> - + + + + \ No newline at end of file diff --git a/src/test/resources/spring-context.xml b/src/test/resources/spring-context.xml index 0565da72..b5427e07 100644 --- a/src/test/resources/spring-context.xml +++ b/src/test/resources/spring-context.xml @@ -1,14 +1,21 @@ + http://www.springframework.org/schema/beans/spring-beans-3.1.xsd + http://www.springframework.org/schema/aop + http://www.springframework.org/schema/aop/spring-aop-3.0.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd"> - + + + + + + diff --git a/src/test/resources/spring-dubbo-consumer.xml b/src/test/resources/spring-dubbo-consumer.xml index f2fac3aa..82689a0d 100644 --- a/src/test/resources/spring-dubbo-consumer.xml +++ b/src/test/resources/spring-dubbo-consumer.xml @@ -7,27 +7,18 @@ http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> - + - - - - - - - - \ No newline at end of file diff --git a/src/test/resources/spring-dubbo-provider.xml b/src/test/resources/spring-dubbo-provider.xml index c1afea64..a3552b46 100644 --- a/src/test/resources/spring-dubbo-provider.xml +++ b/src/test/resources/spring-dubbo-provider.xml @@ -7,7 +7,7 @@ http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> - + @@ -20,25 +20,10 @@ - - - - - - - - - - - - - \ No newline at end of file -- Gitee