Dubbo 基础练习 笔记

6 篇文章 0 订阅

Dubbo是阿里巴巴开源的一个高性能、轻量级的Java RPC框架

dubbo提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案

官网 http://dubbo.apache.org

dubbo的组成角色

节点角色说明: .
●Provider: 暴露服务的服务提供方
●Contahier: 服务运行容器
●Consumer: 调用远程服务的服务消费方
●Registry: 服务注册与发现的注册中心
●Monitor:统计服务的调用次数和调用时间的监控中心

zookeeper是 dubbo中 Registry注册表 的 注册中心

Dubbo的实现步骤

1.创建服务提供者Provider模块
2.创建服务消费者Consumer模块
3.在服务提供者模块编写UserServicelmpl提供服务
4.在服务消费者中的UserController远程调用
5.UserServicelmpl提供的服务
6.分别启动两个服务,测试

在分布式中 模块是可以独立启动,独立对外提供服务的。如果不能独立启动,与对外提供服务就不是分布式。

dubbo-service模块

实现分布式架构 也要在service模块创建web,并且打包方式设置成war,在pom中写出 tomcat坐标。在实现类中 用@Reference远程注解注入接口对象

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.itheima</groupId>
    <artifactId>dubbo-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--在service层的pom将打包方式 设置成war service成就能对外提供服务了-->
    <packaging>war</packaging>

    <properties>
        <spring.version>5.1.9.RELEASE</spring.version>
        <dubbo.version>2.7.4.1</dubbo.version>
        <zookeeper.version>4.0.0</zookeeper.version>

    </properties>

    <dependencies>
        <!-- servlet3.0规范的坐标 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--spring的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--springmvc的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>



        <!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>


        <!--公共接口模块依赖-->
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <!--service不用启动tomcat 所以不用导入tomcat插件-->
<!--要使service层实现分布式架构,独立启动也得在service的pom里
写出tomcat插件,但端口号要改变,不能与其他层的端口号相同
 以示区分-->
    <build>
        <plugins>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>9000</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

service实现类

//@Service这个是spring的注解
//使service层独立对外提供服务,要使用 dubbo里的Service注解
//两个注解导包是不一样的
/*
dubbo的service注解是将类提供的方法,对外发布到 需要访问的
路径,端口,ip地址 以及注册到注册中里
*/

@Service
public class UserviceImpl implements UserService {


    public String sayHello() {
        return "洛天依";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


</web-app>

dubbo-service applicationContext.xml
这里空间名称 要引入dubbo的

<?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" xmlns:context="http://www.springframework.org/schema/context"
	   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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--
	<context:component-scan base-package="com.itheima.service"/>
-->

<!--在spring配置文件中 配置dubbo-->
	<!--配置项目名称,项目的名称必须唯一不能重复-->
	<dubbo:application name="dubbo-service"/>
	<!--配置zookeeper注册中心的ip地址-->
	<!--2181是zookeeper注册中心的默认端口号-->
	<dubbo:registry address="zookeeper://192.168.23.130:2181"/>
<!--配置 dubbo的包扫描,这里就不用spring的包扫描配置-->
	<dubbo:annotation package="com.itheima.service.impl"/>

</beans>

dubbo-controller模块

<?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.itheima</groupId>
    <artifactId>dubbo-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--web项目必须写出打包方式为 war-->
    <packaging>war</packaging>

    <properties>
        <spring.version>5.1.9.RELEASE</spring.version>
        <dubbo.version>2.7.4.1</dubbo.version>
        <zookeeper.version>4.0.0</zookeeper.version>

    </properties>

    <dependencies>
        <!-- servlet3.0规范的坐标 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--spring的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--springmvc的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>



        <!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>


        <!--公共接口模块-->
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>


        <!--依赖service-->
        <!--<dependency>-->
            <!--<groupId>com.itheima</groupId>-->
            <!--<artifactId>dubbo-service</artifactId>-->
            <!--<version>1.0-SNAPSHOT</version>-->
        <!--</dependency>-->


    </dependencies>


    <build>
        <plugins>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>8000</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

controller 实现类
dubbo实现模块独立,用远程注入的注解

@Reference注解的作用
  1. 从zookeeper注册中心获取userService的访问url
  2. 进行远程调用RPC
  3. 将结果封装为一个代理对象。给变量赋值
@RestController
@RequestMapping("/user")
public class UserController {

    //输入service层接口
   // @Autowired本地注入
    /*dubbo实现模块独立,用远程注入的注解

    @Reference注解的作用
  1. 从zookeeper注册中心获取userService的访问url
  2. 进行远程调用RPC
  3. 将结果封装为一个代理对象。给变量赋值

     */
    @Reference//远程注入
    private UserService userService;


    @RequestMapping("/sayHello")
    public String sayHello(){
        return "初音未来";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <!--在web.xml中要导入 spring 与 springmvc的配置文件
     就是 整合 spring与springmvc-->
		 
	<!-- spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!--在还没有依赖service时,这里会爆红-->
        <param-value>classpath*:spring/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
		 
	<!-- Springmvc -->	 
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


</web-app>

dubbo-web springmvc.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


    <mvc:annotation-driven/>

    <context:component-scan base-package="com.itheima.controller"/>

<!--在 web层的springmvc配置文件里 也要配置dubbo-->
    <!--配置项目名称,项目的名称必须唯一不能重复-->
    <dubbo:application name="dubbo-service">
        <!--出现端口冲突在这里修改,dubbo的端口-->
     <dubbo:parameter key="qos.port" value="33333"/>
    </dubbo:application>
    <!--配置zookeeper注册中心的ip地址-->
    <!--2181是zookeeper注册中心的默认端口号-->
    <dubbo:registry address="zookeeper://192.168.23.130:2181"/>
    <!--配置 dubbo的包扫描,这里就不用spring的包扫描配置-->
    <dubbo:annotation package="com.itheima.service.impl"/>

</beans>

将接口 用一个独立的模块 进行抽取,提高接口共用性。
记得在需要使用此模块中接口的模块中,在使用的模块pom里写出公共接口模块的依赖坐标

 <!--公共接口模块-->
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

创建模块 dubbo-interface

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


</project>

公共模块的接口 示列

public interface UserService {

    /* 这个模块是将接口抽取出来提高接口的共用性,
      将来开发中会有很多接口,每人个写的名称、数据类型等
      一定不一样。这就会导致不一致产生的错误问题。
      创建一个独立的模块来专门写接口,将接口做成共性的接口,
      web层 与 service层 都去对这个共性的接口模块注入依赖
     这样两个模块就能使用这里面的公共接口
      */
    public String sayHello();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

普希托夫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值