实现消费者项目代码调用提供者项目代码,使用 zookeeper 做为注册中心
interface 项目
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>dubbo</groupId> <artifactId>demo-interface</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
接口
package org.apache.dubbo.demo; public interface DemoService { String sayHello(String name); }
项目结构
Provider 提供者项目
基本 springboot 项目,不用添加任何模块(starter)
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>dubbo</groupId> <artifactId>demo-boot-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-boot-provider</name> <description>demo-boot-provider</description> <properties> <java.version>1.8</java.version> <spring-boot.version>2.1.4.RELEASE</spring-boot.version> <dubbo.version>2.7.1</dubbo.version> </properties> <!--版本管理--> <dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>dubbo</groupId> <artifactId>demo-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <type>pom</type> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
配置,application.properties
#1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) dubbo.application.name=demo-provider dubbo.application.qos-enable=true dubbo.application.qos-port=22222 dubbo.application.qos-accept-foreign-ip=false #2、指定注册中心的位置 dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181 #3、指定通信规则(通信协议?通信端口) dubbo.protocol.name=dubbo dubbo.protocol.port=20880
接口实现
package dubbo.demo.boot.provider.service.impl; import org.apache.dubbo.config.annotation.Service; import org.apache.dubbo.demo.DemoService; import org.springframework.stereotype.Component; @Service //暴露服务,非 spring 的注解 @Component public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { System.out.println("Hello " + name + ", request from consumer: "); return "Hello " + name + ", response from provider: "; } }
启动类
package dubbo.demo.boot.provider; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo // 开启 Dubbo @SpringBootApplication public class DemoBootProviderApplication { public static void main(String[] args) { SpringApplication.run(DemoBootProviderApplication.class, args); } }
项目结构
Consumer 消费者项目
创建时加入 web 模块
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>dubbo</groupId> <artifactId>demo-boot-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-boot-consumer</name> <description>demo-boot-consumer</description> <properties> <java.version>1.8</java.version> <spring-boot.version>2.1.4.RELEASE</spring-boot.version> <dubbo.version>2.7.1</dubbo.version> </properties> <!--版本管理--> <dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>dubbo</groupId> <artifactId>demo-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <type>pom</type> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
配置,application.properties
#1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) dubbo.application.name=demo-consumer dubbo.application.qos-enable=true dubbo.application.qos-port=33333 dubbo.application.qos-accept-foreign-ip=false #2、指定注册中心的位置 dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181 #3、指定通信规则(通信协议?通信端口) dubbo.protocol.name=dubbo dubbo.protocol.port=20880
controller 层
package dubbo.demo.boot.consumer.controller; import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.demo.DemoService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class Consumer { @Reference // 引用服务 private DemoService demoService; @ResponseBody @RequestMapping("test") public String test() { return demoService.sayHello("world"); } }
启动类
package dubbo.demo.boot.consumer; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo // 开启 Dubbo @SpringBootApplication public class DemoBootConsumerApplication { public static void main(String[] args) { SpringApplication.run(DemoBootConsumerApplication.class, args); } }
项目结构
测试
先启动提供者,再启动消费者,最后访问 http://127.0.0.1:8080/test
https://github.com/apache/incubator-dubbo-spring-boot-project
http://dubbo.apache.org/zh-cn/docs/user/configuration/annotation.html