From 94bb8fc1b6430c1a8bf3c76a46a60b363211be86 Mon Sep 17 00:00:00 2001 From: javahongxi Date: Fri, 30 Jun 2017 00:37:24 +0800 Subject: [PATCH] motan demo --- .../com/weibo/motan/demo/service/App.java | 5 +++ .../motan/demo/service/DemoRpcServer.java | 33 +++++++++++++++ .../com/itlong/whatsmars/spring/boot/App.java | 8 +++- .../boot/controller/SampleController.java | 9 ++-- .../src/main/resources/spring/motan.xml | 32 ++++++++++++++ .../resources/spring/motan_demo_client.xml | 7 +--- .../spring/motan_demo_client_direct.xml | 42 ------------------- ...o_server.xml => motan_demo_server.xml.bak} | 11 ++--- 8 files changed, 86 insertions(+), 61 deletions(-) create mode 100644 whatsmars-motan/src/main/java/com/weibo/motan/demo/service/DemoRpcServer.java create mode 100644 whatsmars-spring-boot/src/main/resources/spring/motan.xml delete mode 100644 whatsmars-spring-boot/src/main/resources/spring/motan_demo_client_direct.xml rename whatsmars-spring-boot/src/main/resources/spring/{motan_demo_server.xml => motan_demo_server.xml.bak} (80%) diff --git a/whatsmars-motan/src/main/java/com/weibo/motan/demo/service/App.java b/whatsmars-motan/src/main/java/com/weibo/motan/demo/service/App.java index 87495a47..ac6e8b9e 100644 --- a/whatsmars-motan/src/main/java/com/weibo/motan/demo/service/App.java +++ b/whatsmars-motan/src/main/java/com/weibo/motan/demo/service/App.java @@ -1,5 +1,7 @@ package com.weibo.motan.demo.service; +import com.weibo.api.motan.common.MotanConstants; +import com.weibo.api.motan.util.MotanSwitcherUtil; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -12,6 +14,9 @@ public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); + + MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); + System.out.println("server start..."); } } \ No newline at end of file diff --git a/whatsmars-motan/src/main/java/com/weibo/motan/demo/service/DemoRpcServer.java b/whatsmars-motan/src/main/java/com/weibo/motan/demo/service/DemoRpcServer.java new file mode 100644 index 00000000..b834a728 --- /dev/null +++ b/whatsmars-motan/src/main/java/com/weibo/motan/demo/service/DemoRpcServer.java @@ -0,0 +1,33 @@ +/* + * Copyright 2009-2016 Weibo, Inc. + * + * 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 com.weibo.motan.demo.service; + +import com.weibo.api.motan.common.MotanConstants; +import com.weibo.api.motan.util.MotanSwitcherUtil; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class DemoRpcServer { + + public static void main(String[] args) throws InterruptedException { + ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] {"classpath*:spring/motan_demo_server.xml"}); + MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); + System.out.println("server start..."); + + } + +} diff --git a/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/App.java b/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/App.java index 99e76550..582bdefb 100644 --- a/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/App.java +++ b/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/App.java @@ -1,6 +1,8 @@ package com.itlong.whatsmars.spring.boot; import com.itlong.whatsmars.spring.boot.config.UserConfig; +import com.weibo.api.motan.common.MotanConstants; +import com.weibo.api.motan.util.MotanSwitcherUtil; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -11,11 +13,15 @@ import org.springframework.context.annotation.ImportResource; // 加上exclude=DataSource..后,不会连接数据库,同时@Mapper也不会起作用 @EnableAutoConfiguration//(exclude={DataSourceAutoConfiguration.class}) @EnableConfigurationProperties({UserConfig.class}) -//@ImportResource(locations={"classpath*:spring/*server.xml"}) +@ImportResource(locations={"classpath*:spring/*.xml"}) public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); + + // motan + MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); + System.out.println("server start..."); } } \ No newline at end of file diff --git a/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/controller/SampleController.java b/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/controller/SampleController.java index 78ebdbaf..787c4cb6 100644 --- a/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/controller/SampleController.java +++ b/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/controller/SampleController.java @@ -2,6 +2,7 @@ package com.itlong.whatsmars.spring.boot.controller; import com.itlong.whatsmars.spring.boot.config.UserConfig; import com.itlong.whatsmars.spring.boot.common.LocaleService; +import com.weibo.motan.demo.service.MotanDemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -30,8 +31,8 @@ public class SampleController { @Autowired private LocaleService localeService; -// @Resource(name = "motanDemoService") -// private MotanDemoService motanDemoService; + @Resource(name = "motanDemoService") + private MotanDemoService motanDemoService; /** * 设置区域解析器 (default is AcceptHeaderLocaleResolver) @@ -65,9 +66,7 @@ public class SampleController { @RequestMapping("/motan") @ResponseBody public String motan() { - ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:spring/motan_demo_client.xml"}); - com.weibo.motan.demo.service.MotanDemoService service = (com.weibo.motan.demo.service.MotanDemoService) ctx.getBean("motanDemoReferer"); - return userConfig.getWelcome() + service.hello("motan"); + return userConfig.getWelcome() + motanDemoService.hello("motan"); } } diff --git a/whatsmars-spring-boot/src/main/resources/spring/motan.xml b/whatsmars-spring-boot/src/main/resources/spring/motan.xml new file mode 100644 index 00000000..e8677a45 --- /dev/null +++ b/whatsmars-spring-boot/src/main/resources/spring/motan.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + diff --git a/whatsmars-spring-boot/src/main/resources/spring/motan_demo_client.xml b/whatsmars-spring-boot/src/main/resources/spring/motan_demo_client.xml index 307572d9..0a9c0f35 100644 --- a/whatsmars-spring-boot/src/main/resources/spring/motan_demo_client.xml +++ b/whatsmars-spring-boot/src/main/resources/spring/motan_demo_client.xml @@ -20,10 +20,7 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd"> - - - - + - diff --git a/whatsmars-spring-boot/src/main/resources/spring/motan_demo_client_direct.xml b/whatsmars-spring-boot/src/main/resources/spring/motan_demo_client_direct.xml deleted file mode 100644 index 4963a3f0..00000000 --- a/whatsmars-spring-boot/src/main/resources/spring/motan_demo_client_direct.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/whatsmars-spring-boot/src/main/resources/spring/motan_demo_server.xml b/whatsmars-spring-boot/src/main/resources/spring/motan_demo_server.xml.bak similarity index 80% rename from whatsmars-spring-boot/src/main/resources/spring/motan_demo_server.xml rename to whatsmars-spring-boot/src/main/resources/spring/motan_demo_server.xml.bak index 46a76fb4..cbb76d42 100644 --- a/whatsmars-spring-boot/src/main/resources/spring/motan_demo_server.xml +++ b/whatsmars-spring-boot/src/main/resources/spring/motan_demo_server.xml.bak @@ -24,27 +24,22 @@ - - - - - - + ref="motanDemoServiceImpl" export="demoMotan:8003" basicService="serviceBasicConfig"> + ref="motanDemoServiceImpl" export="demoMotan:8004" basicService="serviceBasicConfig"> -- GitLab