maven管理。用MyEclipse 2017
项目地址:https://github.com/hannibal2017/p2p/tree/simpleDemo
项目结构如下图:
步骤如下:
1,new - maven project ,新建p2p-dubbo,package选pom
2,在项目p2p-dubbo建立子项目,右键-maven-new maven module
分别创建子项目:p2p-dubbo-api,p2p-dubbo-provider,p2p-dubbo-consumer
3,父项目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.luo</groupId>
<artifactId>p2p-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<dubbo.version>2.6.1</dubbo.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo框架 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<modules>
<module>p2p-dubbo-api</module>
<module>p2p-dubbo-provider</module>
<module>p2p-dubbo-consumer</module>
</modules>
</project>
4,p2p-dubbo-api新建立一个接口DemoService
package com.luo.test; public interface DemoService { String test(); }
5,提供者:p2p-dubbo-provider。
pom.xml引入子项目:
<dependencies> <dependency> <groupId>com.luo</groupId> <artifactId>p2p-dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
实现接口:
package com.luo.provider.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.luo.test.DemoService; @Service(version = "1.0.0", interfaceName = "com.luo.test.DemoService") public class DemoServiceImpl implements DemoService { @Override public String test() { return "恭喜连接成功"; } }
引入资源配置文件,有三个,这里选主要的
server.port=8081 # dubbo spring.dubbo.application.name=p2p-provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.luo.provider.service.impl spring.dubbo.provider.filter=-exception
建立启动类ProviderApplication:
package com.luo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
结构图如下:
5,消费者:p2p-dubbo-consumer。
pom.xml导入子项目
<dependencies> <dependency> <groupId>com.luo</groupId> <artifactId>p2p-dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
引入资源配置文件,有三个,这里选主要的
server.port=8082 # dubbo spring.dubbo.application.name=p2p-consumer spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.luo.control spring.dubbo.provider.filter=-exception
新建一个类
package com.luo.control; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.luo.test.DemoService; @RequestMapping("test") @RestController public class TestControl { @Reference(version = "1.0.0", check = false) private DemoService demoService; @RequestMapping("getMessage") public String test() { System.out.println("getMessage:" + demoService.test()); return "OK:返回信息=" + demoService.test(); } @RequestMapping("index") public String index() { return "你好啊"; } }
新建启动类ConsumerApplication
package com.luo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
6,运行,测试:
下载zookeeper,并启动。启动项目provider,consumer。然后在浏览器输入:http://localhost:8082/test/getMessage
这是基础的例子,后面加上其他功能。
7 dubbo管理控制dubbo-admin
7.1 去 https://github.com/apache/incubator-dubbo/tree/dubbo-2.6.0下载项目
下载后进入文件夹dubbo-admin,执行:
mvn package -Dmaven.skip.test=true
执行完后,如图
把这个war包放到Tomcat的webapp下面,并改名为ROOT,(apache-tomcat-8.5.38)
启动后,浏览器输入:http://localhost:8080/。再输入用户名和密码,root和root。如图