pom引用

  1. <properties>
  2. <dubbo.version>2.7.3</dubbo.version>
  3. </properties>
  4. <dependencies>
  5. <dependency>
  6. <groupId>org.apache.dubbo</groupId>
  7. <artifactId>dubbo</artifactId>
  8. <version>${dubbo.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.apache.dubbo</groupId>
  12. <artifactId>dubbo-dependencies-zookeeper</artifactId>
  13. <version>${dubbo.version}</version>
  14. <type>pom</type>
  15. </dependency>
  16. </dependencies>

provider

配置文件

发布的时候可以和 jar同路径,会优先读取。

dubbo.properties

  1. dubbo.application.name=xxx-provider
  2. dubbo.protocol.port=-1
  3. dubbo.registry.address=zookeeper://ip-191:2181
  4. dubbo.protocol.name=dubbo
  5. dubbo.service.loadbalance=roundrobin
  6. dubbo.service.group=test
  7. dubbo.service.version=1.0
  8. dubbo.service.retries=0

启动类

  1. @EnableDubbo(scanBasePackages = "com.alvin")

service

  1. import org.apache.dubbo.config.annotation.Service;
  2. @Service
  3. public class AddService implements IAddService {
  4. @Override
  5. public Integer add(int a, int b) {
  6. return a + b;
  7. }
  8. }

consumer

配置文件


dubbo.properties

  1. dubbo.application.name=xxx-consumer
  2. dubbo.registry.address=zookeeper://ip-191:2181
  3. dubbo.consumer.check=false
  4. dubbo.reference.loadbalance=roundrobin
  5. dubbo.reference.group=test
  6. dubbo.reference.version=1.0
  7. dubbo.reference.retries=0

启动类

  1. @EnableDubbo(scanBasePackages = "com.alvin.service")

调用

  1. @Reference
  2. private IAddService addService;
  3. @Override
  4. public void run(String... args) throws Exception {
  5. System.out.println(addService.add(2,3));
  6. System.in.read();
  7. }