首页 > 其他分享 >HMdubbo4【dubbo快速入门】

HMdubbo4【dubbo快速入门】

时间:2022-10-09 23:22:06浏览次数:55  
标签:web 入门 service dubbo yppah org HMdubbo4 com

  • 框架

image-20221008234303244

提供者和消费者理论上是两个项目,此处用同一项目中两个模块进行模拟

  • 实现步骤

    ①创建服务提供者Provider模块

    ②创建服务消费者Consumer模块

    ③在服务提供者模块编写 UserServiceImpl 提供服务

    ④在服务消费者中的 UserController 远程调用UserServiceImpl 提供的服务

    ⑤分别启动两个服务,测试

1 demo:spring+springMVC

1.1 创建项目&模块

创建一个空项目用于包裹代码

image-20221008234749122

image-20221008234846378

image-20221008235015639

image-20221008235255379

image-20221008235318306

image-20221008235349896

image-20221008235403730

同上,再新建一个dubbo-web模块

image-20221008235546988

image-20221009000109119

之前传统项目中,web和service是在同一个项目中的

现在将两者分成不同的模块,web依赖于service

web作为war,可以在tomcat中启动

service作为jar,仅提供服务

<?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.yppah</groupId>
    <artifactId>dubbo-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <properties>
        <spring.version>5.1.9.RELEASE</spring.version>
        <dubbo.version>2.7.4.1</dubbo.version>
        <zookeeper.version>4.0.0</zookeeper.version>

    </properties>

    <dependencies>
        <!-- servlet3.0规范的坐标 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--spring的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--springmvc的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>
        <!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>8000</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>
<?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.yppah</groupId>
    <artifactId>dubbo-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging> <!-- 默认jar,可省略 -->


    <properties>
        <spring.version>5.1.9.RELEASE</spring.version>
        <dubbo.version>2.7.4.1</dubbo.version>
        <zookeeper.version>4.0.0</zookeeper.version>

    </properties>

    <dependencies>
        <!-- servlet3.0规范的坐标 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--spring的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--springmvc的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>
        <!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
    </dependencies>


</project>

1.2 dubbo-service模块

image-20221009001453857

package com.yppah.service;

/**
 * @Author: haifei
 * @Date: 2022/10/9 0:06
 */
public interface UserService {

    public String sayHello();
}

package com.yppah.service.impl;

import com.yppah.service.UserService;
import org.springframework.stereotype.Service;

/**
 * @Author: haifei
 * @Date: 2022/10/9 0:07
 */
@Service //bean
public class UserServiceImpl implements UserService {

    @Override
    public String sayHello() {
        return "hello 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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫包-->
    <context:component-scan base-package="com.yppah.service" />

</beans>
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=info, stdout,file
# My logging configuration...
#log4j.logger.com.tocersoft.school=DEBUG
#log4j.logger.net.sf.hibernate.cache=debug
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=../logs/iask.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %l  %m%n

1.3 dubbo-web模块

image-20221009204208696


image-20221009002945710

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--注解驱动打开-->
    <mvc:annotation-driven/>
    <!--扫包-->
    <context:component-scan base-package="com.yppah.controller"/>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<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"
         version="2.5">

		 
	<!-- spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext*.xml</param-value>
        <!--需要在web模块的pom中添加对dubbo-service模块的依赖,不然上行报红-->
    </context-param>
    <!--spring监听器ContextLoaderListener-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


	<!-- springMVC -->
    <!--springMVC前端控制器DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>


    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


</web-app>
package com.yppah.controller;

import com.yppah.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: haifei
 * @Date: 2022/10/9 0:30
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/sayHello")
    public String sayHello(){
        return userService.sayHello();
    }
}

1.4 启动测试

  • 因为service模块被依赖了,所以先maven-install安装一下

image-20221009003649247

image-20221009003733331

  • 然后maven-tomcat启动web模块

image-20221009003814821

image-20221009204425604

image-20221009204436610

以上实现了本地调用

虽然拆分成了两个模块,但此时还属于单体应用架构,并没有实现分布式

分布式要求每个模块/项目都可以单独启动、独立对外提供服务;而显然此时service并不符合

image-20221009204814364

下面通过dubbo实现远程调用

改造为SOA架构/分布式架构

image-20221009205236010

2 服务提供者provider

image-20221009205450103

image-20221009205508629

image-20221009205631836

  • 将service模块改造为web项目,并配置dubbo

image-20221009211335868

package com.yppah.service.impl;

import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//import org.springframework.stereotype.Service;

/**
 * @Author: haifei
 * @Date: 2022/10/9 0:07
 */
//@Service //定义bean:将该类的对象创建出来,放到spring的IOC容器中
@Service //将该类提供的方法(服务)对外发布,将访问地址(ip,端口,路径)注册到注册中心中(zk)
public class UserServiceImpl implements UserService {

