在 Spring 框架中使用 Dubbo
Posted xian19900116
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在 Spring 框架中使用 Dubbo相关的知识,希望对你有一定的参考价值。
在 Spring 框架中使用 Dubbo
参考网址
http://dubbo.apache.org/zh-cn/ Dubbo 官网
http://dubbo.apache.org/zh-cn/docs/user/quick-start.html Dubbo 文档
框架组成
spring 5.1.3 + zookeeper 3.6.2 + dubbo 2.6.5;
工具:IDEA 2019.1.2
Dubbo 体系结构
执行流程:
-
启动注册中心:例如 zookeeper,接受 provider 的注册,接受 consumer 的订阅,当 provider 有变化的时候,会主动通知 consumer;
-
启动Provider:provider是服务的提供者,例如提供 com.xian.service.impl.UserServiceImpl 这个服务,它是服务的具体实现,provider 启动的时候会向 zookeeper 这个注册中心注册,表明自己可以提供哪些服务;
-
启动Consumer:consumer 是服务的消费者,例如通过 com.xian.controller.UserController 去调用 UserServiceImpl,consumer 启动的时候会去 zookeeper 这个注册中心 订阅自己需要的服务;
-
启动Registry:注册中心,例如 zookeeper,接受 provider 的注册,接受 consumer 的订阅,当 provider 有变化的时候,会主动通知 consumer;
-
consumer 调用 provider 的具体服务,例如 UserController 调用 UserServiceImpl,但是具体由那一台 provider 服务器提供服务,取决于 负载均衡调度算法;
-
Monitor:监控中心,统计服务被调用的次数和时间,服务的消费者和提供者会在内存中累计调用次数和
调用时间,定时每分钟向监控中心发送一次统计数据;
创建 Project
菜单栏:File| New Project, 创建一个空的 Project
填 Project 的名称和存放路径,然后点击右下角的【Finish】:
要在这个 Project 下创建 3 个子 module,最终目录 module 结构如下:
工程:dubbo-spring-common
创建工程
填项目的坐标:GroupId, ArtifactId
工程结构
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>
<groupId>com.xian</groupId>
<artifactId>dubbo-spring-common</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
</project>
User.java
package com.xian.entity;
import java.io.Serializable;
/**
* Author: xian
* Date: 2020-10-19 21:02
* Description: 描述
*/
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username=‘" + username + ‘‘‘ +
", password=‘" + password + ‘‘‘ +
‘}‘;
}
}
UserService.java
package com.xian.service;
import com.xian.entity.User;
/**
* Author: xian
* Date: 2020-10-19 21:04
* Description: 描述
*/
public interface UserService {
public User findById(int id);
}
工程:dubbo-spring-provider
创建工程
父 module 和父工程全部选【None】:
填项目的坐标:GroupId, ArtifactId
工程结构
找依赖包的方法
去仓库 https://mvnrepository.com/ 中找,例如找 dubbo,搜索以后一般用 usages使用量最高的那个,点击包的标题进入下一个页面:
版本号也是选择使用量最高的,点击版本号进入下一个页面:
默认选中了 maven 标签,点击一下就自动复制到粘贴板了;
把复制到的包名贴到 module 的根目录下的 pom.xml 文件中, 然后点击右下角的 【Import Changes】,maven 就会自动从仓库中下载这个版本的包;
找 zookeeper 包也和上面的流程一样:
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>
<groupId>com.xian</groupId>
<artifactId>dubbo-spring-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 依赖于 dubbo-spring-common -->
<dependency>
<groupId>com.xian</groupId>
<artifactId>dubbo-spring-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 依赖于 spring 的 4 个包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo 的核心包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- zookeeper 核心包 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
</dependency>
<!-- zookeeper 的客户端实现 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.1.3.RELEASE</spring.version>
</properties>
</project>
provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 配置应用程序的名称,一般使用项目名 -->
<dubbo:application name="dubbo-spring-provider"/>
<!-- 指定注册中心 zookeeper 的地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 协议和开放的访问端口-->
<dubbo:protocol name="dubbo" port="8888"/>
<!-- 配置要暴露的端口-->
<dubbo:service interface="com.xian.service.UserService" ref="userService"/>
<bean id="userService" class="com.xian.service.impl.UserServiceImpl"/>
</beans>
UserServiceImpl.java
package com.xian.service.impl;
import com.xian.entity.User;
import com.xian.service.UserService;
/**
* Author: xian
* Date: 2020-10-19 21:22
* Description: 描述
*/
public class UserServiceImpl implements UserService {
@Override
public User findById(int id) {
System.out.println("UserServiceImpl.findById");
User user = new User();
user.setId(id);
user.setUsername("xian");
user.setPassword("123");
return user;
}
}
Test.java
这是测试程序
package com.xian;
import javassist.ClassPath;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Scanner;
/**
* Author: xian
* Date: 2020-10-19 22:17
* Description: 描述
*/
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:provider.xml");
new Scanner(System.in).next();
}
}
工程:dubbo-spring-consumer
创建工程
创建 ioc 容器文件:consumer.xml
创建后的效果:
工程结构
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>
<groupId>com.xian</groupId>
<artifactId>dubbo-spring-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 依赖于 dubbo-spring-common -->
<dependency>
<groupId>com.xian</groupId>
<artifactId>dubbo-spring-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- spring 的 4 个核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo 核心包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- zookeeper 的客户端实现 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.1.3.RELEASE</spring.version>
</properties>
</project>
consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-spring-consumer"/>
<dubbo:reference id="userService" interface="com.xian.service.UserService" retries="3" check="false" url="dubbo://127.0.0.1:8888"/>
<bean class="com.xian.controller.UserController">
<property name="userService" ref="userService"/>
</bean>
</beans>
UserController.java
package com.xian.controller;
import com.xian.entity.User;
import com.xian.service.UserService;
/**
* Author: xian
* Date: 2020-10-19 22:24
* Description: 描述
*/
public class UserController {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void findUser() {
User user = userService.findById(1001);
System.out.println(user);
}
}
Test.java
package com.xian;
import com.xian.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Scanner;
/**
* Author: xian
* Date: 2020-10-19 22:39
* Description: 描述
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:consumer.xml");
UserController userController = ac.getBean(UserController.class);
userController.findUser();
new Scanner(System.in).next();
}
}
启动 zookeeper
启动 dubbo-spring-provider
打开 Test.java (dubbo-spring-provider) ,按 Ctrl + Shift + F10,或者右键 【Run ‘Test main‘】运行此文件;
启动 dubbo-spring-consumer
打开 Test.java (dubbo-spring-consumer) ,按 Ctrl + Shift + F10,或者右键 【Run ‘Test main‘】运行此文件;
打印
Test.java (dubbo-spring-provider) 打印:
Test.java (dubbo-spring-consumer) 打印:
可以看到,consumer 访问 provider 提供的服务了;
以上是关于在 Spring 框架中使用 Dubbo的主要内容,如果未能解决你的问题,请参考以下文章
微服务架构的基础框架选择:Spring Cloud还是Dubbo?
稳定性 耗时 监控原因分析-- dubbo rpc 框架 的线程池,io 连接模型. 客户端,服务端