一文快速回顾dubbo的详细项目结构

1、dubbo结构

dubbo

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

2、项目结构

接口工程就是普通的mavan java工程,对于服务提供者和消费者,在xml文件中都需要配置的是:

<!--声明dubbo服务提供者的名称:保证唯一性-->
<dubbo:application name="dubbo_007_zk_userService_provider"/>

<!--指出注册中心,直连的话,registry=N/A-->
<dubbo:registry address="zookeeper://192.168.154.128:2181"/>

2.1接口工程

接口工程用于存放实体bean(必须要进行序列化操作)和业务接口,一个普通的maven工程就行了,springboot中也是一样。
在这里插入图片描述

2.2 服务提供者

对上述接口进行实现,并编写xml文件进行dubbo配置管理
1、pom中需要添加接口工程和注册中心zookeeper(curator-framework)的依赖。
2、在xml中需要配置协议和端口号<dubbo:protocol name="dubbo" port="20880"/>
3、xml中指定注册中心的地址和端口号
<dubbo:registry address="zookeeper://192.168.154.128:2181"/>
4、xml暴露服务接口,并加载接口的实现类:

<dubbo:service interface="cn.edu.uestc.dubbo.service.UserService" ref="userServiceImpl"/>
<bean id="userServiceImpl" class="cn.edu.uestc.dubbo.service.impl.UserServiceImpl"/>

在这里插入图片描述

2.3消费者

这里编写springmvc的程序(Controller)
消费者的xml中最主要就是 引用远程接口服务(想着reference)。
1、声明dubbo消费者名称,保证唯一性<dubbo:application name="008_zk_consumer"/>
2、指定出注册中心<dubbo:registry address="zookeeper://192.168.154.128:2181"/>
3、引用远程接口服务
4、pom中需要引入接口工程和注册中心(curator-framework)的依赖
<dubbo:reference interface="cn.edu.uestc.dubbo.service.UserService" id="userService"/>

在这里插入图片描述

3、具体代码分析

3.1、服务提供者

暴露服务的服务提供方, 服务提供者在启动时,向注册中心注册自己提供的服务。

pom文件

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.edu.uestc.dubbo</groupId>
    <artifactId>dubbo_009_zk_multi_provider</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>

    <dependencies>
        <!--Spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>


        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>

        <!--接口工程-->
        <dependency>
            <groupId>cn.edu.uestc.dubbo</groupId>
            <artifactId>dubbo_006_zk_interface</artifactId>
            <version>1.0</version>
        </dependency>

        <!--注册中心依赖-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <!--JDK11编译插件-->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

接口的实现类

import cn.edu.uestc.dubbo.pojo.User;
import cn.edu.uestc.dubbo.service.UserService;

public class UserServiceImpl1 implements UserService {
    
    
    @Override
    public User selectUserById(Integer id, String name) {
    
    

        return new User(id,name+"impl1",13);
    }
    @Override
    public Integer selectUserCount() {
    
    
        return 25;
    }
}

配置文件

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

    <!--声明dubbo的协议和端口号-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--
        使用注册中心
        如果使用linux,需要在linux中进行ifconfig,将localhost改为对应的ip地址
    -->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--不管一个接口是否有多个实现类,只要服务提供者 服务接口服务 的时候指定了版本号,那做为消费者引用远程接口服务的时候就必须指定版本号-->
    <dubbo:service interface="cn.edu.uestc.dubbo.service.UserService" ref="userServiceImpl1" version="1.0" timeout="15000"/>
    <dubbo:service interface="cn.edu.uestc.dubbo.service.UserService" ref="userServiceImpl2" version="2.0"/>

    <bean name="userServiceImpl1" class="cn.edu.uestc.dubbo.service.impl.UserServiceImpl1"/>
    <bean name="userServiceImpl2" class="cn.edu.uestc.dubbo.service.impl.UserServiceImpl2"/>

</beans>

web.xml文件注册spring容器,并指定对应的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_multi_provider.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

3.2、消费者

调用远程服务的服务消费方, 服务消费者在启动时,向注册中心订阅自己所需的服务, 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

3.2.1 pom文件

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.edu.uestc.dubbo</groupId>
    <artifactId>dubbo_010_zk_multi_consumer</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <dependencies>
        <!--Spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>


        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>

        <!--接口工程-->
        <dependency>
            <groupId>cn.edu.uestc.dubbo</groupId>
            <artifactId>dubbo_006_zk_interface</artifactId>
            <version>1.0</version>
        </dependency>

        <!--注册中心依赖-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <!--JDK1.8编译插件-->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.2.2 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: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="cn.edu.uestc.dubbo.controller"/>

    <!--配置注解驱动,注意选择mvc的,不要选择cache的-->
    <mvc:annotation-driven/>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

3.2.3 dubbo_multi_consumer.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"
       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="010_zk_multi_consumer"/>

    <!--指定注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--引用远程接口服务-->
    <dubbo:reference id="userService1" interface="cn.edu.uestc.dubbo.service.UserService" version="1.0"/>
    <dubbo:reference id="userService2" interface="cn.edu.uestc.dubbo.service.UserService" version="2.0"/>

</beans>

3.2.4 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_multi_consumer.xml,classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

3.2.5 controller

package cn.edu.uestc.dubbo.controller;


import cn.edu.uestc.dubbo.pojo.User;
import cn.edu.uestc.dubbo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;

@Controller
public class UserController {
    
    

    @Resource
    private UserService userService1; //这里的名字和dubbo 的xml中对应的版本号的名字关联起来
    @Resource
    private UserService userService2;//这里的名字和dubbo 的xml中对应的版本号的名字关联起来

    @RequestMapping("/impl1")
    public String detail1(Integer id, String name, Model model){
    
    
        User user = userService1.selectUserById(id, name);
        model.addAttribute("user",user);
        return "detail";
    }

    @RequestMapping("/impl2")
    public String detail2(Integer id,String name, Model model){
    
    
        User user = userService2.selectUserById(id, name);
        model.addAttribute("user",user);
        return "detail";
    }
}

猜你喜欢

转载自blog.csdn.net/YiGeiGiaoGiao/article/details/128459464