dubbo实战之一:准备和初体验( 三 )

编码(服务提供方)

  • 先创建提供服务的工程helloxmldirectprovider , 一共要创建6个文件 , 创建顺序和功能如下表:

dubbo实战之一:准备和初体验文章插图
  • 完整的文件位置如下图:

dubbo实战之一:准备和初体验文章插图
  • 接下来逐个创建上述内容;
  • 创建名为helloxmldirectprovider的子工程 , pom.xml内容如下 , 可见刚才新建的二方库practiceinterface也被依赖了:
dubbopracticecom.bolingcavalry1.0-SNAPSHOT4.0.0com.bolingcavalryhelloxmldirectprovider1.0-SNAPSHOTcom.bolingcavalrypracticeinterface${project.version}org.apache.dubbodubboorg.projectlomboklombokorg.slf4jslf4j-log4j12
  • 编写启动类ProviderApplication.java , 可见就是个普通的后台程序 , 加载spring配置做初始化:
package com.bolingcavalry.helloxmldirectprovider;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ProviderApplication {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");context.start();System.in.read();}}
  • 编写提供具体服务的业务实现类DemoServiceImpl.java , 只是个简单的接口实现类而已:
package com.bolingcavalry.helloxmldirectprovider.service.impl;import com.bolingcavalry.dubbopractice.service.DemoService;import lombok.extern.slf4j.Slf4j;import org.apache.dubbo.rpc.RpcContext;@Slf4jpublic class DemoServiceImpl implements DemoService {@Overridepublic String sayHello(String name) {log.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();}}
  • 日志配置文件log4j.properties , 内容如下:
###set log levels###log4j.rootLogger=info, stdout###output to the console###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
  • 在同样位置创建dubbo配置文件dubbo.properties , 内容很简单只有qos的端口设置 , 用于支持telnet命令:
dubbo.application.qos.port=22222