Dubbo实例~直连的方式

2.4 Dubbo实例~直连的方式

提供者和消费者直接连接,不需要注册中心

2.4.1 服务的提供者

步骤:
1.创建一个maven web工程:服务的提供者

2.创建一个实体bean查询的结果
3.提供一个服务接口:xxXX
4.实现这个服务接口:xxxxImpl

5.配置dubbo服务提供者的核心配置文件
a. 声明dubbo服务提供者的名称:保证唯一
b. 声明dubbo使用的协议和端口号
c. 暴露服务,使用直连方式
6.添加监听器

  1. 配置相关依赖(创建一个maven web工程:服务的提供者

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.guo.dubbo</groupId>
  <artifactId>001-link-userservice-provider</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>
  <!--让提供者项目打成jar包
  双击Ctrl 输入命令:mvn 001-link-userservice-provider install-->

  <dependencies>
    <!--引入spring依赖 spring-context上下文  spring-webmvc  -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.20</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.20</version>
    </dependency>
    <!--引入dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>

  </dependencies>
  <build>
    <plugins>
      <!--JDK 1.8 编译插件-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    <defaultGoal>compile</defaultGoal>
  </build>
</project>
  1. 服务的提供者:创建一个实体bean查询的结果,提供一个服务接口:xxXX,实现这个服务接口:xxxxImpl

在这里插入图片描述

  1. 配置dubbo服务提供者的核心配置文件
    a. 声明dubbo服务提供者的名称:保证唯一
    b. 声明dubbo使用的协议和端口号
    c. 暴露服务,使用直连方式

src/main/resources/dubbo-userservice-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="001-link-userservice-provider"/>
    <!--访问服务协议的名称及端口号,dubbo官网推荐使用的是dubbo协议20880-->
    <!--name:指定协议的名称
        port:指定协议的端口号(默认是20880-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--暴露服务接口 dubbo:service
        interface:暴露的服务接口的全限定类名
        ref:接口引用的实现类在spring容器中的标识
        registry:如果不使用注册中心,则值为:N/A  (直连)
    -->
    <dubbo:service interface="com.guo.dubbo.service.UserService" ref="userService" registry="N/A"/>
    <!--将接口的实现类加载到spring容器中-->
    <bean id="userService" class="com.guo.dubbo.service.impl.UserSericeImpl"/>
</beans>
  1. 添加监听器

src/main/webapp/WEB-INF/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-userservice-provider.xml</param-value>
    </context-param>

    <!--监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
  1. 创建一个Tomcat
    在这里插入图片描述

2.4.2 服务的消费者

思路:
1.创建一个maven web工程:服务的消费者
2.配置pom文件 :添加需要的依赖(spring,dubbo)
3.设置dubbo的核心配置文件()
4.编写controller
5.配置中央调度器(servlet:DispatcherServlet)

  1. 配置pom文件 :添加需要的依赖(spring,dubbo)

服务消费者需要引入服务提供者的业务接口,调用业务接口所提供的方法,因此,我们需要在服务提供者的项目上打一下jar包,发布到本地仓库,这样在服务消费者直接可以引入服务消费者的相关依赖

<?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.guo.dubbo</groupId>
  <artifactId>002-link-consumer</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>
  <dependencies>
    <!--spring依赖  spring-context spring-webmvc-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.9</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.9</version>
    </dependency>
    <!--dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>

    <!--引入服务提供者的依赖,,这里需要先在服务的提供者项目打一下jar包,这里引用那个jar包-->
      <dependency>
          <groupId>com.guo.dubbo</groupId>
          <artifactId>001-link-userservice-provider</artifactId>
          <version>1.0.0</version>
      </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
          <configuration>
              <source>1.8</source>
              <target>1.8</target>
          </configuration>
      </plugin>
    </plugins>
  </build>
</project>
  1. 设置dubbo的核心配置文件()

src/main/resources/dubbo-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="002-link-consumer"/>
    <!--引用远程服务接口:
        id:远程服务接口对象的名称
        interface:调用远程接口的全限定类名
        url:访问服务接口的地址
        registry:不使用注册中心,直连值为:N/A-->
    <!--服务消费者dubbo的核心配置文件-->
    <dubbo:reference id="userService"
                     interface="com.guo.dubbo.service.UserService"
                     url="dubbo://localhost:20880"
                     registry="N/A"/>
</beans>

src/main/resources/aplication.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-4.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <!--扫描组件-->
    <context:component-scan base-package="com.guo.dubbo.web"/>
    <!--配置注解驱动-->
    <mvc:annotation-driven/>

    <!--前端页面——视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--视图解析器-->
</beans>
  1. 控制层
    UserController.java
package com.guo.dubbo.web;

import com.guo.dubbo.model.User;
import com.guo.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.GetMapping;

import javax.jws.WebParam;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/user")
    public String userDetail(Model model,Integer id){
        User user = userService.queryUserById(id);
        model.addAttribute("user",user);
        return "index";
    }
}
  1. 配置中央调度器servlet
    src/main/webapp/WEB-INF/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:aplication.xml,classpath:dubbo-consumer.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  1. jsp页面展示数据
    src/main/webapp/index.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户详情</title>
</head>
<body>
	<h1>用户详情</h1>
	<div>用户标识:${user.id}</div>
	<div>用户名称:${user.name}</div>
	<div>用户年龄:${user.age}</div>
</body>
</html>
  1. 创建一个Tomcat服务

在这里插入图片描述

2.4.1 服务的提供者、消费者两个服务跑起来

在这里插入图片描述服务成功启动,去访问服务消费者的控制层
http://localhost:8080/user?id=1001
在这里插入图片描述

BUG:

  1. xml 中引入命名空间容易出错
  2. 实体类的序列化实现序列化接口

dubbo官方推荐必须有一个接口工程,它就是一个maven java工程要求接口工程里存放到内容如下:
1.对外暴露的服务接口(service接口)
2.实体bean对象

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Spring Boot 中使用 Dubbo 直连,可以通过在 application.properties 或 application.yml 中配置 Dubbo 的连接信息来实现。 1. 配置 application.properties 在 application.properties 文件中添加以下配置: ``` # Dubbo 直连配置 dubbo.registry.address=N/A dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.provider.timeout=30000 dubbo.consumer.timeout=30000 dubbo.consumer.check=false dubbo.reference.filter=tracing dubbo.reference.filter=consumerContextFilter dubbo.consumer.retries=0 dubbo.consumer.loadbalance=random dubbo.proxy=javassist ``` 其中,dubbo.registry.address 设置为 N/A 表示不使用注册中心,而是直接跟指定的 Dubbo 服务提供者进行连接。dubbo.protocol.name 和 dubbo.protocol.port 分别指定了 Dubbo 的协议和端口,dubbo.provider.timeout 和 dubbo.consumer.timeout 分别指定了 Dubbo 服务提供者和服务消费者的超时时间,dubbo.consumer.check 设置为 false 表示关闭 Dubbo 的服务调用检查功能,dubbo.reference.filter 指定了 Dubbo 的服务引用过滤器,dubbo.consumer.retries 设置为 0 表示关闭 Dubbo 的重试功能,dubbo.consumer.loadbalance 设置为 random 表示使用随机负载均衡算法,dubbo.proxy 设置为 javassist 表示使用 Javassist 代理方式。 2. 配置 application.yml 在 application.yml 文件中添加以下配置: ``` dubbo: registry: address: N/A protocol: name: dubbo port: 20880 provider: timeout: 30000 consumer: timeout: 30000 check: false retries: 0 loadbalance: random filter: - tracing - consumerContextFilter proxy: javassist ``` 与 application.properties 中的配置相同,只是使用了 YAML 格式。 3. 使用 @DubboReference 注解 在需要调用 Dubbo 服务的代码处,使用 @DubboReference 注解引入 Dubbo 的服务接口,代码示例如下: ``` @RestController public class DemoController { @DubboReference(url = "dubbo://127.0.0.1:20880") private DemoService demoService; @GetMapping("/hello") public String sayHello(@RequestParam String name) { return demoService.sayHello(name); } } ``` 其中,@DubboReference 注解的 url 属性指定了 Dubbo 的服务地址,可以直接指定 IP 地址和端口号。使用 @DubboReference 注解的方式不需要在 application.properties 或 application.yml 中配置 Dubbo 的连接信息。 需要注意的是,使用 Dubbo 直连方式调用服务时,需要保证服务提供者和消费者的 Dubbo 版本和协议相同,否则可能会出现调用失败或者异常的问题。同时,在生产环境下不推荐使用 Dubbo 直连方式,建议使用注册中心进行服务管理和发现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

848698119

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值