Dubbo笔记一 具体实现代码基于SpringMVC

开发dubbo程序

1.准备环境

(1)linux中安装注册中心zookeeper
a. linux中安装jdk

下载jdk-8u171-linux-x64.rpm*斜体样式*
安装 rpm -ivh jdk-8u171-linux-x64.rpm,会自动安装到usr目录中的java中,
通过pwd命令,查看jdk安装路径:/usr/java/jdk1.8.0_171-amd64

配置环境变量:

vi /etc/profile,在文件最后追加:
export JAVA_HOME=/usr/java/jdk1.8.0_171-amd64
export CLASSPATH=$JAVA_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$PATH

刷新环境变量

source /etc/profile

b.安装zookeeper

下载zookeeper,
解压tar -zxvf zookeeper-3.4.12.tar.gz
重命名zookeeper的配置文件:
在conf目录中执行:  mv zoo_sample.cfg zoo.cfg

在zoo.cfg中:

可以发现 zookeeper的端口号是 clientPort=2181
设置zookeeper存放数据的目录:
dataDir=/apps/zookeeper-3.4.12/data
启动zookeeper:
[root@bd1 zookeeper-3.4.12]#
关闭zookeeper
bin/zkServer.sh stop
查看zookeeper状态
bin/zkServer.sh status
启动zookeeper
bin/zkServer.sh start

服务方代码:

pom.xml文件所需要引入的依赖
<!-- dubbo组件 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.10</version>
</dependency>
 
<!-- zookeeper -->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.12</version>
</dependency>
<!-- zookeeper客户端 -->
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.21.0-GA</version>
</dependency>
  </dependencies>

服务方可以提供哪些服务:

接口及实现类:(具体的服务)
public interface StudentServer {
public String server(String name);//zs
}
 
@Service//阿里巴巴提供的@Service注解
public class StudentServerImpl  implements StudentServer{
 
public String server(String name) {
return "server:" +name;
}
 
}

配置工作:

继承spring:web.xml
<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" id="WebApp_ID" version="2.5">
 <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>
配置spring: 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"
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-4.3.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--
<bean id="" class="org.students.server.impl.StudentServerImpl"></bean>
-->
<!-- 配置dubbo的应用名称 -->

<dubbo:application name="students-server" />
<!-- 配置注册中心地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.2.128:2181"  />

<!-- 配置dubbo扫描包  :将@Service所在包 放入 dubbo扫描中,供后续 dubbo在rpc时使用-->
<dubbo:annotation package="org.students.server.impl" />

<!-- 将@Service所在包 放入springIOC容器中,供后续 依赖注入时使用 -->
<context:component-scan base-package="org.student.service.impl"></context:component-scan>

</beans>

消费方代码:

a.引入依赖(jar)

pom.xml (与服务方pom.xml一致,改端口号;并设置客户端自己的gav)

b.补齐web工程需要的 WEB-INF/web.xml

<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" id="WebApp_ID" version="2.5">
 
  <!-- 解决post乱码   -->
  <filter>
          <filter-name>CharacterEncodingfilter</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          <init-param>
                  <param-name>encoding</param-name>
                  <param-value>UTF-8</param-value>
          </init-param>
                  <init-param>
                  <param-name>foreEncoding</param-name>
                  <param-value>UTF-8</param-value>
          </init-param>
  </filter>
 
  <filter-mapping>
          <filter-name>CharacterEncodingfilter</filter-name>
<url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <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</param-value>                  
          </init-param>
  </servlet>
 
  <servlet-mapping>
          <servlet-name>dispatcherServlet</servlet-name>
          <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

c.配置springmvc(通过springmvc 来访问 提供方)

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<mvc:annotation-driven>
<!-- 此配置的目的:将Controller中的内容 直接打印到 浏览器中 -->
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
 
 
 
 
 
 
 
<!-- 配置dubbo的应用名称 -->
<dubbo:application name="students-consumer"/>
<!-- 配置注册中心地址 -->
<dubbo:registry address="zookeeper://192.168.2.128:2181" />
 
 
 
 
<!-- 配置dubbo扫描包 -->
<dubbo:annotation  package="org.controller"/>
<!-- 将控制器@Controller所在包 加入IOC容器 -->
<context:component-scan base-package="org.controller"></context:component-scan>
 
</beans>

编写控制器代码:

用于访问 服务方提供的服务代码
package org.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.students.server.StudentServer;
 
import com.alibaba.dubbo.config.annotation.Reference;
//@Controller
//@ResponseBody
@RestController
@RequestMapping("controller")
public class StudentController {
 
@Reference
private StudentServer stuServer ;
 
@RequestMapping("rpcSerer")
public String rpcSerer() {
String result = stuServer.server("zs") ;
return result ;//将结果显示在控制台
}
}

监听器
安装监听器(存在于dubbo-admin;而dubbo-admin又存在于incubator-dubbo-ops
之中)

因此要使用监听器,必须下载incubator-dubbo-ops,但是最新版的incubator-dubbo-ops是在spring boot中使用。

如果要使用 旧的web版,则需要回退版本。但是陈旧版中 ,当前时间该版本 不完整(没有提供完整的maven依赖),因此无法使用。
只能在 历史提交记录中 寻找一个可用的版本(Commits on May 18, 2018 )

将下载好的dubbo-admin源代码 进行打包war,为了后续的运行。

执行打包好的dubbo-admin的war包 :在linux中的tomcat中运行 (将刚才的war放入 tomcat的webapps中即可0)

在maven中引入一个 mvn中央仓库中不存在的Jar:
将jar自己安装到本地mvn仓库:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=10.2.0.5.0 -Dpackaging=jar -Dfile=d:\ojdbc7.jar

com.oracle ojdbc7 10.2.0.5.0

service依赖:dao、pojo、父工程
service启动、发布

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值