    @Override
    public String sayHello() {
        return "hello 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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫包-->
    <!--<context:component-scan base-package="com.yppah.service" />-->

    <!--dubbo配置-->
    <!--1. 配置项目名称(唯一)-->
    <dubbo:application name="dubbo-service" />
    <!--2. 配置注册中心地址-->
    <dubbo:registry address="zookeeper://192.168.1.8:2181" />
    <!--3. 配置dubbo扫包-->
    <dubbo:annotation package="com.yppah.service.impl" />

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<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"
         version="2.5">

		 
	<!-- spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext*.xml</param-value>
        <!--需要在web模块的pom中添加对dubbo-service模块的依赖,不然上行报红-->
    </context-param>
    <!--spring监听器ContextLoaderListener-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


</web-app>

image-20221009211734374

image-20221009211449503

image-20221009211904749

3 服务消费者consumer

image-20221009223006351

image-20221009222633417

image-20221009222522885

image-20221009223352069

package com.yppah.controller;

//import com.yppah.service.UserService;
import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: haifei
 * @Date: 2022/10/9 0:30
 */
@RestController
@RequestMapping("/user")
public class UserController {

    //@Autowired//本地注入
    /*
        1. 从注册中心zk获取useService的访问url
        2. 进行远程调用RPC
        3. 将结果封装为一个代理对象,并给变量赋值
     */
    @Reference//远程注入
    private UserService userService;

    @RequestMapping("/sayHello")
    public String sayHello(){
        return userService.sayHello();
    }
}

package com.yppah.service;

/**
 * @Author: haifei
 * @Date: 2022/10/9 22:28
 */
public interface UserService {

    public String sayHello();
}

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--注解驱动打开-->
    <mvc:annotation-driven/>
    <!--扫包-->
    <context:component-scan base-package="com.yppah.controller" />

    <!--dubbo配置-->
    <!--1. 配置项目名称(唯一)-->
    <dubbo:application name="dubbo-web" />
    <!--2. 配置注册中心地址-->
    <dubbo:registry address="zookeeper://192.168.1.8:2181" />
    <!--3. 配置dubbo扫包-->
    <dubbo:annotation package="com.yppah.controller" />

</beans>

image-20221009223534132

image-20221009223633582

访问测试

image-20221009223725110

报错如下

image-20221009224045957

原因如下

image-20221009224017324

解决如下

image-20221009224241822

重启web模块测试

image-20221009224344586

image-20221009224402570

ok

4 改造-抽取接口模块

新建模块dubbo-interface

image-20221009230423674

image-20221009224641122

image-20221009230621421

<?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.yppah</groupId>
    <artifactId>dubbo-interface</artifactId>
    <version>1.0-SNAPSHOT</version>


</project>
package com.yppah.service;

/**
 * @Author: haifei
 * @Date: 2022/10/9 0:06
 */
public interface UserService {

    public String sayHello();
}

image-20221009230733560

image-20221009230818294

image-20221009230956791

image-20221009231026663

image-20221009231225102

interface模块是依赖模块,先maven-install一下

然后依次maven-tomcat启动service和web模块

image-20221009231411087

image-20221009231458272

image-20221009231523893

image-20221009231649777

image-20221009231704334

image-20221009231724058

标签:web,入门,service,dubbo,yppah,org,HMdubbo4,com
From: https://www.cnblogs.com/yppah/p/16767521.html

相关文章

  • 三、Axios入门——Axios的CRUD基本使用
    一、启动json-server服务详细教程:https://www.cnblogs.com/wml-it/p/16773220.html二、搭建页面<!doctypehtml><htmllang="en"><head><metacharset="UTF-8">......
  • AI绘画,Midjourney极简入门
    前几天看报道说:一位小哥用AI绘画工具Midjourney生成的作品,在美国科罗拉多州博览会的艺术比赛中获得了第一名。作者表示,他多次调整了输入的提示词,生成了100多幅画作,经过......
  • freemarker从入门到精通
    目录 ​​一:概述​​​​二:Freemarker的Helloworld​​​​三:freemarker模板语法​​​​1.访问map中的key​​​​2.访问POJO中的属性​​​​3.取集合中的数据​​​​......
  • Servlet 入门
    一、Servlet基础使用1.创建web项目,导入Servlet依赖坐标(pom.xml)<dependencies><dependency><groupId>javax.servlet</groupId><artifactId>j......
  • bootstrap入门学习笔记
    本来这记着一天的笔记,网上搜了一下教程手册,整理得比我的强多了。果断删掉。相应的bootStrap教程网址 ​​https://www.runoob.com/bootstrap/bootstrap-typography.html......
  • C++入门到理解set/multiset容器、pair对组
    一:基本概念在插入元素的时候会自动排好序,比如插入的是1,3,4,2,打印是1,2,3,4.底层是通过二叉树结构实现的,set容器不允许有重复的元素,但是multiset允许有元素重复。二:构造,遍历,赋值#i......
  • python基础入门之函数
    python基础入门之函数目录python基础入门之函数一、函数的概念二、函数的语法结构1、格式2、格式信息的解释三、函数的定义与调用四、函数分类五、函数的返回值1、什么是......
  • 【2022-10-09】DRF从入门到入土(七)
    drf组件之权限类使用#认证:校验用户是否登录,登录认证#用户登录了,某个接口可能只有超级管理员才能访问,普通用户不能访问#出版社的所有接口,必须登录,而且是超级管理员才......
  • 函数入门
    目录函数基础函数的语法结构函数的定义和调用函数的分类函数的返回值函数的参数函数基础计算机的函数,是一个固定的一个程序段,或称其为一个子程序,它在可以实现固定运算功......
  • Java入门,如何高效学习
      对于当下要想入行学习Java,那就一定是个不错的选择,因为这个行业是个你只要努力就能看到成果的行业,而且就从近两年来看,当前的程序员依旧是这个时代的高薪职业,且想要入这......