Spring+zookeeper+dubbo構(gòu)建微服務(wù)項(xiàng)目框架

整個項(xiàng)目源碼我已經(jīng)提交到Github上面了,大家可以去看一下項(xiàng)目源碼地址

1、首先上搭建好的項(xiàng)目結(jié)構(gòu):

分層多模塊web項(xiàng)目(微服務(wù)架構(gòu))
SpringMVC+Spring+mybatis-plus 集成redis

  • commons-parent是父級項(xiàng)目管理子項(xiàng)目的maven jar包的版本信息。

  • commons-util是項(xiàng)目中一些公共類型的存放模塊。

  • commons-config是項(xiàng)目中通用配置文件的存放模塊

  • commons-manaeger是微服務(wù)內(nèi)聚項(xiàng)目

    • commons-mananger-dao dao層代碼(持久層代碼在這里,mybatis-plus的Mapper也是放在這里)
    • commons-manager-interface 接口類的存放模塊
    • commons-manager-model model和pojo類型的存放路徑
    • commons-manager-service 服務(wù)提供者
    • commons-manager-web 服務(wù)消費(fèi)者

    整體的項(xiàng)目目錄樹如上圖。
    項(xiàng)目創(chuàng)建的具體過程就不在這里貼出來了,使用idea構(gòu)建這種多模塊內(nèi)聚項(xiàng)目還是很簡單。

    201801.png

2、啟動相應(yīng)的前置中間件服務(wù)

啟動順序:
因?yàn)樵擁?xiàng)目已經(jīng)整合了redis,所以在啟動服務(wù)提供者和服務(wù)訂閱者之前最好先把redis服務(wù)給先啟動了


201802.png

redis服務(wù)啟動后,還需要把zookeeper注冊中心給開啟,zookeeper的配置我是使用默認(rèn)的端口


201803.png

以上的兩個服務(wù)啟動完成,我們就可以分別啟動服務(wù)提供者項(xiàng)目和服務(wù)訂閱者項(xiàng)目了。

3、啟動服務(wù)提供者和訂閱者

服務(wù)提供者項(xiàng)目其實(shí)就是commons-manager-service,該項(xiàng)目主要就是把項(xiàng)目的服務(wù)接口注冊到zookeeper上面,所以我這里就使用了main方法的方式來啟動。其實(shí)也可以使用其他的方式來啟動,不過這里為了簡單演示,所以就使用了main方法;
我們打開commons-manager-service目錄下面test文件下面的dubbo.test.DubboProviderTest類:


2018-10-19 10-55-52 的屏幕截圖.png

啟動該類的main方法就可以了。


2018-10-19 10-57-18 的屏幕截圖.png

如果控制臺,沒有報錯就說明項(xiàng)目啟動正常1,服務(wù)也已經(jīng)成功注冊到了zookeeper上面。

為了驗(yàn)證我們可以使用dubbo-admin項(xiàng)目來查看一下,服務(wù)是否已經(jīng)真的成功注冊了。使用tomcat啟動dubbo-admin項(xiàng)目。


201804.png

訪問dubbo-admin項(xiàng)目,我們可以看到有服務(wù)注冊了。


2018-10-19 11-01-30 的屏幕截圖.png

到這里服務(wù)提供者就已經(jīng)啟動成功了。

接著我們來啟動服務(wù)訂閱者來訪問注冊到zookeeper的服務(wù)。其實(shí)所說的服務(wù)訂閱者也就是項(xiàng)目commons-manager-web,這是一個web項(xiàng)目來的,我們使用tomcat來啟動就行了。


2018-10-19 11-04-39 的屏幕截圖.png

2018-10-19 11-05-18 的屏幕截圖.png

同樣的,如果控制臺沒有報錯,就說明啟動完成了。我們接著看一下dubbo-admin的消費(fèi)者信息。


2018-10-19 11-06-24 的屏幕截圖.png

可看到已經(jīng)有消費(fèi)者訂閱到服務(wù)了。

到這里我們就已經(jīng)把項(xiàng)目完全啟動了。并且項(xiàng)目里面已經(jīng)有通過mybatis-plus的代碼生成器根據(jù)數(shù)據(jù)庫的一些表生成了代碼了。所以我們可以通過在commons-manager-web的controller調(diào)用一個服務(wù)來驗(yàn)證一下服務(wù)是否能夠正常通訊并且訪問數(shù)據(jù)庫返回數(shù)據(jù)。
我們以ResourceController這個類,來寫一個請求方法來做校驗(yàn):


2018-10-19 11-10-41 的屏幕截圖.png

重啟項(xiàng)目的服務(wù),通過瀏覽器請求:http://localhost:8080/resource/testResource

2018-10-19 11-11-54 的屏幕截圖.png

可以看到,接口成功的返回了數(shù)據(jù)庫查詢到的數(shù)據(jù)了。

PS:mybatis-plus的代碼生成器,我已經(jīng)放到了項(xiàng)目commons-manager-dao的項(xiàng)目中了,這個代碼生成器,我是根據(jù)官網(wǎng)給出的代碼,做了一下相關(guān)的調(diào)整的,使得該生成器可以根據(jù)多模塊項(xiàng)目的目錄來對應(yīng)生成相關(guān)的文件,除了xml文件生成后我們需要手動移動到對應(yīng)的文件夾,其他的文件mapper,model,service,controller這些,我們度可以通過在代碼生成器的那個類里面設(shè),這樣我們在生成代碼后就不用再一個個的手動的移動了。具體的代碼大家可以去看一下那個類:

8120601-dfd04f9a000e788d.png
public class MybatisPlusUtils {

