直连方式 dubbo

空~2022年10月9日
  • Dubbo
大约 4 分钟

直连方式 dubbo

点对点的直连项目:消费者直接访问服务提供者,没有注册中心。

消费者必须指定服务提供者的访问地址(url)。

消费者直接通过 url 地址访问固定的服务提供者。这个 url 地址是不变的。

image-20221009114909822

  1. 创建一个空项目
  2. 创建 Module 服务提供者 link-orderservice-provider
  3. 创建 Module 服务消费者 link-main-web

服务提供者

使用 maven 创建一个 web 工程 的 module 并导入相关依赖.

<dependencies>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.3.23</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>dubbo</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.24</version>
  <scope>compile</scope>
</dependency>
</dependencies>

实体类

@Data
public class User implements Serializable {
    private Integer id;
    private String  name;
    private String pwd;

    public User(Integer id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public User() {
    }
}

mapper 类

public class UserMapper {
    public User queryUser(Integer id) {
        return new User(id, "lisi", "111");
    }
}

service 接口

public interface UserService {

    User queryUserById(Integer id);
}

service 实现类

public class UserServiceImpl implements UserService {
    private UserMapper userMapper = new UserMapper();

    @Override
    public User queryUserById(Integer id) {
        return userMapper.queryUser(id);
    }
}

DubboProvideApplication.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内部使用的唯一标识
    -->
    <dubbo:application name="dubbo-001"/>

    <!--
    name: 指定协议名称
    port: 指定协议端口号, 默认 20880
    -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--
    暴露服务接口: dubbo:service
    interface: 暴露服务接口的全限定类名
    ref: 接口引用的实现类在spring容器中标识
    registry: 如果不使用注册中心, 赋值为 N/A
    -->
    <dubbo:service interface="com.demo.service.UserService" ref="userService" registry="N/A"/>
    <bean id="userService" class="com.demo.service.impl.UserServiceImpl"/>
</beans>

web.xml

web-app 的命名空间不对可能导致 jstl 结果无法解析.

<?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_3_1.xsd"
         version="3.1">
    <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:SpringApplication.xml,classpath:DubboLinkApplication.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

配置 tomcat 时修改下端口号

image-20221009121925206

使用 maven 打包 服务提供者

image-20221009120541442

可在 maven 本地仓库查看

image-20221009120732902

服务消费者

再创建一个 web 工程 module, 导入相关依赖.

<dependencies>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.3.23</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>dubbo</artifactId>
  <version>2.6.2</version>
</dependency>
</dependencies>

将服务提供者导入.

<dependency>
  <groupId>org.example</groupId>
  <artifactId>link-orderservice-provider</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

controller 层

@Controller
public class UserController {

    @Resource
    private UserService userServiceImpl;

    @RequestMapping("/user")
    public String QueryUser(Model model, Integer id) {
        model.addAttribute("user", userServiceImpl.queryUserById(id));
        return "index";
    }
}

dubbo 配置

DubboLinkApplication.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-002"/>
    <!--
    引用远程服务接口:
    id: 远程服务接口对象名称
    interface: 调用远程接口的全限定类名
    url: 范围服务接口地址
    registry: 不使用注册中心, 值为: N/A
    -->
    <dubbo:reference id="userService" interface="com.demo.service.UserService" url="dubbo://localhost:20880"
                     registry="N/A"/>
</beans>

配置视图解析器

SpringApplication.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
       https://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.demo.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

web-app 的命名空间不对可能导致 jstl 结果无法解析.

<?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_3_1.xsd"
         version="3.1">
    <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:SpringApplication.xml,classpath:DubboLinkApplication.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<div>用户标识:${user.id}</div>
<div>用户名称:${user.name}</div>
</body>
</html>

启动项目尝试访问

localhost:9090/user?id=1

image-20221009124238521