一个简易版工程终于调通了,使用springboot框架,zookeeper注册中心,dubbo服务(提供者和消费者)。
一、dubbo服务提供者:
1.SayHelloImpl.java
package com.dubbo.provider.demo.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.demo.api.ISayHello;
@Service
public class SayHelloImpl implements ISayHello {
@Override
public String hello(String name) {
String out = "hello,"+name;
return out;
}
}
2.DemoApplication.java
package com.dubbo.provider.demo;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
3.application.properties
server.port=20000
spring.application.name=dubbo-provider-demo
# dubbo
dubbo.application.name=dubbo-provider-demo
dubbo.protocol.name=dubbo
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.port=10012
4.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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<groupId>com.dubbo.demo</groupId>
<artifactId>dubbo-provider-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.artifactId>dubboProviderDemo</project.artifactId>
</properties>
<dependencies>
<dependency>
<groupId>com.dubbo.demo</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.12</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、dubbo服务消费者:
1.CallProviderService.java
package com.dubbo.customer.demo.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.demo.api.ISayHello;
import org.springframework.stereotype.Service;
@Service
public class CallProviderService {
@Reference
ISayHello sayHello;
public void callInterface(){
String res = sayHello.hello("hl");
System.out.println("res--"+res);
}
}
2.DemoApplication.java
package com.dubbo.customer.demo;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
3.CallProviderServiceController.java
package com.dubbo.customer.demo.controller;
import com.dubbo.customer.demo.service.CallProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class CallProviderServiceController {
@Autowired
private CallProviderService callProviderService;
@RequestMapping("aa")
public void subscribeSayHello(){
callProviderService.callInterface();
}
}
4.application.properties
server.port=26000
spring.application.name=dubbo-customer-demo
dubbo.application.name=dubbo-customer-demo
dubbo.protocol.name=dubbo
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.check=false
5.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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<groupId>com.dubbo.demo</groupId>
<artifactId>dubbo-customer-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.artifactId>dubboCustomerDemo</project.artifactId>
</properties>
<dependencies>
<dependency>
<groupId>com.dubbo.demo</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.12</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
本地调试过程中遇到的问题:
1.由于本地电脑启用了虚拟网卡,导致启动起来的提供者IP和消费者IP不同;因为提供者和消费者都注册在本地zookeeper,在zookeeper上查看两个服务信息,其IP应该确保是同一个。
解决方式:禁止本地电脑网络的虚拟网卡。
2.两个服务正常启动后,消费者总是提示找不到服务者提供的服务。后来发现消费者服务里没有引用zookeeper。这个问题困扰比较久。
解决方式:在消费者的pom文件里引入zookeeper