Dubbo
Dubbo直连
1、创建项目结构

2、添加依赖
link_consumer pom.xml
<dependencies>
<!-- Dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- 接口 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>link_provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
link_provider pom.xml
<dependencies>
<!-- Dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
</dependencies>
<build>
<!-- 不添加打包的时候会报错 -->
<defaultGoal>compile</defaultGoal>
</build>
3、添加配置文件
link_consumer
dubbo-link-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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 声明服务消费者名称,保证它的唯一性,它是dubbo内部使用的唯一标识 -->
<dubbo:application name="link_consumer"/>
<!--
引用远程接口
id 远程接口服务的代理对象名称
interface 接口的全限定类名
url 调用远程接口服务的url地址
registry 直连方式,不使用注册中心,值:N/A
-->
<dubbo:reference id="someService"
interface="com.dyf.dubbo.service.SomeService"
url="dubbo://localhost:20880"
registry="N/A"/>
</beans>
spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.dyf.dubbo.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-link-consumer.xml,classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
link_provider
dubbo-link-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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 声明服务提供者名称,保证它的唯一性,它是dubbo内部使用的唯一标识 -->
<dubbo:application name="link_provider"/>
<!--
指定dubbo的协议名称和端口号
name 指定协议名称,官方推荐dubbo协议
port 协议的端口号,默认为20880
-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--
暴露服务
interface 暴露服务的接口全限定类名
ref 引用接口在spring容器中的标识名称
registry 使用直连方式,不使用注册,值:N/A
-->
<dubbo:service interface="com.dyf.dubbo.service.SomeService"
ref="someService"
registry="N/A"/>
</beans>
spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.dyf.dubbo.service"/>
<mvc:annotation-driven/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-link-provider.xml,classpath:spring-mvc.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
4、配置Tomcat服务器


5、测试
启动link_provider,然后启动link_consumer
6、结论
直连方式没有屏蔽底层实现细节
dubbo官方推荐使用的项目结构如下:
1、服务提供者工程(web工程)
实现接口工程中的业务接口
2、服务消费者工程(web工程)
消费服务提供者提供的业务接口
3、接口工程(java工程)
业务接口和实体类
Dubbo官方推荐使用的项目结构
1、创建项目结构
1、服务提供者工程(web工程)link_provider_01
实现接口工程中的业务接口
2、服务消费者工程(web工程)link_consumer_01
消费服务提供者提供的业务接口
3、接口工程(java工程)interface
业务接口和实体类

2、interface
User.java
package com.dyf.dubbo.model;
import java.io.Serializable;
/**
* 此处需要实现Serializable接口,否则传输时会报错
* @author: dyf
* @date: 2022/9/3 10:13
* @version: 1.0
*/
public class User implements Serializable {
private Integer id;
private Integer age;
public User(Integer id, Integer age) {
this.id = id;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
'}';
}
}
UserService.java
package com.dyf.dubbo.service;
import com.dyf.dubbo.model.User;
/**
* @author: dyf
* @date: 2022/9/3 10:14
* @version: 1.0
*/
public interface UserService {
/**
* hello
* @param msg
* @return
*/
String hello(String msg);
/**
* 根据id获取用户
* @param id
* @return
*/
User getUserById(Integer id);
}
3、link_provider_01
pom.xml
<dependencies>
<!-- Dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- 接口工程 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
UserServiceImpl.java
package com.dyf.dubbo.service.impl;
import com.dyf.dubbo.model.User;
import com.dyf.dubbo.service.UserService;
/**
* @author: dyf
* @date: 2022/9/3 10:21
* @version: 1.0
*/
public class UserServiceImpl implements UserService {
@Override
public String hello(String msg) {
return "hello" + msg;
}
@Override
public User getUserById(Integer id) {
return new User(id,20);
}
}
dubbo-link-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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 声明服务提供者名称,名称唯一 -->
<dubbo:application name="link_provider_01"/>
<!-- 指定协议和端口号,官方推荐为dubbo、20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 暴露服务接口 -->
<dubbo:service interface="com.dyf.dubbo.service.UserService"
ref="userService"
registry="N/A"/>
<bean name="userService" class="com.dyf.dubbo.service.impl.UserServiceImpl"/>
</beans>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-link-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4、link_consumer_01
pom.xml
<dependencies>
<!-- Dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- 接口工程 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
UserController.java
package com.dyf.dubbo.controller;
import com.dyf.dubbo.model.User;
import com.dyf.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author: dyf
* @date: 2022/9/3 16:43
* @version: 1.0
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/hello")
public String hello(Model model) {
String hello = userService.hello("world");
model.addAttribute("hello",hello);
return "hello";
}
@RequestMapping("/user/{id}")
public String user(Model model,
@PathVariable("id") Integer id) {
User user = userService.getUserById(id);
model.addAttribute("user",user);
return "user";
}
}
dubbo-link-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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务消费者名称 -->
<dubbo:application name="link_consumer_01"/>
<!-- 引用远程接口 -->
<dubbo:reference interface="com.dyf.dubbo.service.UserService"
id="userService"
url="dubbo://localhost:20880"
registry="N/A"/>
</beans>
spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.dyf.dubbo.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-link-consumer.xml,classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
hello.jsp
<%--
User: dyf
Date: 2022/9/3
Time: 16:59
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${requestScope.hello}
</body>
</html>
user.jsp
<%--
User: dyf
Date: 2022/9/3
Time: 16:58
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${requestScope.user.id} <br>
${requestScope.user.age}
</body>
</html>
5、测试
配置tomcat服务器如上直连,运行link_provider_01,然后运行link_consumer_01
localhost:8080/hello
localhost_8080/user/1
使用Zookeeper
1、创建项目结构

