综合学习SpringBoot+Dubbo+Zookeeper,结合平时用的框架,对常用的框架整合了一下,从框架到后台到前端,如有侵权,请联系删除!

后台框架springboot+mybatis+dubbo+zookeeper+redis+nginx,其中redis做数据缓存,nginx做页面转发,数据库用mysql!首先springboot-mybatis-dubbo-zookeeper目录架构如下:

 

首先项目外层定义版本等的pom文件如下:

 

<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.cn.bee</groupId>
    <artifactId>springboot-mybatis-dubbo-zookeeper</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>springboot-mybatis-dubbo-zookeeper</name>
    <description>springboot-mybatis-dubbo-zookeeper</description>


    <properties>
        <jdk.version>1.7</jdk.version>
        <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
        <maven-resources-plugin.version>2.6</maven-resources-plugin.version>
        <org.mybatis.generator.version>1.3.2</org.mybatis.generator.version>
        <mybatis.version>3.1.1</mybatis.version>
        <mybatis-spring.version>1.2.0</mybatis-spring.version>
        <mybatis-spring-boot>1.2.0</mybatis-spring-boot>
        <dubbo.version>2.5.3</dubbo.version>
        <zookeeper.version>3.3.6</zookeeper.version>
        <zkclient.version>0.1</zkclient.version>
        <com.alibaba.druid.version>0.2.6</com.alibaba.druid.version>
        <spring-boot-starter-redis-version>1.3.2.RELEASE</spring-boot-starter-redis-version>
    </properties>
    <!-- springboot使用1.4.1.RELEASE版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>

            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    <modules>
        <!-- 主要放实体、接口、也可以放一些公用的工具类工程-->
      <module>edu-web-common</module>
        <!--  服务提供者工程-->
      <module>edu-web-server</module>
        <!-- 服务消费者工程 -->
        <module>edu-web-client</module>
   </modules>
   <!--http://blog.csdn.net/shawearn1027/article/details/55098024-->
</project>

edu-web-client下面的Application.java,主要用注解方式加载Bean,而且服务提供者的启动main方法也写在这下面,代码如下:

 

 

 

 

@MapperScan("com.cn.bee.dao")
@SpringBootApplication
@ImportResource(locations={"dubbo-provider.xml"})
public class Application {
    /**
     * 1.启动app层的main方法之后,会自动加载bean, @ConfigurationProperties(prefix="spring.datasource")会自动去application.properties
     * 下面以spring.datasource开头的有关数据库连接的配置文件
     * 2.通过注解@MapperScan("com.cn.bee.dao")自动扫描mybatis的mapper文件
     */
   private static volatile boolean running = true;

    private static Logger logger = Logger.getLogger(Application.class);

    //DataSource配置
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        return new com.alibaba.druid.pool.DruidDataSource();
    }

    //提供SqlSeesion
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 配置mapper的扫描,找到所有的mapper.xml映射文件
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
        // 加载全局的配置文件
        sqlSessionFactoryBean.setConfigLocation(
                new DefaultResourceLoader().getResource("classpath:mybatis-config.xml"));

        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    /**
     * Main 方法启动项
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        System.out.println("============= APP Start ON SpringBoot Success =============");
        synchronized (Application.class) {
            while (running) {
                try {
                    Application.class.wait();
                } catch (Throwable e) {
                }
            }
        }
    }

}

启动edu-web-client的main方法之后,会自动加载bean, @ConfigurationProperties(prefix="spring.datasource")会自动去application.properties下面以spring.datasource开头的有关数据库连接的配置文件,application.properties如下:

 

 

 

 

spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

dubbo.registry.center=127.0.0.1:2181
dubbo.protocol.port=20885

 

服务提供者的dubbo-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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd  
        http://code.alibabatech.com/schema/dubbo  
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <context:component-scan base-package="com.cn.bee.service.*">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <dubbo:application name="springBoot-service-provider-app" />
    <!-- Dubbo注册地址 -->
    <dubbo:registry protocol="zookeeper" address="${dubbo.registry.center}" />
    <dubbo:protocol name="dubbo" port="${dubbo.protocol.port}" threadpool="cached" threads="1000" accepts="2000" serialization="java"/>

    <dubbo:service interface="com.cn.bee.service.UserService" ref="userService" timeout="3000" ></dubbo:service>
    <dubbo:service interface="com.cn.bee.service.DictService" ref="dictService" timeout="3000" ></dubbo:service>
    <dubbo:service interface="com.cn.bee.service.AreaService" ref="areaService" timeout="3000" ></dubbo:service>


</beans>

 

edu-web-client中其他层次的代码就省略了,有需要可以联系我了解!同时自己本地可以下载一个单机版的zookeeper

edu-web-common:主要放实体、接口、也可以放一些公用的工具类等待。

到了这里,我们的服务已经可以完成了,运行application.Java的main方法,我们可以看到控制台如下,就代表成功了!

然后我们到edu-web-server,主要做服务端

application.properties如下:

 

platform_context_path=/bee

dubbo.registry.center=127.0.0.1:2181
server.port=9090
#redis服务器
redis_core_database=127.0.0.1_6379
#配置启动项目时,定时任务隔多长时间运行一次
redis_time=60

然后服务消费者代码如下:

 

 

 

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd   
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <context:annotation-config />
    <dubbo:application name="springboot-service-consumer-app" />
    <dubbo:registry id="springboot-reg" protocol="zookeeper" address="${dubbo.registry.center}" />
    <dubbo:annotation />

    <context:component-scan base-package="com.cn.bee.*">
        <context:include-filter type="annotation"
                                expression="com.alibaba.dubbo.config.annotation.Reference" />
    </context:component-scan>
</beans>  

Controller的代码就省略了!

 

 

 

最后就是到edu-web-server的启动项,你只需要运行这个main方法,项目就可以访问了,Application.java代码如下:

 

@SpringBootApplication(scanBasePackages="com.cn.bee.controller")
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ImportResource(locations = {"dubbo-consumer.xml"})
public class Application{
   private static Logger logger = Logger.getLogger(Application.class);
    public static void main(String[] args)  throws Exception {
        SpringApplication.run(Application.class, args);
        System.out.println("============= SpringBoot web Start Success =============");
    }
}

 

@SpringBootApplication(scanBasePackages="com.cn.bee.controller") 括号里面的内容,如果Controller和启动main同一个包下面是不需要指向的,如果要放在不同包下面不加scanBasePackages="com.cn.bee.controller"会报错!

然后我们现在启动Application,如果成功,我们会看到控制台如下:

 

这时候我们就可以访问试一下,看到返还的如下JSON数据,就证明我们项目跑起来了:

 

其中Dubbo注册中心的页面如下:

 

 

                                                           

                                                

                                                   关注「码农小助手」,提升技能

                                     

                                                  每天记得对自己说:你是最棒的!

                                                “转发和在看是对作者最大的支持”
 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