提供方和消费方properties 配置基本差不多
nutz.application.name这个值不一样
提供方配置自动端口就行server.port=0
消费方一般需要对外提供web服务配置ip和端口server.port=8088 server.host=0.0.0.0
服务提供方 application.properties
server.port=0 nutz.application.name=server dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=0 dubbo.annotation.package=com.chao.dubbo.rpc
服务消费方
server.port=8088 server.host=0.0.0.0 nutz.application.name=client dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=0 dubbo.annotation.package=com.chao.dubbo.rpc
添加依赖
<!--nutzboot 核心依赖--> <dependency> <groupId>org.nutz</groupId> <artifactId>nutzboot-core</artifactId> <version>2.3-SNAPSHOT</version> </dependency> <!--日志相关--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!--dubbo依赖--> <dependency> <groupId>org.nutz</groupId> <artifactId>nutzboot-starter-dubbo</artifactId> <version>2.3-SNAPSHOT</version> </dependency> <!--接口项目依赖--> <dependency> <groupId>com.chao.test</groupId> <artifactId>dubbo-common</artifactId> <version>${project.version}</version> </dependency> <!--zookeeper客户端依赖--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.11</version> </dependency> <!--下面依赖 服务提供方不要加入,服务消费方使用对外提供web服务--> <dependency> <groupId>org.nutz</groupId> <artifactId>nutzboot-starter-nutz-mvc</artifactId> <version>${nutzboot.version}</version> </dependency> <dependency> <groupId>org.nutz</groupId> <artifactId>nutzboot-starter-jetty</artifactId> <version>${nutzboot.version}</version> </dependency>
接口项目(dubbo-common 就一个接口类什么都没有)
public interface TimeService { long now(); }
服务提供方项目(dubbo-server)
@IocBean @Service(interfaceClass = TimeService.class) public class TimeServiceImpl implements TimeService { public long now() { return System.currentTimeMillis(); } }
启动类
@IocBean public class ServiceLauncher { public static void main(String[] args) throws Exception{ new NbApp().run(); } }
服务消费方项目(dubbo-client)
@IocBean public class ClientLauncher { //注入远程服务 @Inject @Reference private TimeService timeService; //使用http://localhost:8088/now调用测试 @At @Ok("raw") public long now(){ return timeService.now(); } public static void main(String[] args) throws Exception{ new NbApp().run(); } }
不要忘了启动zookeeper客户端
(win) zkServer.cmd (linux) zkServer.sh