• 【微服务】SpringBoot+Dubbo+ZooKeeper 实战



    项目说明

    本项目采用dubbo来是实现消费者来远程调用生产者的服务。
    原本生产者应该连接数据库,service服务来对dao层进行业务逻辑实现,但是为了更好的理解,项目将省略数据库操作,采用模拟实现。

    如果你对微服务感兴趣可参考博主以前写的SpringCould系列文章:Spring Could

    项目有3个模块:

    • common:存放实体类,interface接口,以及其他一些公共部分
    • provider:生产者,提供服务:实现common模块的interface接口
    • customer:消费者,远程调用product的服务。
      在这里插入图片描述

    common模块

    在这里插入图片描述

    1. pom.xml【引入springboot、dubbo、zk相关依赖】
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <version>2.5.1version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <version>2.5.2version>
            dependency>
    
            
            <dependency>
                <groupId>com.alibaba.bootgroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>0.1.0version>
            dependency>
            
            <dependency>
                <groupId>com.101tecgroupId>
                <artifactId>zkclientartifactId>
                <version>0.6version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    1. 实体类User
    /**
     * 

    User

    *

    用户实体类

    * * @author : he zhe * @date : 2022-07-29 10:09 **/
    public class User implements Serializable { /** * 用户名 */ private String username; /** * 昵称 */ private String nick; /** * 年龄 */ private Integer age; //构造器,get、set方法 省略 }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    1. 服务接口UserService
    public interface DemoService {
    
        /**
         * 获取全部用户
         * @return
         */
        List<User> getUser();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    common模块是一个公共模块,把生产者消费者都需要的依赖,都统一导入common模块,然后其他模块引入common模块,还有都需要用到的实体类,需要远程调用的服务接口都统一放在common模块。

    provider模块

    在这里插入图片描述

    1. pom.xml 【引入common模块即可】
        <dependencies>
            
            <dependency>
                <groupId>org.examplegroupId>
                <artifactId>commonartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. DemoServiceImpl【实现common模块的接口】
    /**
     * 

    DemoServiceImpl

    *

    demo服务实现类

    * * @author : he zhe * @date : 2022-07-29 10:12 **/
    @Service("demoService") public class DemoServiceImpl implements DemoService { @Override public List<User> getUser() { //暂不连接数据库 采用模拟数据 User user = new User("小明","明明",18); User user2 = new User("小菜","菜菜",20); List<User> list = new ArrayList<>(); list.add(user); list.add(user2); return list; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    1. service-provide.xml【dubbo配置,把借口暴露给消费者】
    
    <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-2.5.xsd
    	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
    
        
    
        
    
    
    
        
        <dubbo:service ref="demoService" interface="com.study.service.DemoService" />
    
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    关于dubbo标签解释,在文章最后有介绍。

    1. application.yml
    dubbo:
      #服务器名称唯一
      application:
        name: provider
      #注册中心配置,使用zookeeper做注册中心
      registry:
        address: zookeeper://127.0.0.1:2181
      protocol:
        #    使用dubbo协议,端口默认20880
        port: 20880
        name: dubbo
      monitor:
        address: registry
    
    server:
      port: 8081
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    关于dubbo的配置,你可以放到service-provide.xml里面,也可以在application.yml里面配置,但是最好不要两个地方都配置,不然到时候会报dubbo服务器名称不唯一的错误,启动不起来。

    1. ProductApplication 【启动类,需要@ImportResource扫描到dubbo的配置,不然不会生效】
    /**
     * 

    ProductApplication

    *

    提供者 启动类

    * * @author : he zhe * @date : 2022-07-29 09:41 **/
    @SpringBootApplication @ImportResource({"classpath:/dubbo/service-provider.xml"}) public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); System.out.println("生产者启动成功"); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    customer模块

    在这里插入图片描述

    1. service-consumer.xml【dubbo,接受生产者暴露的服务】
    
    <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-2.5.xsd
    	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        <dubbo:application name="customer"/>
        <dubbo:registry address="zookeeper://127.0.0.1:2181">dubbo:registry>
        
        <dubbo:reference id="demoService" interface="com.study.service.DemoService" />
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. application.yml
    server:
      port: 8088
    
    • 1
    • 2
    1. DomeController
    /**
     * 

    DemoController

    *

    demo controller

    * * @author : he zhe * @date : 2022-07-29 09:36 **/
    @RestController @RequestMapping("/user") public class DemoController { @Autowired private DemoService demoService; @RequestMapping(value = "/get", method = RequestMethod.POST) public List<User> getUsers(){ return demoService.getUser(); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    1. CustomerApplication
    /**
     * 

    CustomerApplication

    *

    消费者 启动类

    * * @author : he zhe * @date : 2022-07-29 09:36 **/
    @SpringBootApplication @ImportResource({"classpath:/dubbo/service-consumer.xml"}) public class CustomerApplication { public static void main(String[] args) { SpringApplication.run(CustomerApplication.class, args); System.out.println("消费者启动成功"); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    小结

    先在本地启动zookeeper服务,然后启动生产者(product),再启动消费者(customer)。
    访问POST http://localhost:8088/user/get,最好不要用浏览器访问,浏览器默认的是get请求方式,可能会报405错误,可以修改Controller请求的方法,改为get方式。
    在这里插入图片描述

    在这里插入图片描述

    Dubbo配置标签属性说明

    参考官方网址:https://dubbo.apache.org/zh/docs/references/xml/dubbo-provider/
    在这里插入图片描述

  • 相关阅读:
    Linux - grep命令详解
    自己学习Cesium的笔记简介
    UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
    Elasticsearch学习-- 聚合查询
    企业数据现状分析:为什么需要实时数据?如何高效挖掘实时数据价值?
    基于 StarRocks 的风控实时特征探索和实践
    无涯教程-JavaScript - INDIRECT函数
    text2sql、nl2sql框架总结
    Git:Git中的分支管理
    【C++初阶】类和对象(三)
  • 原文地址:https://blog.csdn.net/qq_43466788/article/details/126052340