    public static void main(String[] args) {
        String[] models = {"commons-manager/commons-manager-dao", "commons-manager/commons-manager-pojo", "commons-manager/commons-manager-service",
                "commons-manager/commons-manager-interface", "commons-manager/commons-manager-web"};
        for (String model : models) {
            shell(model);
        }
    }

    private static void shell(String model) {
        File file = new File(model);
        String path = file.getAbsolutePath();
        System.out.println(path);
        //path = path.substring(0, path.lastIndexOf(File.separator));
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(path + "/src/main/java");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二級緩存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor("ChinPangLung");

        // 自定義文件命名,注意 %s 會自動填充表實(shí)體屬性!
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("I%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 數(shù)據(jù)源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert() {
            // 自定義數(shù)據(jù)庫表字段類型轉(zhuǎn)換【可選】
            @Override
            public DbColumnType processTypeConvert(String fieldType) {
                System.out.println("轉(zhuǎn)換類型:" + fieldType);
                // 注意?。rocessTypeConvert 存在默認(rèn)類型轉(zhuǎn)換,如果不是你要的效果請自定義返回、非如下直接返回。
                return super.processTypeConvert(fieldType);
            }
        });
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setUrl("jdbc:mysql:///managerDB?characterEncoding=utf8");
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setCapitalMode(true);// 全局大寫命名 ORACLE 注意
//        strategy.setTablePrefix(new String[]{"tlog_", "tsys_"});// 此處可以修改為您的表前綴
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setInclude(new String[]{"resource"}); // 需要生成的表
        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
        // 自定義實(shí)體父類
        //strategy.setSuperEntityClass("com.spf.model.Entity");
        // 自定義實(shí)體,公共字段
        //strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
        // 自定義 mapper 父類
        // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
        // 自定義 service 父類
        //strategy.setSuperServiceClass("com.baomidou.demo.TestService");
        // 自定義 service 實(shí)現(xiàn)類父類
        //strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
        // 自定義 controller 父類
        strategy.setSuperControllerClass("com.lung.common.controller.SuperController");
        // 【實(shí)體】是否生成字段常量(默認(rèn) false)
        // public static final String ID = "test_id";
        // strategy.setEntityColumnConstant(true);
        // 【實(shí)體】是否為構(gòu)建者模型(默認(rèn) false)
        // public User setName(String name) {this.name = name; return this;}
        // strategy.setEntityBuliderModel(true);
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.lung.application.test");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("api");
        pc.setServiceImpl("service");
        //pc.setModuleName("test");
        mpg.setPackageInfo(pc);

        // 注入自定義配置,可以在 VM 中使用 cfg.abc 【可無】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };

        // 自定義 xxList.jsp 生成
        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
//    focList.add(new FileOutConfig("/template/list.jsp.vm") {
//       @Override
//       public String outputFile(TableInfo tableInfo) {
//          // 自定義輸入文件名稱
//          return "D://my_" + tableInfo.getEntityName() + ".jsp";
//       }
//    });
//    cfg.setFileOutConfigList(focList);
//    mpg.setCfg(cfg);

        // 調(diào)整 xml 生成目錄演示
//        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
//            }
//        });
//        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 關(guān)閉默認(rèn) xml 生成,調(diào)整生成 至 根目錄
        TemplateConfig tc = new TemplateConfig();
        if ("commons-manager/commons-manager-dao".equals(model)) {
            tc.setController(null);
            tc.setEntity(null);
            tc.setService(null);
            tc.setServiceImpl(null);
//            tc.setXml(null);
        }
        /*else if ("commons-manager/commons-manager-service/src/main/resources/mapper".equals(model)) {
            PackageConfig packageInfo = mpg.getPackageInfo();
            packageInfo.setParent(null);
            packageInfo.setXml("xml");
            tc.setController(null);
            tc.setEntity(null);
            tc.setService(null);
            tc.setServiceImpl(null);
            tc.setMapper(null);
        }*/
        else if ("commons-manager/commons-manager-pojo".equals(model)) {
            tc.setController(null);
            tc.setService(null);
            tc.setServiceImpl(null);
            tc.setMapper(null);
            tc.setXml(null);
        } else if ("commons-manager/commons-manager-service".equals(model)) {
            tc.setController(null);
            tc.setMapper(null);
            tc.setService(null);
            tc.setXml(null);
            tc.setEntity(null);
        } else if ("commons-manager/commons-manager-interface".equals(model)) {
            tc.setController(null);
            tc.setMapper(null);
            tc.setServiceImpl(null);
            tc.setXml(null);
            tc.setEntity(null);
        } else if ("commons-manager/commons-manager-web".equals(model)) {
            tc.setMapper(null);
            tc.setXml(null);
            tc.setService(null);
            tc.setServiceImpl(null);
            tc.setEntity(null);
        }
        mpg.setTemplate(tc);

        // 自定義模板配置,可以 copy 源碼 mybatis-plus/src/main/resources/template 下面內(nèi)容修改,
        // 放置自己項(xiàng)目的 src/main/resources/template 目錄下, 默認(rèn)名稱一下可以不配置,也可以自定義模板名稱
        // TemplateConfig tc = new TemplateConfig();
        // tc.setController("...");
        // tc.setEntity("...");
        // tc.setMapper("...");
        // tc.setXml("...");
        // tc.setService("...");
        // tc.setServiceImpl("...");
        // 如上任何一個模塊如果設(shè)置 空 OR Null 將不生成該模塊。
        // mpg.setTemplate(tc);

        // 執(zhí)行生成
        mpg.execute();

        // 打印注入設(shè)置【可無】
        System.err.println(mpg.getCfg().getMap().get("abc"));
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容