Intellij IDEA 2018.2 搭建springboot+dubbo+zookeeper+mybatis简单的分布式框架(二)

搭建springboot+dubbo+zookeeper+mybatis具体操作:(注意必须启动zookeeper服务哦!

一、打开IDE创建springboot父项目demo,再新建三个module:

    1.  dubbo-api(对外RPC接口)     

    2.  dubbo-provider(dubbo接口提供者)     

    3. dubbo-customer(dubbo接口消费者)
如图所示:

二、 父项目demo的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dubbo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>


    <!--子模块-->
    <modules>
        <module>dubbo-api</module>
        <module>dubbo-provider</module>
        <module>dubbo-customer</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--手动添加包-->
        <!--dubbo整合springboot包-->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>1.0.1</version>
        </dependency>
        <!--zookeeper客户端来连接zookeeper服务并注册服务-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <!--zookeeper包-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  三、 3个子项目相关配置与文件

    3.1  dubbo-api(对外RPC接口)    -->主要放实体、接口、也可以放一些公用的工具类工程

 

业务接口

    3.2  dubbo-provider(dubbo接口提供者)      -->主要是服务提供者

mapper接口:

import com.dubbo.demo.api.entity.Userinfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 底层数据UserinfoMapper接口
 */
@Mapper   //扫描注入bean
public interface UserinfoMapper {

    @Select("select * from userinfo")
    @Results(value = {
            @Result(property = "id", column = "id"),
            @Result(property = "userCode", column = "userCode"),
            @Result(property = "userName", column = "userName"),
            @Result(property = "userPassword", column = "userPassword")
    })
    public List<Userinfo> getAll();


    @Select("select * from userinfo where id=#{id}")
    public Userinfo findById(Integer id);

}

实现接口的业务类

application.yml

spring:
  dubbo:
    appname: provider
    registry: zookeeper://47.240.20.111:2181
    port: 20080
    protocol: dubbo
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
    username: root
    password: root
server:
  port: 8882


providerApplication运行:

    3.3  dubbo-customer(dubbo接口消费者)      -->主要是服务消费者

控制器:

package com.hlx.demo.customer.controller;

import com.alibaba.dubbo.config.annotation.Reference;


import com.dubbo.demo.api.entity.Userinfo;
import com.dubbo.demo.api.service.UserinfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//控制器
@RestController
public class UserinfoController {

    //业务接口bean的注入
    @Reference(check = false)  //dubbo的包
    private UserinfoService userinfoService;

    @RequestMapping("/say1/{name}")
    public String say1(@PathVariable("name")  String name){
        System.out.println("进来了!say1");
        //调用业务方法
        return "how are you ? "+userinfoService.hello(name);
    }

    @GetMapping("say2")
    public String say2(){
        System.out.println("进来了!say2");
        //调用业务方法

        return "你好吗? "+userinfoService.hello("mike");
    }

    @GetMapping("all")
   public List<Userinfo> all(){
        System.out.println("查看数据列表++++》");
       return userinfoService.getList();
   }

   @RequestMapping("/find/{id}")
   public Userinfo find(@PathVariable("id") Integer id){
       System.out.println("查看用户信息一条++++》");
       return userinfoService.getUserinfo(id);
   }
}

 application.yml文件:


server:
  port: 8884
  servlet:
    context-path: /
spring:
  dubbo:
    appname: consumer-test
    registry: zookeeper://47.240.20.111:2181
    protocol: dubbo

customerApplication运行:

先启动服务器,再启动消费者

运行效果:

有参数的传递方式

无参数的传递方式

查询所有的数据以JSON格式显示

根据ID查询数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值