Spring Boot integrates dubbo, mybatis builds maven project

1. Dubbo service provider (Spring Boot+Dubbo+Mybatis) The

Dubbo service provider application integrates Dubbo and Mybatis through Spring Boot, encapsulates the Service layer and Dao layer, and provides Dubbo services to the outside world.


[1. Project structure]





[2. pom.xml]

<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.iteye.maosheng</groupId>
  <artifactId>springboot_dubbo_provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 
  <!-- Spring Boot start parent dependency -->
<parent>
<groupId>org.springframework.boot</groupId>
<
<version>1.5.1.RELEASE</version>
</parent>

    <properties>
        <!-- JDK 1.8 -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
   
        <!-- Import dependency management from Spring Boot -->
        <!-- 有些情况我们已经有父pom,不能直接增加<parent>时,可以通过如下方式,注意:<scope>import</scope>  -->
        <!--        
        <dependency>
           
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.2.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        -->

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

<dependency> 
            <groupId>com.alibaba</groupId> 
            <artifactId>druid</artifactId> 
            <version>1.0.5</version> 
        </dependency>

<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>
<dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
       
    </dependencies>
   
    <build>
<plugins>
    <!-- Compile -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
            </plugin>
           
    <!-- spring boot maven plugin -->
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <!-- 通过mvn spring-boot:run启动就支持热部署了 -->
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.5.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
</plugins>
</build>
   
</project>


【3. application.yml】

server:
  port: 8011
       
spring:
    datasource:
        name: test
        url: jdbc:mysql://192.168.0.243:3306/test?useUnicode=true&allowMultiQueries=true&characterEncoding=utf-8
        username: root
        password: 112233
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20

mybatis:  
  mapperLocations: classpath:com/maosheng/dubbo/demo1/dao/mapper/*Mapper.xml
  typeAliasesPackage: com.maosheng.dubbo.demo1.entity


【4. provider.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 ">
    <!-- Consumer application name, used to calculate dependencies, not matching conditions, not the same as the provider-->
    <dubbo:application name="provider" />
    <!- - Use zookeeper as the registry-->
    <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.87.214:1051" />
    <!-- Generate remote service proxy, you can use demoService like local bean - ->
    <dubbo:service interface="com.maosheng.dubbo.demo1.TestService" ref="testService"></dubbo:service>
    <dubbo:service interface="com.maosheng.dubbo.demo1.UserService" ref="userService"></dubbo:service>
       
</beans>


【5. Application.java】

package com.maosheng.dubbo.demo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
*
* @author Administrator
*
*/
@SpringBootApplication
@ImportResource("classpath:provider.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}


2. Dubbo service consumer (Spring Boot+Dubbo)

Dubbo service consumer application integrates Dubbo through Spring Boot and encapsulates the Service layer And the Controller layer, the Service layer consumes the services provided by the Dubbo provider application, and the Controller layer provides Restful services externally.


[1. Project structure]





[2. pom.xml]

<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.iteye.maosheng</groupId>
  <artifactId>springboot_dubbo_consumer</artifactId>
  <version>0.0.
 
  <!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
 
    <properties>
        <!-- JDK 1.8 -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
   
        <!-- Import dependency management from Spring Boot -->
        <!-- 有些情况我们已经有父pom,不能直接增加<parent>, you can use the following methods, pay attention: <scope>import</scope> -->
        <!--        
        <dependency>
           
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.2.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        -->

<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>

<dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>       
        <dependency> 
    <groupId>com.alibaba</groupId> 
     <artifactId>fastjson</artifactId> 
     <version>1.1.41</version> 
        </dependency>

    </dependencies>
   
    <build>
<plugins>
    <!-- Compile -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
            </plugin>
           
    <!-- spring boot maven plugin -->
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <!-- 通过mvn spring-boot:run启动就支持热部署了 -->
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.5.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
</plugins>
</build>
   
</project>


【3. application.yml】

server:
  port: 8012
       

【4. consumer.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 ">
    <!-- Consumer application name, used to calculate dependencies, not matching conditions, not the same as the provider -->
    <dubbo:application name="consumer" />
    <!-- zookeeper as a registry -->
    <dubbo:registry  protocol="zookeeper" address="zookeeper://192.168.87.214:1051" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference interface="com.maosheng.dubbo.demo1.TestService" id="testService"></dubbo:reference>
    <dubbo:reference interface="com.maosheng.dubbo.demo1.UserService" id="userService"></dubbo:reference>
   
</beans>


【5. Application.java】

package com.maosheng.dubbo.demo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:consumer.xml")
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
   
}


3. Start the test

1. Run the Application under the

springboot_dubbo_provider project 2. Run the Application under the springboot_dubbo_consumer project

3. Call the hello Restful interface through http://localhost:8012/hello





  Call the sayHello Restful interface through http://localhost:8012/sayHello/maosheng:





  Test the addUserWithBackId Restful interface through the RESTClient tool:




  Database results:





4. See the attachment for the demo source code ( springboot_dubbo_demo.zip)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326381164&siteId=291194637