栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

简搭一个springcloud+zookeeper+dubbo脚手架

简搭一个springcloud+zookeeper+dubbo脚手架


简搭一个springcloud+zookeeper+dubbo脚手架

写了这么久的代码,除了crud就是crud,到头来连项目都不会搭,这里捡起来一下,仿照现有项目样式,自己写一个,锻炼一下自己的能力(基本看着各种文档搭的,知其然不知其所以然)。

1.项目准备 1.1项目介绍

简介:一个分布式的贸易项目,先把后端基础编码环境搭起来,数据库,前端还有其他后续再说。

1.2项目创建

先建几个maven项目。
base-parent:管理其他模块项目,该项目配置的dependency所有的jar包都会被该项目下的所有模块继承
app-gateway:网关项目,主要接收前台强求,连接后端接口
master-data-api:主数据中心api
master-data-service:主数据中心service层

2.创建项目 2.1base-parent

先建base-parent项目,就是一个maven项目。


一路next完事。
其实听同事说,现在的不需要再单独建parent项目了,各模块单独在一个父模块下面就行,继续问下去他也不是很清楚,我就还是建了,后续了解了再改吧。
base-parent pom文件
主要导入boot和cloud的包



    4.0.0

    com.example
    base-parent
    1.0-SNAPSHOT
    pom
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
    
    
        
            
            
                org.springframework.boot
                spring-boot-dependencies
                2.3.1.RELEASE
                pom
                import
            
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR6
                pom
                import
            
        
    

    
        8
        8
    


2.2app-gateway

同样是建一个maven项目,想简单些就直接右击parent项目


可以看到parent就是base-parent,pom文件的配置会自己给你写上去。
然后是gateway的pom配置,因为这个项目需要用到zookeeper和dubbo,所以需要引入相关依赖。



    4.0.0
    
        com.example
        base-parent
        1.0-SNAPSHOT
    
    app-gateway
    
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
        
            com.alibaba.spring.boot
            dubbo-spring-boot-starter
            2.0.0
        
        
        
            com.101tec
            zkclient
            0.10
        
    
    
        8
        8
    

gateway application.properties文件配置

#设置内嵌tomcat端口号
server.port=8082
server.servlet.context-path=/
#dubbo配置
spring.application.name=app-gateway
#指定注册中心,使用的是zookeeper
spring.dubbo.registry=zookeeper://127.0.0.1:2181

因为gateway是消费端的,所以先把对应的配置写好
doubbo-data-consumer.xml



    
    
    
    
    
    

    
    

    
    
    

然后还有最重要的application

package com.example;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.importResource;

@SpringBootApplication(scanbasePackages = {"com.example"})
@importResource({"classpath:doubbo-data-consumer.xml"})
@EnableDubboConfiguration//开启dubo的配置
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

2.3master-data-api

同样是建一个maven项目,想简单些就直接右击parent项目


可以看到parent就是base-parent,pom文件的配置会自己给你写上去。
然后是gateway的pom配置,因为这个项目需要用到zookeeper和dubbo,所以需要引入相关依赖。



    4.0.0
    
        com.example
        base-parent
        1.0-SNAPSHOT
    
    app-gateway
    
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
        
            com.alibaba.spring.boot
            dubbo-spring-boot-starter
            2.0.0
        
        
        
            com.101tec
            zkclient
            0.10
        
    
    
        8
        8
    

api pom默认,打包方式改成jar就行

    
        base-parent
        com.example
        1.0-SNAPSHOT
    
    4.0.0

    master-data-api
    1.0-SNAPSHOT
    jar
2.4master-data-service

同样建一个继承base-parent的项目
master-data-service是服务提供方,所以pom里也需要加入zk,db的依赖。

    
        base-parent
        com.example
        1.0-SNAPSHOT
    
    4.0.0

    master-data-service
    1.0-SNAPSHOT
    war
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.alibaba.spring.boot
            dubbo-spring-boot-starter
            2.0.0
        

        
            com.101tec
            zkclient
            0.10
        
    
    
        8
        8
    

master-data-service application.properties

#设置内嵌tomcat端口号
server.port=8081
#设置上下文
server.servlet.context-path=/
#设置dubbo的配置
spring.application.name=master-data-service
#设置当前工程是一个服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://127.0.0.1:2181

doubbo-data-provider.xml



    
    

    
    
    
    
    
    

    
    
    

application:

package com.example.data;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.importResource;

@SpringBootApplication(scanbasePackages = {"com.example"})
@importResource({"classpath:doubbo-data-provider.xml"})
@EnableDubboConfiguration//开启dubo的配置
public class MasterDataServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(MasterDataServiceApplication.class, args);
    }
}

然后接下来还有最后一步,安装zookeeper
http://www.apache.org/dyn/closer.cgi/zookeeper
下载zookeeper,目录随意,能找到就行。
在zookeeper-3.4.6conf下面创建zoo.cfg

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

zookeeper-3.4.6bin下面

每次启动项目提前打开它。
项目启动先起service,再起gateway。
这个脚手架只是初步搭建,还很不完善,但是勉强能用,仅供参考,照抄等死。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629743.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号