dubbo(四)——Dubbo官方推荐的项目结构

一、dubbo官方推荐使用的项目结构如下:

  1. 服务提供者工程
    实现接口工程中的业务接口 (web工程)
  2. 服务消费者工程
    消费服务提供者提供的业务接口 (web工程)
  3. 接口工程
    业务接口和实体类 (java工程)

二、官方推荐使用的项目结构实战(直联方式)

  1. 直联方式项目结构图
    001-link-provider表示服务提供者
    002-link-consumer表示服务消费者
    003-interface接口工程
    在这里插入图片描述

三、接口

  1. 项目结构图
    在这里插入图片描述
  2. DubboService
public interface DubboService {

    public String sayDubbo();
}

  1. 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.cjw.dubbo</groupId>
  <artifactId>003-interface</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
</project>

四、提供者代码

  1. 项目结构图
    在这里插入图片描述
  2. DubboServiceImpl
public class DubboServiceImpl implements DubboService {
    public String sayDubbo() {
        return "001-link-provider-sayDubbo";
    }
}
  1. dubbo-link-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:application name="001-link-provider"/>
<!--指定协议名称以及端口号-->
    <dubbo:protocol name="dubbo" port="20880"/>
<!--暴露服务-->
    <dubbo:service interface="com.cjw.dubbo.service.DubboService" ref="dubboServiceImpl" registry="N/A"/>
<!--接口实现类-->
    <bean id="dubboServiceImpl" class="com.cjw.dubbo.service.imp.DubboServiceImpl"/>
</beans>
  1. 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-link-provider.xml</param-value>
   </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

  1. 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.cjw.dubbo</groupId>
  <artifactId>001-link-provider</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.16.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.16.RELEASE</version>
    </dependency>
    <!--接口工程-->
    <dependency>
      <groupId>com.cjw.dubbo</groupId>
      <artifactId>003-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

五、服务消费者

  1. 项目结构图
    在这里插入图片描述
  2. 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.cjw.dubbo</groupId>
  <artifactId>002-link-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
   <dependencies>
     <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>dubbo</artifactId>
         <version>2.6.0</version>
     </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.3.16.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>4.3.16.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>com.cjw.dubbo</groupId>
           <artifactId>003-interface</artifactId>
           <version>1.0-SNAPSHOT</version>
       </dependency>
   </dependencies>
</project>
  1. 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:springmvc.xml,classpath:dubbo-link-consumer.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

  1. DubboController
@Controller
public class DubboController {
    @Autowired
    private DubboService dubboService;
    @RequestMapping("/say")
    public String sayDubbo(Model model){
        String sayDubbo = dubboService.sayDubbo();
        System.out.println("sayDubbo = " + sayDubbo);
        model.addAttribute("sayDubbo", sayDubbo);
        return "sayDubbo";
    }
}
  1. dubbo-link-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:调用远程接口服务的url地址
        registry 直联方式 N/A
    -->
    <dubbo:reference id="dubboService" interface="com.cjw.dubbo.service.DubboService"
                     url="dubbo://localhost:20880"
         registry="N/A"/>
 <!--
 缺少id 或者id错误(意思是和Controller中使用的service对应不上)
 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
 No qualifying bean of type 'com.cjw.dubbo.service.DubboService' available: expected at
 least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org
 -->

</beans>
  1. 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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.cjw.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. sayDubbo.jsp
    <title>Title</title>
    <hr/>
    测试dubbo直联---》${sayDubbo}
    <hr/>

六、测试结果

在这里插入图片描述

七、注意

  • idea 配置tomcat
    一个tomcat就可以 ,只需要在不同的Tomcat Server中需要修改HTTP PortJMX port
    在这里插入图片描述
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dubbo是一个分布式服务框架,可以帮助我们快速构建高性能、可扩展的分布式应用。下面是一个简单的Dubbo项目搭建的步骤: 1. 创建Maven项目:首先,在你的IDE中创建一个新的Maven项目。 2. 添加Dubbo依赖:在项目的pom.xml文件中添加Dubbo的依赖。示例代码如下: ```xml <dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.12</version> </dependency> </dependencies> ``` 3. 配置Dubbo服务提供者:创建一个Java类作为Dubbo服务提供者,并在类上添加Dubbo的注解。示例代码如下: ```java import org.apache.dubbo.config.annotation.DubboService; @DubboService public class DemoServiceImpl implements DemoService { // 实现接口方法 } ``` 4. 配置Dubbo服务消费者:创建一个Java类作为Dubbo服务消费者,并在类上添加Dubbo的注解。示例代码如下: ```java import org.apache.dubbo.config.annotation.DubboReference; public class DemoConsumer { @DubboReference private DemoService demoService; // 调用远程服务方法 } ``` 5. 配置Dubbo注册中心:在项目的配置文件中配置Dubbo的注册中心,比如Zookeeper。示例配置如下: ```properties # dubbo.registry.address指定注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 ``` 6. 运行项目:启动Dubbo服务提供者和消费者,通过Dubbo的注册中心进行服务的注册和发现。 以上是一个简单的Dubbo项目搭建的步骤,具体可以根据你的实际需求进行相应的配置和调整。希望对你有所帮助!如有更多问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值