标签:int dubbo bat res 模块 访问 没有 oca code
创建三个Maven Project:
public interface DemoService { public String demo(String name); }
1. pom.xml配置
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.linhw.demo</groupId> <artifactId>dubbo-service-impl</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.linhw.demo</groupId> <artifactId>dubbo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- dubbo依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <!-- 访问 zookeeper 的客户端 jar --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies> </project>
2. 接口实现类
public class DemoServiceImpl implements DemoService{ @Override public String demo(String name) { return "dubbo RPC " + name; } }
3. 新增配置文件dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 给当前provider自定义一个名字 --> <dubbo:application name="demo-provider"/> <!-- 配置注册中心 --> <dubbo:registry address="192.168.178.5:2181" protocol="zookeeper"/> <!-- 配置协议及端口 --> <dubbo:protocol name="dubbo" port="28888"/> <!-- 注册功能 --> <bean id="demoService" class="com.linhw.demo.service.impl.DemoServiceImpl"/> <dubbo:service interface="com.linhw.demo.service.DemoService" ref="demoService"/> </beans>
4. 启动容器
(1) 通过 spring 方式启动:对dubbo-provider.xml的位置没有要求
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("dubbo-provider.xml"); ctx.start(); System.out.println("启动成功"); System.in.read();
(2) 使用 dubbo 提供的方式启动(推荐使用这种方式):要求 dubbo-provider.xml必须放入类路径下/META-INF/spring/*.xml
Main.main(args);
查看是否发布成功,可以启动Dubbo Admin,在管理控制台查看。
标签:int dubbo bat res 模块 访问 没有 oca code
原文地址:https://www.cnblogs.com/myitnews/p/11484786.html