一、向本地仓库导入Dubbox依赖
1.1解压压缩包
1.2打开cmd窗口切到源码包路径
1.3输入命令行
1.4成功后展示如图
1.5输入命令行
1.6成功后如图
public class DoSomeServiceImpl implements DoSomeService { @Override public String doSome(String userName) { System.out.println("dubbo 发布的DoSomeService服务 dosome方法\t"+userName); return "dubbox"; } }
二、Dubbox案例
2.创建生产者项目dubbo_provider
2.1创建service层接口
@Path("/doSomeService") public interface DoSomeService { @Path("/dosome/{userName}") @GET @Consumes({MediaType.APPLICATION_JSON}) public String doSome(@PathParam("userName") String userName);
2.2创建service接口实现类
public class DoSomeServiceImpl implements DoSomeService { @Override public String doSome(String userName) { System.out.println("dubbo 发布的DoSomeService服务 dosome方法\t"+userName); return "dubbox"; } }
2.3创建applicationContext-provider.xml文件将项目 发送到注册中心
<dubbo:application name="dubbox-provider"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:protocol name="rest" port="8081"/> <dubbo:service interface="com.dubbo.service.DoSomeService" ref="doSomeService"/> <bean id="doSomeService" class="com.dubbo.service.impl.DoSome
2.4test测试类
public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-provider.xml"); System.out.println("dubbox服务已发布"); //阻塞 System.in.read(); }
2.5控制台
3.创建消费者项目dubbo_consumer
3.1创建service层接口
@Path("/doSomeService") public interface DoSomeService { @Path("/dosome/{userName}") @GET @Consumes({MediaType.APPLICATION_JSON}) public String doSome(@PathParam("userName") String userName); }
3.2创建applicationContext-consumer.xml文件将项目发送到注册中心
<dubbo:application name="dubbox-consumer"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference interface="com.dubbo.service.DoSomeService" id="doSomeService"/>
3.3test测试类
public static void main(String[] args) { ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-consumer.xml"); DoSomeService doSomeService=(DoSomeService) ctx.getBean("doSomeService"); doSomeService.doSome("张三"); }
3.4运行结果