大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
新浪科技讯 2016年5月10日,微博方面宣布,支撑微博千亿调用的轻量级 RPC 框架 Motan 正式开源了。微博技术团队希望未来能有更多优秀的开源人入驻,并进一步完善优化。
搭建新浪RPC框架motan Demo:http://blog.csdn.net/linuu/article/details/53115290
motan是新浪微博开源的RPC框架,github官网是:https://github.com/weibocom/motan
今天就先搭建一个Hello world demo,本demo基于motan 0.2.1版本
首先先去github下载源代码(motan-manager报错请忽略,eclipse的web Module不兼容):
选中motan下的pom.xml,进行编译打包:
下载源码编译打包的目的有三个:
1)下载源代码可以方便源代码阅读学习
2)下载到motan.xsd,因为motan与spring进行了继承,有了自己的schema,eclipse会不识别,会导致xml报错,跟dubbo的配置文件报错一个道理
3)打包获取到motan-manager的页面管理端的war包(在motan-manager的target文件下找到打好的war包):
单独找一个tomcat,将其放入webapp文件下
打开浏览器http://localhost:8080/motan-manager-0.2.2-SNAPSHOT:
到此为止motan的管理平台就搭建好了,下面就搭建基于zookeeper为注册中心的RPC demo
1)首先先启动一个简单的zookeeper实例,在windows下简单的启动一个,写个demo,不需要集群
打开eclipse,搭建两个简单的maven项目:
motan-provider为服务提供端,motan-client服务调用端。两个项目的依赖基本上一样的:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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-registry-zookeeper</artifactId>
<version>${motan.version}</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-springsupport</artifactId>
<version>${motan.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
服务提供端motan-provider的编写
1)我们写一个简单的接口HelloService:
package com.motan.rpc.service;
public interface HelloService {
public String hello(String str);
}
}
HelloSerivce简单的实现类HelloServiceImpl:
package com.motan.rpc.service;
public class HelloServiceImpl implements HelloService {
public String hello(String str) {
return "hello " + str;
}
}
然后在src/main/resources下配置spring-motan-provider.xml
<?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">
<!-- spring管理的服务具体实现 -->
<bean id="serviceImpl" class="com.bazinga.study.motan.rpc.service.HelloServiceImpl"/>
<!-- 注册中心的配置 -->
<motan:registry regProtocol="zookeeper" name="zkRegistry" address="127.0.0.1:2181" connectTimeout="2000"/>
<!-- 协议配置。为防止多个业务配置冲突,推荐使用id表示具体协议。-->
<motan:protocol id="demoMotan" default="true" name="motan" maxServerConnection="80000"
maxContentLength="1048576" maxWorkerThread="800" minWorkerThread="20"/>
<!-- 服务的默认配置 group:标识服务的分组 module:标识模块信息 registry:标识service使用的注册
中心,与motan:registry中的name对应 -->
<motan:basicService export="demoMotan:8002" group="lyncc-motan-demo-rpc"
accessLog="true" shareChannel="true" module="motan-demo-rpc" application="myMotanDemo"
registry="zkRegistry" id="serviceBasicConfig"/>
<!-- 要暴露的服务,唯一标识是integerface -->
<motan:service interface="com.bazinga.study.motan.rpc.service.HelloService"
ref="serviceImpl" export="demoMotan:8001" basicService="serviceBasicConfig"/>
</beans>
注意此时你的IDE可能会报错,不识别<motan:registry>这些标签,解决方案:
Window–>Preferences—>XML:
选中我们刚才冲源码包中获取到的motan.xsd,然后key type选择Schema location 最最关键的就是key值了:http://api.weibo.com/schema/motan.xsd
这个值要与你命名空间中的值一样:
重新打开spring-motan-provider.xml文件就不报错了~
好了,我们继续,编写provider的启动类ProviderStarter:
package com.motan.rpc.quickstart;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.util.MotanSwitcherUtil;
public class ProviderStarter {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-motan-provider.xml");
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
System.out.println("server start...");
}
}
启动该类:
此时再打开motan-manager的管理页面:
可以查询到我们刚才注册好的服务,并且可以看到Server的服务者的个数是1个,我们开始编写motan-client的编写:
1)先把motan-provider端的HelloSerivce接口复制到motan-client,package的目录也要一样,因为接口的绝对路径被认为服务的唯一标识
2)在motan-client的src/main/resources文件夹下新建文件spring-motan-client.xml
<?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="zookeeper" name="registry" address="127.0.0.1:2181"/>
<!-- motan协议配置 -->
<motan:protocol default="true" name="motan" haStrategy="failover"
loadbalance="roundrobin" maxClientConnection="10" minClientConnection="2"/>
<!-- 通用referer基础配置 -->
<motan:basicReferer requestTimeout="200" accessLog="true" retries="2" group="lyncc-motan-
demo-rpc" module="motan-demo-rpc" application="myMotanDemo" protocol="motan"
registry="registry" id="motantestClientBasicConfig" throwException="false" check="true"/>
<motan:referer id="remoteService" interface="com.bazinga.study.motan.rpc.service.HelloService"
connectTimeout="300" requestTimeout="300" basicReferer="motantestClientBasicConfig"/>
</beans>
3)编写启动类:
package com.motan.rpc.quickstart;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bazinga.study.motan.rpc.service.HelloService;
public class ClientStart {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-motan-client.xml");
HelloService service = (HelloService) ctx.getBean("remoteService");
for(int i = 0;i< 50;i++){
System.out.println(service.hello("motan " + i));
}
}
}
4)log4j.properties
log4j.rootLogger=warn
log4j.logger.info=info,infofile
log4j.logger.warn=warn,warnfile
log4j.logger.error=error,errorfile
log4j.logger.profile=info,profile
log4j.logger.trace=trace,tracefile
log4j.logger.accessLog=debug,accessfile
log4j.logger.serviceStatsLog=info,serviceStatsLog
log4j.appender.tracefile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.tracefile.file=./logs/client/trace.log
log4j.appender.tracefile.DatePattern='.'yyyyMMdd
log4j.appender.tracefile.layout=org.apache.log4j.PatternLayout
log4j.appender.tracefile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p] %m%n
log4j.appender.accessfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.accessfile.file=./logs/client/access.log
log4j.appender.accessfile.DatePattern='.'yyyyMMdd
log4j.appender.accessfile.layout=org.apache.log4j.PatternLayout
log4j.appender.accessfile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p] %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p %d{yy/MM/dd HH:mm:ss} %-50.50c(%L) - %m%n
log4j.appender.infofile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.infofile.file=./logs/client/info.log
log4j.appender.infofile.DatePattern='.'yyyyMMdd-HH
log4j.appender.infofile.layout=org.apache.log4j.PatternLayout
log4j.appender.infofile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p] %m%n
log4j.appender.warnfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.warnfile.file=./logs/client/warn.log
log4j.appender.warnfile.DatePattern='.'yyyyMMdd
log4j.appender.warnfile.layout=org.apache.log4j.PatternLayout
log4j.appender.warnfile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p] %m%n
log4j.appender.errorfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorfile.file=./logs/client/error.log
log4j.appender.errorfile.DatePattern='.'yyyyMMdd
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorfile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p] %m%n
#profile
log4j.appender.profile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.profile.file=./logs/client/profile.log
log4j.appender.profile.DatePattern='.'yyyyMMdd-HH
log4j.appender.profile.layout=org.apache.log4j.PatternLayout
log4j.appender.profile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %m%n
#serviceStats
log4j.appender.serviceStatsLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.serviceStatsLog.file=./logs/client/serverserviceStatsLog.log
log4j.appender.serviceStatsLog.DatePattern='.'yyyyMMdd
log4j.appender.serviceStatsLog.layout=org.apache.log4j.PatternLayout
log4j.appender.serviceStatsLog.layout.ConversionPattern=%-d{HH:mm:ss} %m%n
启动ClientStart.java
现在已经调通了,我们再看看motan-manager:
好了,到此为此,一个简单的motan demo已经搭建好了~
完整的项目截图:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/207136.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...