网上这类教程太多了,为什么我要写一个呢,因为,那些大神写的东西坑太多了,把他们的代码稍微改改就全报错,有些东西可能在大神眼里是基础,没必要细说。但是给后来者埋了一堆又一堆的坑,今天我先来填几个。
首先下载一个zookeeper,安装网上有教程,版本要3.4.0以上,打开。
1.服务端
结构图:
1.1 application.properties
## Dubbo 服务提供者配置
spring.dubbo.application.name=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.example.springboot.dubbo //第一个坑!!这一行 是注册的接口所在包,不是实现类!而且要和消费者的配置文件一模一样!! 不一样的话消费者是没办法调用的。
1.2 具体实现类
package com.example.springboot.dubbo.impl;
import com.alibaba.dubbo.config.annotation.Service; //第二个坑!这个@Service 要导入dubbo的包,不要spring的包,不然根本启动不起来
import com.example.springboot.dubbo.TestService;
// 注册为 Dubbo 服务
@Service(version = "1.0.0") // 这个要和消费者一样
public class TestServiceImpl implements TestService {
@Override
public String reString(String cityName) {
return "链接成功";
}
}
1.3接口
package com.example.springboot.dubbo;
public interface TestService {
/**
* 根据城市名称,查询城市信息
* @param cityName
*/
String reString(String cityName);
}
1.4 pom
<?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>springboot</groupId>
<artifactId>springboot-dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo 服务端:: 整合 Dubbo/ZooKeeper 详解 SOA 案例</name>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>
<dependencies>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
启动类用自动生成的就行,不用做任何改动。不要画蛇添足。
2.消费者
结构图
2.1application.properties
## 避免和 server 工程端口冲突
server.port=8081
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.example.springboot.dubbo //就这个坑 一定要和服务端一样 意味着 接口的结构要和服务端一样
2.2TestService
package com.example.springboot.dubbo;
public interface TestService {
String reString(String cityName);
}
2.3 controller
package com.example.springboot.dubbo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
public class TestController {
@Reference(version = "1.0.0")// 和服务端版本一致
TestService testService;
@RequestMapping(value = "/hgxone")
public void printCity() {
String cityName="温岭";
String city = testService.reString(cityName);
System.out.println(city.toString());
}
}
2.4 pom
<?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>springboot</groupId>
<artifactId>springboot-dubbo-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo 客户端:: 整合 Dubbo/ZooKeeper 详解 SOA 案例</name>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>
<dependencies>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
//第三个坑!! pom文件 引入的东西要一致,而且最好是我粘贴的这个版本,不然你会神奇的发现,死活注册不了服务。
最后 希望大佬们以后写点文章,稍微给我们这些弟弟们,标注一下比较容易出错的地方,感激不尽!!