HM-RocketMQ2.3【SpringBoot整合Dubbo】
1 前置条件
安装依赖包
下载dubbo-spring-boot-starter依赖包
将dubbo-spring-boot-starter
安装到本地仓库
mvn install -Dmaven.skip.test=true
注意
springboot整合dubbo、springboot整合rocketmq的前置任务目的是一致的,就是在本地安装源码
其实完全可以通过在maven中导入依赖解决
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
我们在springboot-rocketmq-provider和springboot-rocketmq-consumer两个微服务的pom中均加入以上两个依赖(此处只需duboo的)
2 搭建zookeeper[单机三节点|伪]集群
HMZK5【Zookeeper集群】 - yub4by - 博客园 (cnblogs.com)
学习zk课程时,曾在云服务器上搭建过一个zk集群,刚好用上,启动就可
因腾讯云服务器即将到期,实际使用的是华为云服务器,新搭建zk集群
端口开放
centos76网络/端口/防火墙常用命令 - yub4by - 博客园 (cnblogs.com)
zk配置文件
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper-cluster/zk1/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
#server.1=123.249.83.224:2881:3881
#server.2=123.249.83.224:2882:3882
#server.3=123.249.83.224:2883:3883
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper-cluster/zk2/data
# the port at which the clients will connect
clientPort=2182
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
#server.1=123.249.83.224:2881:3881
#server.2=123.249.83.224:2882:3882
#server.3=123.249.83.224:2883:3883
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper-cluster/zk3/data
# the port at which the clients will connect
clientPort=2183
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
#server.1=123.249.83.224:2881:3881
#server.2=123.249.83.224:2882:3882
#server.3=123.249.83.224:2883:3883
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883
zk启动命令
/usr/local/zookeeper-cluster/zk1/bin/zkServer.sh start
/usr/local/zookeeper-cluster/zk2/bin/zkServer.sh start
/usr/local/zookeeper-cluster/zk3/bin/zkServer.sh start
3 dubbo:PRC服务接口
3.1 创建模块
在项目shop-project下创建仨maven模块:
springboot-dubbo-consumer
springboot-dubbo-provider
springboot-dubbo-interface
3.2 声明接口
在interface微服务下声明接口
package com.irun2u.service;
/**
* @Author: haifei
* @Date: 2022/11/9 8:32
*/
public interface UserService {
public String sayHello(String name);
}
3.3 导入依赖
分别在consumer和provider微服务的pom中导入interface微服务的依赖
<dependency>
<groupId>com.irun2u</groupId>
<artifactId>springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
4 服务提供者provider
4.1 添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!--API-->
<dependency>
<groupId>com.irun2u</groupId>
<artifactId>springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--spring-boot-stater-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j-to-slf4j</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
4.2 配置文件
# application.properties
spring.application.name=dubbo-demo-provider
spring.dubbo.application.id=dubbo-demo-provider
spring.dubbo.application.name=dubbo-demo-provider
# 云服务器上的伪集群
#spring.dubbo.registry.address=zookeeper://101.43.128.227:2181;zookeeper://101.43.128.227:2182;zookeeper://101.43.128.227:2183
spring.dubbo.registry.address=zookeeper://123.249.83.224:2181;zookeeper://123.249.83.224:2182;zookeeper://123.249.83.224:2183
# 本地虚拟机上的真集群
#spring.dubbo.registry.address=zookeeper://192.168.115.15:2181;zookeeper://192.168.115.16:2181;zookeeper://192.168.115.17:2181
spring.dubbo.server=true
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
4.3 启动类
package com.irun2u;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author: haifei
* @Date: 2022/11/9 9:42
*/
/**
* 注意:运行sb-dubbo-provider微服务前
* 必须先对sb-dubbo-interface微服务进行maven-install操作
* 因为provider和consumer服务均依赖于interface服务(导入了依赖)
*
* 直接运行会报错如下
* 错误: 找不到或无法加载主类 com.irun2u.ProviderApplication
*/
@EnableDubboConfiguration
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args){
SpringApplication.run(ProviderApplication.class, args);
}
}
4.4 服务实现
package com.irun2u.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.irun2u.service.UserService;
import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Service;
/**
* @Author: haifei
* @Date: 2022/11/9 9:44
*/
//@Service
@Service(interfaceClass = UserService.class)
@Component //为了区分dubbo的service注解,用Component替换spring的service注解
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "hello: " + name;
}
}
4.5测试
5 dubbo-admin
本地远程访问http://123.249.83.224:8080/dubbo-admin root/root
服务名即接口名
6 服务消费者consumer
6.1 添加依赖
<?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>com.irun2u</groupId>
<artifactId>springboot-dubbo-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!--API-->
<dependency>
<groupId>com.irun2u</groupId>
<artifactId>springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--WEB-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--spring-boot-stater-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j-to-slf4j</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
6.2 配置文件
spring.application.name=dubbo-demo-consumer
spring.dubbo.application.id=dubbo-demo-consumer
spring.dubbo.application.name=dubbo-demo-consumer
spring.dubbo.registry.address=zookeeper://123.249.83.224:2181;zookeeper://123.249.83.224:2182;zookeeper://123.249.83.224:2183
6.3 启动类
package com.irun2u;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author: haifei
* @Date: 2022/11/21 15:58
*/
@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
6.4 controller
package com.irun2u.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.irun2u.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: haifei
* @Date: 2022/11/21 15:59
*/
@RestController //此处响应数据,而不跳转页面
@RequestMapping("/user")
public class UserController {
@Reference //dubbo远端注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(String name){
return userService.sayHello(name);
}
}
6.5 测试
总结:以上,通过dubbo实现了rpc远程调用