motan整合zookeeper之如何发布服务器完成分布式调用

首先以最简单的一个例子开始介绍

api的接口是这么写的

@Path("/user") public interface UserService {

/** * 测试用例 返回字符串  这里随便返回的字符串   后来改成从数据库查询出的字符串

* @return

*/

@GET

@Path("/getString1")

@Consumes(MediaType.APPLICATION_JSON)

@Produces(MediaType.APPLICATION_JSON)

ResponseEntity getALLUsers();

}


server 端  实现类是这样的  这里查询的user信息,,返回一些数据

@Override

 public ResponseEntity getALLUsers() {

List ucUsers = ucUserMapper.select(); Map map = new HashMap<>();

map.put("rows",ucUsers);

return new ResponseEntity.Builder>().setData(map).setMessage("成功").setStatus(0).build(); }


功能大概就是这样  具体来说下配置 

 api 

这里是不需要什么配置的,只是一个对外暴露的接口,我这个测试用例用的是gradle 构建的  

server  

这个编辑器好无语不按格式显示只能这样了,把重点的搞出来

server.port=8076

mysql 配置

# REDIS (RedisProperties) spring.redis.database=1 spring.redis.host=192.168.1.203 spring.redis.port=6379 spring.redis.password= spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.timeout=0 # \ufffd\ufffd\u05be\ufffd\ufffd\ufffd\ufffd\ufffd\u026b spring.output.ansi.enabled=DETECT # mongodb spring.data.mongodb.uri=mongodb://192.168.1.203:27017/log_db # mybatis mybatis.config-locations=classpath:myBatis-config.xml mybatis.mapper-locations=classpath:mapper/*.xml ## motan

motan.registry.regProtocol=zookeeper

motan.registry.address=192.168.1.203:2181

motan.registry.connectTimeout=2000

##\u042d\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd motan.protocol.name=restful motan.protocol.endpointFactory=netty #motan.protocol.minWorkerThread=20 #\ufffd\ufffd\u0421\ufffd\ufffd\ufffd\ufffdpool\ufffd\u07f3\ufffd\ufffd\ufffd #motan.protocol.maxWorkerThread=50 #\ufffd\ufffd\ufffd\ufffd\ufffdpool\ufffd\u07f3\ufffd\ufffd\ufffd motan.protocol.maxContentLength=1048576 motan.protocol.isDefault=true #motan.protocol.filter=statistic ##\u05b8\ufffd\ufffd\ufffd\ufffd\u04aa\ufffd\ufffd\ufffd\ufffd\ufffd\u0130\ufffd\ufffd\ufffd

motan.annotation.package=cn.thunderwind.uc.user

##\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd

motan.server.export=motan:8004

motan.server.registry=registry


然后再去配置测试端  client   基本和server差不多 不需要数据库的配置

#\u05e2\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd motan.registry.regProtocol=zookeeper #motan.registry.address=192.168.1.203:2181 motan.registry.address=192.168.1.203:2181 motan.registry.connectTimeout=2000 #\u042d\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd motan.protocol.name=restful motan.protocol.endpointFactory=netty #motan.protocol.minWorkerThread=20 #\ufffd\ufffd\u0421\ufffd\ufffd\ufffd\ufffdpool\ufffd\u07f3\ufffd\ufffd\ufffd #motan.protocol.maxWorkerThread=50 #\ufffd\ufffd\ufffd\ufffd\ufffdpool\ufffd\u07f3\ufffd\ufffd\ufffd motan.protocol.maxContentLength=1048576 motan.protocol.isDefault=true #\u05b8\ufffd\ufffd\ufffd\ufffd\u04aa\ufffd\ufffd\ufffd\ufffd\ufffd\u0130\ufffd\ufffd\ufffd motan.annotation.package=cn.thunderwind.uc.user #server\u914d\u7f6e server.port=8076 motan.server.protocol=motan motan.server.registry=registry motan.server.throwException=true motan.server.filter=motanLogFilter,motanExceptionFilter # mongodb spring.data.mongodb.uri=mongodb://192.168.1.203:27017/log_db


client的目录结构要和另外两个一样 

@RestController

@RequestMapping("/user")

public class UserController {

@MotanReferer(basicReferer = "basicRefererConfig", directUrl = "192.168.1.211:8004")

//这里的8004是在server端的配置里的motan的内部调用端口 

private UserService userService;

@RequestMapping(value = "/getString", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")

@ResponseBody public ResponseEntity getALLUsers1() {

return userService.getALLUsers();

}


接下来打包发布 测试就可以了,server.port=8076  这个配置对应的服务器上的端口号

motan.server.export=motan:8004  这个配置是motan自己内部调用的端口号和服务器无关

发布之后,运行成功就代表成功了,如果没有,欢迎评论,加QQ2930824786讨论

你可能感兴趣的:(motan整合zookeeper之如何发布服务器完成分布式调用)