Dubbo入门以及搭建简单的demo项目

1、dubbo基本架构

在这里插入图片描述

  1. 服务提供者(Provider):暴露服务的服务提供方,服务提供者在 启动时,向注册中心注册自己提供的服务。
  2. 服务消费者(Consumer): 调用远程服务的服务消费方,服务消 费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如 果调用失败,再选另一台调用。
  3. 注册中心(Registry):注册中心返回服务提供者地址列表给消费 者,如果有变更,注册中心将基于长连接推送变更数据给消费者
  4. 监控中心(Monitor):服务消费者和提供者,在内存中累计调用 次数和调用时间,定时每分钟发送一次统计数据到监控中心

2、直连方式 dubbo

2.1 直连方式 dubbo介绍

消费者直接访问服务提供者,没有注册中心。消费者直接通过 url 地址访问固定的服务提供者。这个 url 地址是不变的。
在这里插入图片描述

2.2 demo实现直连方式 dubbo

服务提供者,服务消费者都是 web项目。
在这里插入图片描述

2.2.1 创建服务提供者项目

dubbo-provider(web项目)

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.wen</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
        </dependency>
        <!--dubbo依赖,必须加-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
        </dependency>
    </dependencies>

</project>

创建产品实体类:Product.java

package com.wen.model;

import java.io.Serializable;

public class Product implements Serializable {
    private String name;
    private String id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                '}';
    }
}

新建产品服务接口:ProductService

package com.wen.service;

import com.wen.model.Product;

public interface ProductService {
   Product getProductById(String id);
}

新建产品服务接口实现类:ProductServiceImpl.java

package com.wen.service.impl;

import com.wen.model.Product;
import com.wen.service.ProductService;

public class ProductServiceImpl implements ProductService {
    @Override
    public Product getProductById(String id) {
        Product product = new Product();
        product.setId(id);
        product.setName("iphone 13");
        return product;
    }
}

创建dubbo的配置文件:applicationContext.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="provider"/>
    <!--服务协议名称和端口号,20880是dubbo服务默认端口号-->
    <dubbo:protocol name="dubbo" port="20880"/>
    
    <!--暴露服务接口,供消费者使用
        interface: 暴露服务的接口全限定名
        ref: 暴露接口的实现类bean的标识id
        registry: 直连的方式,N/A表示不使用注册中心,注意千万不要写成register
    -->
    <dubbo:service interface="com.wen.service.ProductService" ref="productService" registry="N/A" />

    <!--将暴露的接口实现类加载到spring容器-->
    <bean id="productService" class="com.wen.service.impl.ProductServiceImpl"/>

</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">

    <!--服务器一启动,加载spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

2.2.2 创建服务消费者项目

dubbo-consumer(web项目)

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 <?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.wen</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
        </dependency>
        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
        </dependency>
        <!--服务提供者的gav-->
        <dependency>
            <groupId>com.wen</groupId>
            <artifactId>dubbo-provider</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--json依赖-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

</project>

创建控制器:ProductController.java

package com.wen.web.controller;

import com.wen.model.Product;
import com.wen.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("product/getById")
    @ResponseBody
    public Product getProductById(String id){
        System.out.println(productService);
        return productService.getProductById(id);
    }
}

创建dubbo的配置文件:applicationContext.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:dubbo="http://code.alibabatech.com/schema/dubbo" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.wen.web"/>
    <mvc:annotation-driven/>

    <dubbo:application name="consumer"/>
    <!--使用远程服务
        id:远程服务代理对象名称
        interface:远程接口全限定类名
        url:访问服务提供者的地址
    -->
    <dubbo:reference id="productService" interface="com.wen.service.ProductService" url="dubbo://localhost:20880"/>

</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:applicationContext.xml</param-value>
    </context-param>
    <!--SpringMVC的核心处理器-->
    <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:applicationContext.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>

2.2.3 测试

先启动服务提供者,再启动服务消费者,
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值