Dubbo整合系列
一、zookeeper
Dubbo官方推荐使用Zookeeper作为注册中心
1、准备
1、安装Zookeeper
- 下载完了后,解压出来,到\apache-zookeeper-3.6.0-bin\conf目录下,将zoo_sample.cfg文件复制一份改一个名字:zoo.cfg
_
- 修改zoo.cfg文件,主要修改的是下面两个目录,需要到对应的路径下创建目录
- 启动。切换到apache-zookeeper-3.6.0-bin\bin目录下,双击zkServer.cmd启动就Ok了
2、dubbo安装
- 下载完后了之后解压,然后切换到dubbo-admin目录下,打开命令行,输入:mvn clean install,然后会在target目录产生一个war包,将产生的war包放入tomcat的webapps目录下,然后到tomcat的bin目录下执行startup.bat启动tomcat(我的tomcat默认端口为8080,启动时一直提示端口本占用,可以把tomcat的端口号换一个,我换的8081)
- 启动成功后就可以在浏览器访问dubbo可是页面了,登录用户和密码:root/root
2、项目搭建
1.先创建一个父项目dubbo-parent,然后引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
2、创建一个公共的api接口工程:common-api,然后创建一个HelloService接口
public interface HelloService {
public String sayHello(String message);
}
3、创建一个服务提供者工程:server-provider,然后引入common-api工程,实现common-api的中HelloService接口
@Service(interfaceClass = HelloService.class)
public class HelloServiceImpl implements HelloService {
public String sayHello(String message) {
return "hello:"+message;
}
}
这个地方的@Service是Dubbo框架中的注解,然后需要指明接口
配置文件的配置如下:
server:
port: 9001
dubbo:
application:
# 服务名称,保持唯一
name: server-provider
# zookeeper地址,用于向其注册服务
registry:
protocal: zookeeper
address: zookeeper://127.0.0.1:2181
#address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
protocol:
name: dubbo
port: 20880
scan: com.pyh.provider.service
启动类:
@EnableDubbo
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
System.out.println("=============start============");
SpringApplication.run(MainApplication.class,args);
System.out.println("=============end============");
}
}
4、创建一个服务消费者工程:server-consumer,然后引入commin-api工程,然后创建一个控制器
@RestController
public class HelloController {
@com.alibaba.dubbo.config.annotation.Reference
private HelloService helloService;
@RequestMapping("sayHello")
public String sayHello(String message){
return helloService.sayHello(message);
}
}
这个地方需要注意下,一定要使用dubbo的@reference注解,不然消费者就不能注册到dubbo中,我在这个地方折腾了好久,人都快崩溃了,切记,切记,切记!
配置文件:
server:
port: 9002
dubbo:
application:
name: server-consumer
registry:
protocol: zookeeper
address: zookeeper://127.0.0.1:2181
#address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
scan:
base-packages: com.pyh.consumer
启动类:
@SpringBootApplication
@EnableDubboConfig
public class MainApplication {
public static void main(String[] args) {
System.out.println("=============start===============");
SpringApplication.run(MainApplication.class,args);
System.out.println("=============end===============");
}
}
5、启动server-provider和server-consumer服务
启动完成后就会在dubbo的页面看到服务的提供者和消费者,到此就搭建完成了
3、小问题
出现一个很小的问题,但是耗费了好长时间才解决,在server-consumer中使用过的@reference不是dubbo中的,导致注册失败!
二、Multicast
Dubbo 中有个 注册中心 这个东西,官方推荐的虽然是 Zookeeper,但由于 Zookeeper 东西也比较多,我们本篇博客暂时使用 Multicast 注册中心 带大家入门。
Multicast 只适用于学习和测试,非常不推荐用于实际生产,这也是官方说的。
Multicast 注册中心 不需要启动任何中心节点,只要广播地址一样,就可以互相发现。下面是官方提供的结构图:
- 提供方启动时广播自己的地址;
- 消费方启动时广播订阅请求;
- 提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了 unicast=false,则广播给订阅者;
- 消费方收到提供方地址时,连接该地址进行 RPC 调用。
由组播受网络结构限制,只适合小规模应用或开发阶段使用。组播地址段: 224.0.0.0 - 239.255.255.255。
一、搭建 Maven 项目
1、dubbo-demo 项目的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
<module>dubbo-api</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.7.3</dubbo.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、创建 dubbo-api、dubbo-server、dubbo-client 子模块
二、dubbo-api
1、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、User 类
public class User implements Serializable {
private Integer id;
private String name;
// set..() & get..()
// toString()
}
3、UserService 接口
public interface UserService {
User selectUserById(Integer id);
}
三、dubbo-provider
1、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- dubbo-api依赖 -->
<dependency>
<groupId>com.demo</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.yml
#防止端口冲突
server:
port: 8001
dubbo:
application:
# 应用名称
name: provider
registry:
# 注册中心地址
address: multicast://224.5.6.7:1234
# 超时时间,单位毫秒
timeout: 6000
protocol:
#协议名称
name: dubbo
#协议端口
port: 20880
scan:
#扫描包的位置
base-packages: com.demo.service
3、UserServiceImpl
import org.apache.dubbo.config.annotation.Service;
@Service(version = "1.0")
public class UserServiceImpl implements UserService {
@Override
public User selectUserById(Integer id) {
User user = new User();
user.setId(id);
user.setName("张三");
return user;
}
}
4、DubboProviderApplication
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
四、dubbo-consumer
1、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- dubbo-api依赖 -->
<dependency>
<groupId>com.demo</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.yml
#防止端口冲突
server:
port: 8002
dubbo:
application:
# 应用名称
name: consumer
registry:
# 注册中心地址
address: multicast://224.5.6.7:1234
3、UserInit
@Component
public class UserInit implements CommandLineRunner {
@Reference(version = "1.0",parameters = {
"unicast","false"})
private UserService userService;
@Override
public void run(String... args) throws Exception {
System.out.println(userService.selectUserById(2));
}
}
4、DubboConsumerApplication
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
五、测试
先执行 DubboProviderApplication ( 服务提供者程序),再执行 DubboConsumerApplication ( 服务消费者程序)。
三、Redis
有了前面的Multicast的经验,用Redis作为注册中心就很容易了。
环境搭建都大致相同。
加入依赖
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
创建AP项目用来放接口和实体bean
创建调用服务提供者项目
application.properties
server.port=80
spring.dubbo.appname=spring-boot-starter-dubbo-provider-test
spring.dubbo.registry=redis://127.0.0.1:6379
spring.dubbo.protocol=dubbo
服务提供者启动类
@SpringBootApplication
@DubboComponentScan(basePackages = "com.rookie.*.impl")
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class, args);
}
}
服务提供者接口实现类
@Service(interfaceClass = DemoService.class)
public class DemoServiceImpl implements DemoService {
@Override
public User getUser() {
return new User("1", "2");
}
}
创建服务消费者项目
application.properties
server.port=8080
spring.dubbo.appname=spring-boot-starter-dubbo-consumer-test
spring.dubbo.registry=redis://127.0.0.1:6379
spring.dubbo.protocol=dubbo
服务消费者启动类
@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
服务消费者controller
@RestController
public class Demo {
@Reference(interfaceClass = DemoService.class)
private DemoService demoService;
@RequestMapping("/")
public void demo() {
System.out.println(demoService.getUser());
}
}
测试
和multicast一样,不再赘述。