spring boot +motan+zookeeper整合,jdk1.8+idea开发环境

        由于换了公司,公司用的是motan-RPC框架,所以就自己建了一个demo测试一下,有不足之处还请谅解,在网上搜了很多资料,motan+spring boot的整合还是比较少的,所以也耗时了几个小时。废话不多说,直接上干货。

       首先是zookeeper的安装,详见我的另一篇博客(https://blog.csdn.net/qq_27394485/article/details/85098947),这里就不赘述。

       一、server端:

        idea项目整体目录

        spring boot +motan+zookeeper整合,jdk1.8+idea开发环境_第1张图片

motan依赖:

		
			com.weibo
			motan-core
			0.2.1
			
		

		
			com.weibo
			motan-transport-netty
			0.2.1
			
                
                    slf4j-log4j12
                    org.slf4j
                
                
                    log4j
                    log4j
                
            
		

		
			com.weibo
			motan-springsupport
			0.2.1
			
                
                    slf4j-log4j12
                    org.slf4j
                
                
                    log4j
                    log4j
                
            
		

		
			com.weibo
			motan-registry-zookeeper
			0.2.1
			
                
                    slf4j-log4j12
                    org.slf4j
                
                
                    log4j
                    log4j
                
            
		

 

api 暴露服务,server是spring boot 实现

下面上详细代码:

HelloService:

/**
 *
 */
public interface HelloService {
     void say(String name);
}

HelloServiceImpl:

public class HelloServiceImpl implements HelloService {
    @Override
    public void say(String name) {
        System.out.println("hello "+name);
    }
}

MotandemoServerApplication.java

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;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(locations={"classpath*:motan-server.xml"})
public class MotandemoServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(MotandemoServerApplication.class, args);
		MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
	}

}

接下来是服务提供者的xml文件,即motan-server.xml,里面配置了一些RPC调用的信息以及注册信息等,玩过dubbo的应该很熟悉。




    

    

    

    

    
    

    

 

二、client端  

client是个我以前写的springbootdemo,我在里面加了一个motan-client.xml文件和包等即可完成,比较简单,但是容易出错,有人说是motan的bug,一会上错误信息。

controller类

@RestController
@EnableAutoConfiguration
public class HelloSpringBootApplication {

    @Autowired
    private ExceptionService exceptionService;
    @Autowired
    private UserService userService;

    @Autowired
    private HelloService helloService;

    @RequestMapping("hello")
    public Object  helloWorld(@RequestParam(value = "age",required = false) int age) throws Exception{
              int result=exceptionService.exceptionTest(age);
              helloService.say("刘先生");
              return new Result(200,"success",result);
        //return "hello world! This is springboot demo!";
    }

 

motan-client.xml




    

    

    

    

    

springboot启动类

@SpringBootApplication
@MapperScan("com.example.springbootdemo.dao")
@ImportResource(locations={"classpath*:motan-client.xml"})
public class SpringbootdemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootdemoApplication.class, args);
		MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
	}
}

三、运行阶段

启动zookeeper服务端后,先启动motan-server端,具体启动信息如下:

2018-12-22 21:18:37.879  INFO 14444 --- [           main] org.apache.zookeeper.ZooKeeper           : Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=60000 watcher=org.I0Itec.zkclient.ZkClient@3549bca9
2018-12-22 21:18:37.890  INFO 14444 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2018-12-22 21:18:37.891  INFO 14444 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2018-12-22 21:18:37.919  INFO 14444 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x100772732a90001, negotiated timeout = 40000
2018-12-22 21:18:37.925  INFO 14444 --- [           main] info                                     : CommandFailbackRegistry init. url: zookeeper://127.0.0.1:2181/com.weibo.api.motan.registry.RegistryService?group=default_rpc
2018-12-22 21:18:37.925  INFO 14444 --- [           main] info                                     : [ZookeeperRegistry] Url (motan://192.168.0.7:8030/com.finerliu.motandemoapi.HelloService?group=group-1) will register to Registry [zookeeper://127.0.0.1:2181/default_rpc/com.weibo.api.motan.registry.RegistryService/1.0/service]
2018-12-22 21:18:38.006  INFO 14444 --- [           main] c.f.m.MotandemoServerApplication         : Started MotandemoServerApplication in 1.336 seconds (JVM running for 1.959)
2018-12-22 21:18:38.006  INFO 14444 --- [           main] info                                     : [ZookeeperRegistry] Url (null) will set to available to Registry [zookeeper://127.0.0.1:2181/default_rpc/com.weibo.api.motan.registry.RegistryService/1.0/service]
2018-12-22 21:19:07.856  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-totalAccessStatistic] app: motan module: motan total_count: 0 slow_count: 0 biz_excp: 0 other_excp: 0 avg_time: 0.00ms biz_time: 0.00ms avg_tps: 0
2018-12-22 21:19:07.856  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-memoryStatistic] 43.19MB of 3632.00 MB (1.2%) used 
2018-12-22 21:19:07.856  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-statisticCallback] identity: motan://192.168.0.7:8030/group-1//1.0.0/service connectionCount: 0 taskCount: 0 queueCount: 0 maxThreadCount: 800 maxTaskCount: 1800
2018-12-22 21:19:37.866  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-totalAccessStatistic] app: motan module: motan total_count: 0 slow_count: 0 biz_excp: 0 other_excp: 0 avg_time: 0.00ms biz_time: 0.00ms avg_tps: 0
2018-12-22 21:19:37.866  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-memoryStatistic] 43.19MB of 3632.00 MB (1.2%) used 
2018-12-22 21:19:37.866  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-statisticCallback] identity: motan://192.168.0.7:8030/group-1//1.0.0/service connectionCount: 0 taskCount: 0 queueCount: 0 maxThreadCount: 800 maxTaskCount: 1800
2018-12-22 21:20:07.866  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-totalAccessStatistic] app: motan module: motan total_count: 0 slow_count: 0 biz_excp: 0 other_excp: 0 avg_time: 0.00ms biz_time: 0.00ms avg_tps: 0
2018-12-22 21:20:07.866  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-memoryStatistic] 43.20MB of 3632.00 MB (1.2%) used 
2018-12-22 21:20:07.866  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-statisticCallback] identity: motan://192.168.0.7:8030/group-1//1.0.0/service connectionCount: 0 taskCount: 0 queueCount: 0 maxThreadCount: 800 maxTaskCount: 1800
2018-12-22 21:20:37.863  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-totalAccessStatistic] app: motan module: motan total_count: 0 slow_count: 0 biz_excp: 0 other_excp: 0 avg_time: 0.00ms biz_time: 0.00ms avg_tps: 0
2018-12-22 21:20:37.863  INFO 14444 --- [pool-2-thread-1] serviceStatsLog                          : [motan-memoryStatistic] 43.20MB of 3632.00 MB (1.2%) used 

 

