diff --git a/pom.xml b/pom.xml index 1be34e0354bc522172c52882860cea4870d23e57..90446e4a4c5b48d25981f5470da2650447757de3 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 @@ -340,7 +359,7 @@ com.alibaba dubbo - 2.6.0 + 2.8.5-SNAPSHOT org.springframework @@ -361,6 +380,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 @@ -400,9 +432,9 @@ - com.101tec + com.alibaba zkclient - 0.10 + 0.8.1 @@ -489,7 +521,12 @@ - + + + com.github.davidb + metrics-influxdb + 0.9.3 + 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 0000000000000000000000000000000000000000..4c98d0220ff757ebd9eee6124d5a87ae115c7756 --- /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/component/hystrix/JbootHystrixCommand.java b/src/main/java/io/jboot/component/hystrix/JbootHystrixCommand.java index 6467ff34063feae1643d54724ead545bfc711a10..960fcf70acd54f30bb6416eaa5e8da61e92df843 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/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java b/src/main/java/io/jboot/component/metric/reporter/influxdb/InfluxdbReporter.java index b9777fc70aa2ea19a40466fa060b28fc0c48d416..6950ea0c55f6ecba07cc3bc22d3f9199dec0e020 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,50 @@ */ 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; +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.metric.reporter.jmx + * @author shaojava 邵文长 (shaojava@sina.com) + * @version V1.1 + * @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."); + } + + 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/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 0000000000000000000000000000000000000000..3ac0e0447b526fc61db0299b30e9677d30705b4c --- /dev/null +++ b/src/main/java/io/jboot/component/metric/reporter/influxdb/JbootMetricsInfluxdbReporterConfig.java @@ -0,0 +1,77 @@ +/** + * 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; + + +/** + * @author shaojava 邵文长 (shaojava@sina.com) + * @version V1.1 + * @Package io.jboot.component.metrics.reporter.influxdb + */ +@PropertyConfig(prefix = "jboot.metric.reporter.influxdb") +public class JbootMetricsInfluxdbReporterConfig { + + private String host; + private Integer port = 8086; + 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; + } +} + + + diff --git a/src/main/java/io/jboot/config/server/JbootConfigController.java b/src/main/java/io/jboot/config/server/JbootConfigController.java index 6a2636e573e0fbb4fc2222448b76d62eae7d1600..d5726f1a7ddff1cdfbd8bd92120b5b2a010dbf4e 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 { 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 d81edf4ad9fbdd4d56092c1002e72a915c5874a5..4047e66c78742933b00c1b87b26fd120a8f2a432 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 mode = JbootZbusmqImpl.MODE.PRODUCER.getValue(); public String getQueue() { return queue; @@ -40,4 +41,12 @@ public class JbootZbusmqConfig { public void setBroker(String broker) { this.broker = broker; } + + public String getMode() { + return mode; + } + + 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 38a9438fe738b11d423460b5faf432c348d469b3..71853b7a5427ae44f595d966c51be7b6f502fb6d 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,44 +31,48 @@ 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) { - ConsumerConfig config = new ConsumerConfig(broker); - config.setTopic(channel); - config.setMessageHandler(this); - ConsumeGroup group = ConsumeGroup.createTempBroadcastGroup(); - config.setConsumeGroup(group); - 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 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[] 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(); + } } } } @@ -117,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/main/java/io/jboot/web/JbootControllerManager.java b/src/main/java/io/jboot/web/JbootControllerManager.java index f6033d0ad1b719a734ba223aff41ee0aeb7933e0..64d8823323f37bc139e15ace6ab0112756ee11ab 100644 --- a/src/main/java/io/jboot/web/JbootControllerManager.java +++ b/src/main/java/io/jboot/web/JbootControllerManager.java @@ -20,7 +20,11 @@ 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; import java.util.HashMap; import java.util.Map; @@ -46,17 +50,27 @@ public class JbootControllerManager extends ControllerFactory { } }; + /*public Controller getController(Class extends Controller> 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 extends Controller> 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 extends Controller> getControllerByPath(String path) { @@ -71,5 +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/ControllerTest.java b/src/test/java/ControllerTest.java index 607f41c620cc2bfbd4a858510d93025cd58317e0..22557a7b5f1e93b0d40d59a41de48ba1a61f992f 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/HttpTest.java b/src/test/java/HttpTest.java index a84c2bb9a0b31f03550cf355a50c16c996d7a0f8..80c3cbecdbd49e0eee38ef8042382b3a1e59ee9e 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/MetricsTest.java b/src/test/java/MetricsTest.java index a503ca78bacad947ea91f98a2558c6469788a897..6679dded1486814813dd0d41a3c7adf7ddf0cf05 100644 --- a/src/test/java/MetricsTest.java +++ b/src/test/java/MetricsTest.java @@ -1,16 +1,81 @@ +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.metrics.url", "/metrics.html/*"); - Jboot.setBootArg("jboot.metrics.jmxReporter", true); + 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.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"); + Jboot.setBootArg("jboot.metric.reporter.influxdb.database", "jmeter"); + 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。 } diff --git a/src/test/java/RedisTest.java b/src/test/java/RedisTest.java index 55f260d624af3dc1d76829f72f6839e8173c6c32..8fdce6870124abe12b08fd72c29c9c7a6b117bc1 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,32 +11,71 @@ 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", "192.168.19.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)); + 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); } diff --git a/src/test/java/aop/AopDemo.java b/src/test/java/aop/AopDemo.java index 5dbf1f7353ba01389494d5be13c0dfa5f39dcb56..cb035b45540761568fc6154fea99814beb030e6c 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 ba842f16971b20243edcd13001e5ad352f052313..0aaebc7144a1b8ffd02ac1df54083eb47f674f5b 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 4d5e6c3a16b549992c29ff7eff04fbabe2dd3bca..7147c8bf5656ec055ad144577843764e9eddb84b 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/limitation/LimitationDemo.java b/src/test/java/limitation/LimitationDemo.java index 7a76f56fb4bf2a693a82b6468fa6b198952b840e..68c7e7845dfd85d463a098f7cfc3827e1d7a52ec 100644 --- a/src/test/java/limitation/LimitationDemo.java +++ b/src/test/java/limitation/LimitationDemo.java @@ -96,7 +96,7 @@ public class LimitationDemo extends JbootController { /** * 每个IP地址,每5秒钟只能访问一次 */ - @EnablePerIpLimit(rate = 0.2) + @EnableIpRateLimit(rate = 0.2) public void ip() { renderText("ip() render ok"); } diff --git a/src/test/java/mqzbus/ZbusMqClientDemo.java b/src/test/java/mqzbus/ZbusMqClientDemo.java index 6a260b0cf8029bbd400f997d84afc9dd5384860c..e7ee4d2470c169c39fd44f5e720dbf0e8453c785 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 d99d1c46b696dda6da3bfab0b06c78ee3c75788b..e0e011a689620799682837ecd8d3fa5ef5e0655a 100644 --- a/src/test/java/mqzbus/ZbusMqServerDemo.java +++ b/src/test/java/mqzbus/ZbusMqServerDemo.java @@ -16,30 +16,51 @@ 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.mode", "producer"); Jboot.setBootArg("jboot.mq.zbus.broker", "127.0.0.1:15555"); + Jboot.setBootArg("jboot.mq.channel", "CpMQ,RefreshCpMQ"); - 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); + } + public void ref() { + String msg = "message "+(count++); + Jboot.me().getMq().publish(msg, "RefreshCpMQ"); + renderText(msg); } } diff --git a/src/test/java/opentracing/ClientDemo.java b/src/test/java/opentracing/ClientDemo.java index b1a455f74db0e9422265b81c7ddf688a42d12a14..f7571582fad5000a40d1402ecea259e1d8b01f6b 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 07363bbd248eb578e4939638fa58b6d5c1c38b4c..4255d6d75949a069701294c4f1702cb9f2818423 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 ce3fe872d558c1b27c4023a886d6c5d2cd485012..b826d2632099ff958bf5df0d692f3f6c28462a7d 100644 --- a/src/test/java/service/CategoryServiceImpl.java +++ b/src/test/java/service/CategoryServiceImpl.java @@ -17,11 +17,14 @@ 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 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 45f84d536f6a0a4a50263b846a81ad80faf9e641..27aa228e06222402d9859d346953f0dbcfa1a928 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/UserService.java b/src/test/java/service/UserService.java index 5550c4844d256c1b9c16815a800eaab8d1b232f1..a8cdc168dbb0533bba26ce2d9f33e93ad11f74f6 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 f50b13a4db3f8164e052f61478c1ab793c64623e..851c2cd65de97be0ca489ecfca9f0937a15d2862 100644 --- a/src/test/java/service/UserServiceImpl.java +++ b/src/test/java/service/UserServiceImpl.java @@ -17,21 +17,22 @@ 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) { System.out.println("UserServiceImpl hello invoked!!!"); - - return Jboot.service(CategoryService.class).hello(name); + return "UserServiceImpl hello invoked!!! "+name; } - - @Override public String findUserById(String userId) { return "get user:" + userId; diff --git a/src/test/java/spring/CodeConfig.java b/src/test/java/spring/CodeConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..6663277ebb11d6d01a5266a5304ca1b9b00a9ed6 --- /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/ConsumerStarter.java b/src/test/java/spring/ConsumerStarter.java new file mode 100644 index 0000000000000000000000000000000000000000..ac6ed4d0c2c419614619fca16328e3b1bc3db6e0 --- /dev/null +++ b/src/test/java/spring/ConsumerStarter.java @@ -0,0 +1,38 @@ +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 ConsumerStarter 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("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 new file mode 100644 index 0000000000000000000000000000000000000000..865cc055443188f16023dc5b1b25e83ba6902a1a --- /dev/null +++ b/src/test/java/spring/ControllerTest.java @@ -0,0 +1,25 @@ +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 void index(){ + String codeError = codeConfig.getCodeError(); + renderText("hello world."+codeError); + } +} \ No newline at end of file diff --git a/src/test/java/spring/Starter.java b/src/test/java/spring/Starter.java new file mode 100644 index 0000000000000000000000000000000000000000..b1622f239d58917003b2d41b2f10c22973e44399 --- /dev/null +++ b/src/test/java/spring/Starter.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/swagger/SwaggerStarter.java b/src/test/java/swagger/SwaggerStarter.java index 01b743c1a9ab6ea0347b04fecc6d24e2edba9cfd..b99a750617addba87c3880a5a6b53093a11ec3d4 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); diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml new file mode 100644 index 0000000000000000000000000000000000000000..e58d1a60f6077876745cad73459bcaeb62182f0a --- /dev/null +++ b/src/test/resources/applicationContext.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/spring-context.xml b/src/test/resources/spring-context.xml new file mode 100644 index 0000000000000000000000000000000000000000..b5427e07e54a4bc6141d77b33dd3c9901cca2582 --- /dev/null +++ b/src/test/resources/spring-context.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ 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 0000000000000000000000000000000000000000..82689a0d4a31b1273dd632d6218c1688943149a2 --- /dev/null +++ b/src/test/resources/spring-dubbo-consumer.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + \ 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 0000000000000000000000000000000000000000..a3552b4618c5b095b5ce1cd5efcc34468a8b8d5f --- /dev/null +++ b/src/test/resources/spring-dubbo-provider.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file
+ * 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; + + +/** + * @author shaojava 邵文长 (shaojava@sina.com) + * @version V1.1 + * @Package io.jboot.component.metrics.reporter.influxdb + */ +@PropertyConfig(prefix = "jboot.metric.reporter.influxdb") +public class JbootMetricsInfluxdbReporterConfig { + + private String host; + private Integer port = 8086; + 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; + } +} + + + diff --git a/src/main/java/io/jboot/config/server/JbootConfigController.java b/src/main/java/io/jboot/config/server/JbootConfigController.java index 6a2636e573e0fbb4fc2222448b76d62eae7d1600..d5726f1a7ddff1cdfbd8bd92120b5b2a010dbf4e 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 { 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 d81edf4ad9fbdd4d56092c1002e72a915c5874a5..4047e66c78742933b00c1b87b26fd120a8f2a432 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 mode = JbootZbusmqImpl.MODE.PRODUCER.getValue(); public String getQueue() { return queue; @@ -40,4 +41,12 @@ public class JbootZbusmqConfig { public void setBroker(String broker) { this.broker = broker; } + + public String getMode() { + return mode; + } + + 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 38a9438fe738b11d423460b5faf432c348d469b3..71853b7a5427ae44f595d966c51be7b6f502fb6d 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,44 +31,48 @@ 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) { - ConsumerConfig config = new ConsumerConfig(broker); - config.setTopic(channel); - config.setMessageHandler(this); - ConsumeGroup group = ConsumeGroup.createTempBroadcastGroup(); - config.setConsumeGroup(group); - 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 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[] 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(); + } } } } @@ -117,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/main/java/io/jboot/web/JbootControllerManager.java b/src/main/java/io/jboot/web/JbootControllerManager.java index f6033d0ad1b719a734ba223aff41ee0aeb7933e0..64d8823323f37bc139e15ace6ab0112756ee11ab 100644 --- a/src/main/java/io/jboot/web/JbootControllerManager.java +++ b/src/main/java/io/jboot/web/JbootControllerManager.java @@ -20,7 +20,11 @@ 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; import java.util.HashMap; import java.util.Map; @@ -46,17 +50,27 @@ public class JbootControllerManager extends ControllerFactory { } }; + /*public Controller getController(Class extends Controller> 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 extends Controller> 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 extends Controller> getControllerByPath(String path) { @@ -71,5 +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/ControllerTest.java b/src/test/java/ControllerTest.java index 607f41c620cc2bfbd4a858510d93025cd58317e0..22557a7b5f1e93b0d40d59a41de48ba1a61f992f 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/HttpTest.java b/src/test/java/HttpTest.java index a84c2bb9a0b31f03550cf355a50c16c996d7a0f8..80c3cbecdbd49e0eee38ef8042382b3a1e59ee9e 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/MetricsTest.java b/src/test/java/MetricsTest.java index a503ca78bacad947ea91f98a2558c6469788a897..6679dded1486814813dd0d41a3c7adf7ddf0cf05 100644 --- a/src/test/java/MetricsTest.java +++ b/src/test/java/MetricsTest.java @@ -1,16 +1,81 @@ +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.metrics.url", "/metrics.html/*"); - Jboot.setBootArg("jboot.metrics.jmxReporter", true); + 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.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"); + Jboot.setBootArg("jboot.metric.reporter.influxdb.database", "jmeter"); + 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。 } diff --git a/src/test/java/RedisTest.java b/src/test/java/RedisTest.java index 55f260d624af3dc1d76829f72f6839e8173c6c32..8fdce6870124abe12b08fd72c29c9c7a6b117bc1 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,32 +11,71 @@ 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", "192.168.19.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)); + 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); } diff --git a/src/test/java/aop/AopDemo.java b/src/test/java/aop/AopDemo.java index 5dbf1f7353ba01389494d5be13c0dfa5f39dcb56..cb035b45540761568fc6154fea99814beb030e6c 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 ba842f16971b20243edcd13001e5ad352f052313..0aaebc7144a1b8ffd02ac1df54083eb47f674f5b 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 4d5e6c3a16b549992c29ff7eff04fbabe2dd3bca..7147c8bf5656ec055ad144577843764e9eddb84b 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/limitation/LimitationDemo.java b/src/test/java/limitation/LimitationDemo.java index 7a76f56fb4bf2a693a82b6468fa6b198952b840e..68c7e7845dfd85d463a098f7cfc3827e1d7a52ec 100644 --- a/src/test/java/limitation/LimitationDemo.java +++ b/src/test/java/limitation/LimitationDemo.java @@ -96,7 +96,7 @@ public class LimitationDemo extends JbootController { /** * 每个IP地址,每5秒钟只能访问一次 */ - @EnablePerIpLimit(rate = 0.2) + @EnableIpRateLimit(rate = 0.2) public void ip() { renderText("ip() render ok"); } diff --git a/src/test/java/mqzbus/ZbusMqClientDemo.java b/src/test/java/mqzbus/ZbusMqClientDemo.java index 6a260b0cf8029bbd400f997d84afc9dd5384860c..e7ee4d2470c169c39fd44f5e720dbf0e8453c785 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 d99d1c46b696dda6da3bfab0b06c78ee3c75788b..e0e011a689620799682837ecd8d3fa5ef5e0655a 100644 --- a/src/test/java/mqzbus/ZbusMqServerDemo.java +++ b/src/test/java/mqzbus/ZbusMqServerDemo.java @@ -16,30 +16,51 @@ 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.mode", "producer"); Jboot.setBootArg("jboot.mq.zbus.broker", "127.0.0.1:15555"); + Jboot.setBootArg("jboot.mq.channel", "CpMQ,RefreshCpMQ"); - 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); + } + public void ref() { + String msg = "message "+(count++); + Jboot.me().getMq().publish(msg, "RefreshCpMQ"); + renderText(msg); } } diff --git a/src/test/java/opentracing/ClientDemo.java b/src/test/java/opentracing/ClientDemo.java index b1a455f74db0e9422265b81c7ddf688a42d12a14..f7571582fad5000a40d1402ecea259e1d8b01f6b 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 07363bbd248eb578e4939638fa58b6d5c1c38b4c..4255d6d75949a069701294c4f1702cb9f2818423 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 ce3fe872d558c1b27c4023a886d6c5d2cd485012..b826d2632099ff958bf5df0d692f3f6c28462a7d 100644 --- a/src/test/java/service/CategoryServiceImpl.java +++ b/src/test/java/service/CategoryServiceImpl.java @@ -17,11 +17,14 @@ 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 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 45f84d536f6a0a4a50263b846a81ad80faf9e641..27aa228e06222402d9859d346953f0dbcfa1a928 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/UserService.java b/src/test/java/service/UserService.java index 5550c4844d256c1b9c16815a800eaab8d1b232f1..a8cdc168dbb0533bba26ce2d9f33e93ad11f74f6 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 f50b13a4db3f8164e052f61478c1ab793c64623e..851c2cd65de97be0ca489ecfca9f0937a15d2862 100644 --- a/src/test/java/service/UserServiceImpl.java +++ b/src/test/java/service/UserServiceImpl.java @@ -17,21 +17,22 @@ 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) { System.out.println("UserServiceImpl hello invoked!!!"); - - return Jboot.service(CategoryService.class).hello(name); + return "UserServiceImpl hello invoked!!! "+name; } - - @Override public String findUserById(String userId) { return "get user:" + userId; diff --git a/src/test/java/spring/CodeConfig.java b/src/test/java/spring/CodeConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..6663277ebb11d6d01a5266a5304ca1b9b00a9ed6 --- /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/ConsumerStarter.java b/src/test/java/spring/ConsumerStarter.java new file mode 100644 index 0000000000000000000000000000000000000000..ac6ed4d0c2c419614619fca16328e3b1bc3db6e0 --- /dev/null +++ b/src/test/java/spring/ConsumerStarter.java @@ -0,0 +1,38 @@ +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 ConsumerStarter 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("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 new file mode 100644 index 0000000000000000000000000000000000000000..865cc055443188f16023dc5b1b25e83ba6902a1a --- /dev/null +++ b/src/test/java/spring/ControllerTest.java @@ -0,0 +1,25 @@ +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 void index(){ + String codeError = codeConfig.getCodeError(); + renderText("hello world."+codeError); + } +} \ No newline at end of file diff --git a/src/test/java/spring/Starter.java b/src/test/java/spring/Starter.java new file mode 100644 index 0000000000000000000000000000000000000000..b1622f239d58917003b2d41b2f10c22973e44399 --- /dev/null +++ b/src/test/java/spring/Starter.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/swagger/SwaggerStarter.java b/src/test/java/swagger/SwaggerStarter.java index 01b743c1a9ab6ea0347b04fecc6d24e2edba9cfd..b99a750617addba87c3880a5a6b53093a11ec3d4 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); diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml new file mode 100644 index 0000000000000000000000000000000000000000..e58d1a60f6077876745cad73459bcaeb62182f0a --- /dev/null +++ b/src/test/resources/applicationContext.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/spring-context.xml b/src/test/resources/spring-context.xml new file mode 100644 index 0000000000000000000000000000000000000000..b5427e07e54a4bc6141d77b33dd3c9901cca2582 --- /dev/null +++ b/src/test/resources/spring-context.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ 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 0000000000000000000000000000000000000000..82689a0d4a31b1273dd632d6218c1688943149a2 --- /dev/null +++ b/src/test/resources/spring-dubbo-consumer.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + \ 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 0000000000000000000000000000000000000000..a3552b4618c5b095b5ce1cd5efcc34468a8b8d5f --- /dev/null +++ b/src/test/resources/spring-dubbo-provider.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file