# 8 快速入门 dubbo

8快速入门dubbo所需资料注册中心Zookeeper安装zookeeper官方推荐使用zookeeper注册中心;注册中心负责服务地址的注册与查找,相当于目录服务;服务提供者和消费者只在启动时与注册中心交互

8 快速入门 dubbo

所需资料

注册中心 Zookeeper

安装 zookeeper

  • 官方推荐使用 zookeeper 注册中心;
  • 注册中心负责服务地址的注册与查找,相当于目录服务;
  • 服务提供者和消费者只在启动时与注册中心交互,注册中不转发请求,压力较小;
  • Zookeeper 是 apache hadoop 的子项目,是一个树形的目录服务,支持变更推送,适合作为
    dubbo 的服务注册中心,工业强度较高,可用于生产环境;

入门 demo 的架构

# 8 快速入门 dubbo

注意

如果在粘贴这些web.xml 或者pom.xml里面build里面的标签发送这样的报错提示,注意哦 这可能不是错误,不会影响到我们项目的运行。

# 8 快速入门 dubbo

服务提供者

1、一个空的maven项目
2、提供一个服务接口即可

项目目录结构

图中红框的需要我们创建

# 8 快速入门 dubbo

提供方的pom.xml

各种依赖请严格按照下面的版本

  • 记得要更新pom文件哦!
  • 当dependencies中出现依赖的版本信息 说明依赖引入成功了
  • # 8 快速入门 dubbo

    <properties>        <maven.compiler.source>8</maven.compiler.source>        <maven.compiler.target>8</maven.compiler.target>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <spring.version>5.0.6.RELEASE</spring.version>    </properties>    <packaging>war</packaging>    <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>${spring.version}</version>        </dependency>        <!--dubbo -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.5.7</version>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.6</version>        </dependency>        <dependency>            <groupId>com.github.sgroschupf</groupId>            <artifactId>zkclient</artifactId>            <version>0.1</version>        </dependency>        <dependency>            <groupId>javassist</groupId>            <artifactId>javassist</artifactId>            <version>3.11.0.GA</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.tomcat.maven</groupId>                <artifactId>tomcat7-maven-plugin</artifactId>                <configuration>                    <port>8001</port>                    <path>/</path>                </configuration>                <executions>                    <execution>                        <!-- 打包完成后,运行服务 -->                        <phase>package</phase>                        <goals>                            <goal>run</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build>

提供方接口

public interface HelloService {    String sayHello(String name);}

暴露的提供方实现

@Service 这个注解不是spring的哦

@com.alibaba.dubbo.config.annotation.Servicepublic class HelloServiceImpl implements HelloService {    @Override    public String sayHello(String name) {        return \"Hello,\" + name + \"!!!\";    }}

服务提供方的配置文件spring.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\">    <!--1.服务提供方在zookeeper中的“别名”-->    <dubbo:application name=\"dubbo-server\"/>    <!--2.注册中心的地址-->    <dubbo:registry address=\"zookeeper://192.168.77.132:2181\"/>    <!-- 让监控 去注册中心 自动找服务 -->    <dubbo:monitor protocol=\"registry\"/>    <!--3.扫描类(将什么包下的类作为服务提供类)-->    <dubbo:annotation package=\"service.impl\"/></beans>

提供方的web.xml

<?xml version=\"1.0\" encoding=\"UTF-8\"?><web-app xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"         xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"         xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd\"         id=\"WebApp_ID\" version=\"3.1\"><!--使用上下文监听器-初始化项目环境-->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring/spring.xml</param-value>    </context-param></web-app>

服务消费方

项目目录结构

# 8 快速入门 dubbo

消费方的pom.xml

与服务方一致,只需要修改tomcat的端口为8002

消费方的Controller

因为我们是通过浏览器去访问的,所以要创建controller层,提供对外访问的接口

@RestControllerpublic class HelloAction {    @com.alibaba.dubbo.config.annotation.Reference    private HelloService hs;    @RequestMapping(\"hello/{name}\")    @ResponseBody    public String hello(@PathVariable String name) {        return hs.sayHello(name);    }}

消费方的接口

注意:

  • controller中要依赖HelloService,所以我们创建一个接口;
  • 这里是消费方,不需要实现,因为实现会让服务方为我们搞定!跟8001服务提供方的线程远程通信
public interface HelloService {    String sayHello(String name);}

消费方的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://code.alibabatech.com/schema/dubbo\"       xsi:schemaLocation=\"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd\">    <!--Dubbo的应用名称,通常使用项目名 -->    <dubbo:application name=\"dubbo-consumer\"/>    <!--配置Dubbo的注册中心地址 -->    <dubbo:registry address=\"zookeeper://192.168.77.132:2181\"/>    <!-- 让监控 去注册中心 自动找服务 -->    <dubbo:monitor protocol=\"registry\"/>    <!--配置Dubbo扫描类,将这个类作为服务进行发布 -->    <dubbo:annotation package=\"controller\"/></beans>

消费方的web.xml

<?xml version=\"1.0\" encoding=\"UTF-8\"?><web-app xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"         xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"         xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd\"         id=\"WebApp_ID\" version=\"3.1\">    <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring/spring.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

启动测试

还是跟是实现zookeeper实现分布式锁一样启动哦

  • 我们在这里配置了打包之后直接运行
  • 所以我们执行打包命令即可,如果出错,先执行clean在执行package

# 8 快速入门 dubbo

如果执行出错,或者访问失败可以去查找以下问题

  • @service注解是否是dubbo的
  • linux服务器的防火墙是否关闭
  • zookeeper的注册地址是否有误 linux查看本机ip 【ip address】
  • 如果想看到自己的服务,可以先搭建dubbo的可视化工具,文章地址

当出现端口地址 且上面没有报错信息时,说明启动成功

# 8 快速入门 dubbo

可以看到我们的请求成功了

# 8 快速入门 dubbo

本站部分文章来自网络或用户投稿,如无特殊说明或标注,均为本站原创发布。涉及资源下载的,本站旨在共享仅供大家学习与参考,如您想商用请获取官网版权,如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
开发者

SpringMVC底层——请求参数处理流程描述

2022-9-18 14:20:53

开发者

Linux系列之添加和删除软件命令

2022-9-18 14:21:01

搜索