簡(jiǎn)介
Hello World
- 使用的過(guò)程分為Server端和Client端金闽,Server提供RCP的服務(wù)接口籍凝,Client端發(fā)起調(diào)用獲取結(jié)果排监。
- maven的pom文件配置
<properties>
<motan.version>0.2.1</motan.version>
</properties>
<dependencies>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-core</artifactId>
<version>${motan.version}</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-transport-netty</artifactId>
<version>${motan.version}</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-springsupport</artifactId>
<version>${motan.version}</version>
</dependency>
</dependencies>
Server 端
package com.raventech.user.motan;
/**
* @author liuhaiming on 10/11/2016.
*/
public interface HelloService {
String hello(String world);
}
package com.raventech.user.motan;
/**
* @author liuhaiming on 10/11/2016.
*/
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String world) {
return "hello " + world;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:motan="http://api.weibo.com/schema/motan"
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">
<bean id="helloServiceImpl" class="com.raventech.user.motan.HelloServiceImpl"/>
<motan:service interface="com.raventech.user.motan.HelloService" ref="helloServiceImpl" export="8002"/>
</beans>
- 啟動(dòng)服務(wù)的方法(運(yùn)行main方法就可以啟動(dòng)服務(wù)了)
package com.raventech.user;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author liuhaiming on 10/11/2016.
*/
public class Server {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:motan-server.xml");
System.out.println("Server start ...");
}
}
Client 端
- 要請(qǐng)求的接口(不論包名還是類(lèi)名都要和Server端的一樣)
package com.raventech.user.motan;
/**
* @author liuhaiming on 10/11/2016.
*/
public interface HelloService {
String hello(String world);
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:motan="http://api.weibo.com/schema/motan"
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">
<motan:referer id="helloService" interface="com.raventech.user.motan.HelloService" directUrl="127.0.0.1:8002"/>
</beans>
- 調(diào)用服務(wù)的方法(運(yùn)行main方法就可以調(diào)用服務(wù)了)
package com.raventech.web;
import com.raventech.user.motan.HelloService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author liuhaiming on 10/11/2016.
*/
public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan-client.xml");
HelloService fooService = (HelloService) applicationContext.getBean("helloService");
System.out.println(fooService.hello("world"));
}
}
使用Consul作為注冊(cè)中心
- 在集群環(huán)境下使用motan需要依賴Consul等服務(wù)發(fā)現(xiàn)組件
- Consul的介紹安裝和使用
- maven的pom文件配置(在上面的基礎(chǔ)上增加consul)
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-registry-consul</artifactId>
<version>${motan.version}</version>
</dependency>
Server 端
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:motan="http://api.weibo.com/schema/motan"
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">
<motan:registry regProtocol="consul" name="registry" address="127.0.0.1:8500"/>
<bean id="helloServiceImpl" class="com.raventech.user.motan.HelloServiceImpl"/>
<motan:service interface="com.raventech.user.motan.HelloService" ref="helloServiceImpl" registry="registry" export="8002"/>
</beans>
- 啟動(dòng)服務(wù)的方法要在程序啟動(dòng)后調(diào)用心跳開(kāi)關(guān)迫卢,將服務(wù)注冊(cè)到consul,不然Client無(wú)法調(diào)用 (別的和上文Hello World一樣不變邓梅,運(yùn)行main方法啟動(dòng)服務(wù))
package com.raventech.user;
import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.util.MotanSwitcherUtil;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author liuhaiming on 10/11/2016.
*/
public class Server {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:motan-server.xml");
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
System.out.println("Server start ...");
}
}
Client 端
- xml配置文件添加consul的服務(wù)發(fā)現(xiàn)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:motan="http://api.weibo.com/schema/motan"
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">
<motan:registry regProtocol="consul" name="registry" address="127.0.0.1:8500"/>
<motan:referer id="helloService" interface="com.raventech.user.motan.HelloService" registry="registry"/>
</beans>
- 別的和上文Hello World一樣不變脱盲,運(yùn)行Client類(lèi)的main方法調(diào)用服務(wù)
使用注解的方式集成到Spring Boot項(xiàng)目中
- 項(xiàng)目改成Spring Boot后拋棄了繁瑣的xml文件配置改為用注解的方式。motan也支持注解的方式進(jìn)行配置日缨,這樣更加方便了代碼的集成和風(fēng)格的統(tǒng)一宾毒。
- 繼續(xù)在前面的代碼中進(jìn)行修改,沒(méi)提到的保持不變
Server 端
- 刪除motan-server.xml配置文件
- 用注解加載motan需要的配置
package com.raventech.user.config;
import com.weibo.api.motan.config.springsupport.AnnotationBean;
import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;
import com.weibo.api.motan.config.springsupport.ProtocolConfigBean;
import com.weibo.api.motan.config.springsupport.RegistryConfigBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author liuhaiming on 10/11/2016.
*/
@Configuration
public class MotanConfiguration {
@Bean
public AnnotationBean motanAnnotationBean() {
AnnotationBean motanAnnotationBean = new AnnotationBean();
motanAnnotationBean.setPackage("com.raventech.user.motan");
return motanAnnotationBean;
}
@Bean(name = "motan")
public ProtocolConfigBean protocolConfig1() {
ProtocolConfigBean config = new ProtocolConfigBean();
config.setDefault(true);
config.setName("motan");
config.setMaxContentLength(1048576);
return config;
}
@Bean(name = "registry")
public RegistryConfigBean registryConfig() {
RegistryConfigBean config = new RegistryConfigBean();
config.setRegProtocol("consul");
config.setAddress("127.0.0.1:8500");
return config;
}
@Bean
public BasicServiceConfigBean baseServiceConfig() {
BasicServiceConfigBean config = new BasicServiceConfigBean();
config.setExport("motan:8002");
config.setRegistry("registry");
return config;
}
}
- 暴露接口的實(shí)現(xiàn)類(lèi)加上@MotanService注解殿遂,自動(dòng)生成bean
package com.raventech.user.motan;
import com.weibo.api.motan.config.springsupport.annotation.MotanService;
/**
* @author liuhaiming on 10/11/2016.
*/
@MotanService
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String world) {
return "hello " + world;
}
}
- 啟動(dòng)服務(wù)的方法就是啟動(dòng)Spring Boot項(xiàng)目,并在在程序啟動(dòng)后調(diào)用心跳開(kāi)關(guān) (運(yùn)行main方法啟動(dòng)服務(wù))
package com.raventech.user;
import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.util.MotanSwitcherUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author liuhaiming on 10/11/2016.
*/
@SpringBootApplication
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
System.out.println("Server start ...");
}
}
Client 端
- 刪除motan-client.xml配置文件和Client.java啟動(dòng)文件乙各,已經(jīng)沒(méi)用了
- 用注解加載motan需要的配置
package com.raventech.web.config;
import com.weibo.api.motan.config.springsupport.AnnotationBean;
import com.weibo.api.motan.config.springsupport.BasicRefererConfigBean;
import com.weibo.api.motan.config.springsupport.ProtocolConfigBean;
import com.weibo.api.motan.config.springsupport.RegistryConfigBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author liuhaiming on 10/11/2016.
*/
@Configuration
public class MotanConfiguration {
@Bean
public AnnotationBean motanAnnotationBean() {
AnnotationBean motanAnnotationBean = new AnnotationBean();
# 添加用到motan注解的類(lèi)的包名
motanAnnotationBean.setPackage("com.raventech.web.controller");
return motanAnnotationBean;
}
@Bean(name = "motan")
public ProtocolConfigBean protocolConfig1() {
ProtocolConfigBean config = new ProtocolConfigBean();
config.setDefault(true);
config.setName("motan");
config.setMaxContentLength(1048576);
return config;
}
@Bean(name = "registry")
public RegistryConfigBean registryConfig() {
RegistryConfigBean config = new RegistryConfigBean();
config.setRegProtocol("consul");
config.setAddress("127.0.0.1:8500");
return config;
}
@Bean(name = "basicRefererConfig")
public BasicRefererConfigBean basicRefererConfigBean() {
BasicRefererConfigBean config = new BasicRefererConfigBean();
config.setProtocol("motan");
config.setRegistry("registry");
config.setThrowException(true);
return config;
}
}
- 調(diào)用方法(在Controller中使用)
package com.raventech.web.controller;
import com.raventech.user.motan.HelloService;
import com.raventech.web.common.BaseController;
import com.weibo.api.motan.config.springsupport.annotation.MotanReferer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author liuhaiming on 10/11/2016.
*/
@RestController
@RequestMapping("/motan")
public class HelloController extends BaseController {
@MotanReferer(basicReferer = "basicRefererConfig")
private HelloService helloService;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() throws Exception {
return helloService.hello("world");
}
}