spring boot + dubbo 服务部署实例
项目github:https://github.com/nalidou/spring-dubbo
1. 公共组件:dubbo-component
提供了接口定义、实体类等,其他项目可以直接导入jar包,也可以用maven导入。
<?xml version="1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.wzy dubbo-component 1.0
2. 服务生产者:dubbo-producer
实现了公共组件接口
com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0 org.springframework.boot spring-boot-starter-test test org.apache.zookeeper zookeeper 3.4.6 com.101tec zkclient 0.10 com.wzy dubbo-component 1.0
package com.wzy.dubbo.producer.server; import com.alibaba.dubbo.config.annotation.Service; import com.wzy.component.entity.User; import com.wzy.component.server.IUserServer; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Service(interfaceClass = IUserServer.class) //这里用的是dubbo的接口 @Component public class UserServerImpl implements IUserServer { @Override public User getById(long id) { return new User(id, "小明"); } @Override public ListgetAll() { List list = new ArrayList (); list.add(new User(100, "jerry")); list.add(new User(200, "tom")); return list; } }
3. web服务(服务消费者):dubbo-web
org.springframework.boot spring-boot-starter-web com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0 org.springframework.boot spring-boot-starter-test test org.apache.zookeeper zookeeper 3.4.6 com.101tec zkclient 0.10 com.wzy dubbo-component 1.0
package com.wzy.dubbo.web; import com.alibaba.dubbo.config.annotation.Reference; import com.wzy.component.entity.User; import com.wzy.component.server.IOrderServer; import com.wzy.component.server.IUserServer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WebCtl { @Reference private IOrderServer orderServer; @Reference private IUserServer userServer; @RequestMapping("getUser") public User getUser() { System.out.println("WebCtl getUser..."); System.out.println(orderServer.getName()); return userServer.getById(100); } }
4. 在zookeeper上查看注册节点