Dubbo(三)——dubbo直连案例

一、创建工程

  1. 首先创建一个空工程,然后分别创建两个module
  2. Maven module创建
    Maven module创建方式, 创建的Module名称分别为001-link-provider002-link-consumer创建方式如下图:
    在这里插入图片描述
  3. 完成maven工程目录结构
    001-link-provider,002-link-consumer 分别创建java resoureces 并创建资源目录
    在这里插入图片描述
  4. 可以安装翻译插件 Translation(自定义)
    使用方式选中单词然后快捷键 ctrl + shift + Y
    在这里插入图片描述
  5. 提供者配置
  • 创建服务SomeService和服务实现类SomeServiceImpl
    在这里插入图片描述
  • someService代码:
public interface SomeService {
    /**
     * Hello
     * @param message
     * @return
     */
    String hello(String message);
}
  • SomeServiceImpl代码:
public class SomeServiceImpl implements SomeService {
    public String hello(String message) {
        //调用数据持久层
        return "hello" + message;
    }
}
  • 将服务进行暴露,需要配置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内部使用的唯一标识-->
     <dubbo:application name="001-link-provider"/>

    <!--指定dubbo的协议名称和端口号
       name 执行协议名称 官方推荐dubbo协议
       port 协议的端口号,默认为20880
    -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--
    暴露服务:dubbo:service
    interface:暴露服务的接口全限定类名
    ref: 引用接口在spring容器中的标识名称
    registry:使用直连方式,不使用注册中心,值就必须写成:N/A
    -->
    <dubbo:service interface="com.bjpowermode.dubbo.service.SomeService" ref="someServiceImpl" registry="N/A"/>
    <!--加载接口实现类-->
    <bean id="someServiceImpl" class="com.bjpowermode.dubbo.service.impl.SomeServiceImpl"/>
</beans>
  • provider中的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.bjpowermode.dubbo</groupId>
  <artifactId>001-link-provider</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
      <!--dubbo依赖-->
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.0</version>
    </dependency>
    <!--spring依赖-->
    <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>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>8083</port>
          <path>/</path>
          <uriEncoding>GBK</uriEncoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

  • 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. 消费者代码配置
    在这里插入图片描述
  • 配置dubbo的消费信息 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内部服务名称的唯一标识-->
    <dubbo:application name="002-link-consumer"/>
    <!--不需要执行协议,调用时已经知道了-->
    <!--
        引用远程接口
        id: 远程接口服务的代理对象名称
        interface:接口的全限定类名
        url:调用远程接口服务的url地址
        registry 直接方式 N/A
    -->
    <dubbo:reference id="someService"
                     interface="com.bjpowermode.dubbo.service.SomeService"
                     url="dubbo://localhost:20880"
                     registry="N/A"
    />
</beans>
  • 配置包扫描以及视图解析器 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.bipowermode.dubbo.web"/>
    <!--注解驱动-->
    <mvc:annotation-driven/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</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">
    <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-link-consumer.xml,classpath:springmvc.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>
  • 配置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.bjpowermode.dubbo</groupId>
  <artifactId>002-link-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

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

  <!--spring依赖-->
  <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.bjpowermode.dubbo</groupId>
      <artifactId>001-link-provider</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
      <dependency>
          <groupId>com.bjpowermode.dubbo</groupId>
          <artifactId>001-link-provider</artifactId>
          <version>1.0-SNAPSHOT</version>
          <scope>compile</scope>
      </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>8084</port>
          <path>/</path>
          <uriEncoding>GBK</uriEncoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
  • 测试服务调用
    需要配置ConsumeController以及创建hello.jsp
    ConsumerController
@Controller
public class SomeController {
   /* @Qualifier(value = "someService")
    @Resource*/
   @Autowired
    private SomeService someService;

    @RequestMapping(value = "/hello")
    public String hello(Model model) {
        //调用远程接口服务
        String hello = someService.hello("world");
        model.addAttribute("hello", hello);
        System.out.println("hello = " + hello);
        return "hello";
    }
}

hello.jsp
在这里插入图片描述

二、修改web配置文件版本号(web.xml中的版本)

  1. create from archetype : 从原型中创建
    org.apace.maven.archetypes:maven-archetype-webapp: : 这个maven仓库中有,和这个名称路径一样

在这里插入图片描述
2. 将maven-archetype-webapp-1.4.jar中的web.xml中的版本号进行修改,这样在新生成module项目时,版本就会改为你所修改的版本
在这里插入图片描述
3. 将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">
</web-app>

三、 配置服务提供者监听器

这个需要在provider中配置(上面配置完毕这里进行讲解

<?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. 怎么知道服务者给我暴露那些接口
  • packaging注释掉,默认打成jar包
  <groupId>com.bjpowermode.dubbo</groupId>
  <artifactId>002-link-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>
 <!-- <packaging>war</packaging>-->
  • pom.xml界面 双击ctrl 输入 mvn 001-link-provider install
  <!--接口-->
    <dependency>
      <groupId>com.bjpowermode.dubbo</groupId>
      <artifactId>001-link-provider</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

在这里插入图片描述
2. pom.xml

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

  <!--spring依赖-->
  <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.bjpowermode.dubbo</groupId>
      <artifactId>001-link-provider</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

五、配置服务消费者核心配置文件

  1. 消费者如何调用接口(如何调用001-link-provider)
<?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="002-link-consumer"/>
    <!--不需要执行协议,调用时已经知道了-->

    <!--
        引用远程接口
        id: 远程接口服务的代理对象名称
        interface:接口的全限定类名
        url:调用远程接口服务的url地址
        registry 直接方式 N/A
    -->
    <dubbo:reference id="someService" 
                     interface="com.bjpowermode.dubbo.service.SomeService"
                     url="dubbo://localhost:20880" 
                     registry="N/A"
    />
    <dubbo:consumer/>
</beans>

六、配置springmvc配置文件

这个需要在consumer中配置(上面配置完毕这里进行讲解

<?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.bipowermode.dubbo.web"/>

    <!--注解驱动-->
    <mvc:annotation-driven/>

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

</beans>

七、配置中央调试器

这个需要在provider中配置(上面配置完毕这里进行讲解

<?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-link-consumer.xml,classpath:springmvc.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>

八、测试

  1. 配置tomcat插件
    comsumer:
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>8084</port>
          <path>/</path>
          <uriEncoding>GBK</uriEncoding>
        </configuration>
      </plugin>
    </plugins>
  </build>

provider

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>8083</port>
          <path>/</path>
          <uriEncoding>GBK</uriEncoding>
        </configuration>
      </plugin>
    </plugins>
  </build>

添加hello.jsp
在这里插入图片描述

九、直连案例存在问题

  1. 暴露出底层impl,没有实际意义

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值