2、添加依赖
二者需要添加的依赖相同
<dependencies>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<!-- zookeeper注册中心依赖 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>
3、配置文件
zk-provider
dubbo-zk-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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 声明提供者名称 -->
<dubbo:application name="zk-provider"/>
<!-- 指定协议、端口号 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 指定注册中心 -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 暴露服务 -->
<dubbo:service interface="com.dyf.dubbo.service.UserService" ref="userService"/>
<bean id="userService" class="com.dyf.dubbo.service.impl.UserServiceImpl"/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-zk-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
zk-consumer
dubbo-zk-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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 声明消费者名称 -->
<dubbo:application name="zk-consumer"/>
<!-- 指定注册中心 -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 引用远程服务接口 -->
<dubbo:reference id="userService" interface="com.dyf.dubbo.service.UserService"/>
</beans>
spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.dyf.dubbo.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-zk-consumer.xml,classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
interface代码以及其他Java代码与之前的相同
4、测试
启动zookeeper
启动zk-provider
启动zk-consumer
Dubbo的配置
1、配置原则
在服务提供者配置访问参数。因为服务提供者更了解服务的各种参数
2、关闭检查
dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时能及早发现问题,默认check=true。通过“check=false”关闭检查。比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。
例1:关闭某个服务的启动时检查
<dubbo:reference interface="com.dyf.dubbo.service.UserService" check="false"/>
例2:关闭注册中心启动时检查
<dubbo:registry check="false">
默认启动服务时检查注册中心存在并已运行,注册中心不启动会报错
3、重试次数
消费者访问提供者,如果访问失败,则切换重试访问其它服务器,但重试会带来更长延迟。访问时间变长,用户的体验较差。多次重新访问服务器有可能访问成功。可通过retries="2"来设置重试次数(不含第一次)。
<dubbo:service retries="2"> 或 <dubbo:reference retries="2">
4、超时时间
由于网络或服务端不可靠,会导致调用出现一种不确定的中间状态(超时)。为了避免超时导致客户端资源(线程)挂起耗尽,必须设置超时时间。
timeout:调用远程服务超时时间(亳秒)
5、版本号
每个接口都应定义版本号,为后续不兼容升级提供可能。当一个接口有不同的实现,项目早期使用的一个实现类,之 后创建接口的新的实现类。区分不同的接口实现使用version。
<!-- 引用远程服务接口 -->
<dubbo:reference id="userService" interface="com.dyf.dubbo.service.UserService" version="1.0"/>
<dubbo:reference id="newUserService" interface="com.dyf.dubbo.service.UserService" version="2.0"/>
<!-- 暴露服务 -->
<dubbo:service interface="com.dyf.dubbo.service.UserService" ref="userService" version="1.0"/>
<dubbo:service interface="com.dyf.dubbo.service.UserService" ref="newUserService" version="2.0"/>
监控中心
1、什么是监控中心
Dubbo的使用,其实只需要有注册中心,消费者,提供者这三个就可以使用了,但是并不能看到有哪些消费者和提供者,为了更好的调试,发现问题,解决问题,因此引入dubbo-admin。通过dubbo-admin 可以对消费者和提供者进行管理。可以在dubbo 应用部署做动态的调整,服务的管理。
dubbo- admin
图形化的服务管理页面;安装时需要指定注册中心地址,即可从注册中心中获取到所有的提供者/消费者进行配置管理

浙公网安备 33010602011771号