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

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

创建顺序 文件名 作用
1   pom.xml   工程的pom文件  
2   src/main/java/com/bolingcavalry/helloxmldirectprovider/ProviderApplication.java   启动类  
3   src/main/java/com/bolingcavalry/helloxmldirectprovider/service/impl/DemoServiceImpl.java   提供具体的服务  
4   src/main/resources/log4j.properties   日志配置文件  
5   src/main/resources/dubbo.properties   dubbo配置文件  
6   src/main/resources/spring/dubbo-provider.xml   spring的bean配置  

完整的文件位置如下图:

在这里插入图片描述

接下来逐个创建上述内容;

创建名为helloxmldirectprovider的子工程,pom.xml内容如下,可见刚才新建的二方库practiceinterface也被依赖了:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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 "> <parent> <artifactId>dubbopractice</artifactId> <groupId>com.bolingcavalry</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.bolingcavalry</groupId> <artifactId>helloxmldirectprovider</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.bolingcavalry</groupId> <artifactId>practiceinterface</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> </dependencies> </project>

编写启动类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; @Slf4j public class DemoServiceImpl implements DemoService { @Override public 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.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.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

在resources目录下新建文件夹spring,在此文件夹下创建文件dubbo-provider.xml,要重点关注的是dubbo:registry的配置,其address属性值为multicast://224.5.6.7:1234,代表当前服务通过广播让消费者获得自身信息:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <dubbo:application/> <!--广播模式--> <dubbo:registry address="multicast://224.5.6.7:1234" /> <bean/> <dubbo:service interface="com.bolingcavalry.dubbopractice.service.DemoService" ref="demoService"/> </beans>

至此,服务提供方编码完成,直接在IDEA上运行ProviderApplication类即可启动服务,启动成功后的日志输出如下图:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspyjz.html