可以看到一直有心跳信息打印。

 

接下来是客户端启动:

2018-12-22 21:24:48.344  INFO 17208 --- [           main] c.e.s.SpringbootdemoApplication          : Started SpringbootdemoApplication in 2.923 seconds (JVM running for 3.793)
2018-12-22 21:24:48.344  INFO 17208 --- [           main] info                                     : [ZookeeperRegistry] Url (null) will set to available to Registry [zookeeper://127.0.0.1:2181/default_rpc/com.weibo.api.motan.registry.RegistryService/1.0/service]
2018-12-22 21:25:17.862  INFO 17208 --- [pool-4-thread-1] serviceStatsLog                          : [motan-totalAccessStatistic] app: motan module: motan total_count: 0 slow_count: 0 biz_excp: 0 other_excp: 0 avg_time: 0.00ms biz_time: 0.00ms avg_tps: 0
2018-12-22 21:25:17.862  INFO 17208 --- [pool-4-thread-1] serviceStatsLog                          : [motan-memoryStatistic] 36.15MB of 3632.00 MB (1.0%) used 
2018-12-22 21:25:34.966  INFO 17208 --- [nio-8081-exec-3] o.a.c.c.C.[Tomcat].[localhost].[/imooc]  : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-12-22 21:25:34.966  INFO 17208 --- [nio-8081-exec-3] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-12-22 21:25:34.993  INFO 17208 --- [nio-8081-exec-3] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 27 ms
2018-12-22 21:25:47.862  INFO 17208 --- [pool-4-thread-1] serviceStatsLog                          : [motan-totalAccessStatistic] app: motan module: motan total_count: 0 slow_count: 0 biz_excp: 0 other_excp: 0 avg_time: 0.00ms biz_time: 0.00ms avg_tps: 0
2018-12-22 21:25:47.862  INFO 17208 --- [pool-4-thread-1] serviceStatsLog                          : [motan-memoryStatistic] 49.22MB of 3632.00 MB (1.4%) used 
2018-12-22 21:26:17.873  INFO 17208 --- [pool-4-thread-1] serviceStatsLog                          : [motan-totalAccessStatistic] app: motan module: motan total_count: 0 slow_count: 0 biz_excp: 0 other_excp: 0 avg_time: 0.00ms biz_time: 0.00ms avg_tps: 0
2018-12-22 21:26:17.873  INFO 17208 --- [pool-4-thread-1] serviceStatsLog                          : [motan-memoryStatistic] 49.82MB of 3632.00 MB (1.4%) used 

一样是启动后有心跳,因为在springboot启动类中已经设置了。

接下来是在浏览器中请求:

spring boot +motan+zookeeper整合,jdk1.8+idea开发环境_第2张图片

 

可以看到server端的信息也在控制台打印出来了

至此motan+spring boot +zookeeper跑起来了。

忘了说client启动的时候报的错了,其中一个是我在pom.xml关掉了log等相关包后,motan的zookeeper包说找不到log相关包,所以我就打开了一个就正常了。另外一个是,client在启动的时候会报500错误,连接不上zookeeper,但是我多试几次后又好了,不知道是什么问题,还有待研究。

写的比较潦草,还望见谅,其中也有一些地方没搞懂原理,在学习后会更新的。

你可能感兴趣的:(学习)