dubbo小项目2
1、項(xiàng)目結(jié)構(gòu)搭建
父項(xiàng)目pom.xml
2、實(shí)體類型工程
pom.xml
<?xml version="1.0" encoding="UTF-8"?>-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">-<parent><artifactId>dubbo_cms</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>dubbo_cms_pojo</artifactId><version>1.0-SNAPSHOT</version></project>實(shí)體類:
3、mapper數(shù)據(jù)訪問工程
pom.xml
<?xml version="1.0" encoding="UTF-8"?>-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">-<parent><artifactId>dubbo_cms</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>dubbo_cms_mapper</artifactId><version>1.0-SNAPSHOT</version>-<dependencies>-<dependency><groupId>com.bjsxt</groupId><artifactId>dubbo_cms_pojo</artifactId><version>1.0-SNAPSHOT</version></dependency>-<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency>-<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies></project>com.bjsxt.mapper
resources:
1.application-db.yml
2.com.bjsxt.mapper
4、服務(wù)標(biāo)準(zhǔn)工程
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">-<parent><artifactId>dubbo_cms</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>dubbo_cms_serviceapi</artifactId><version>1.0-SNAPSHOT</version>-<dependencies>-<dependency><groupId>com.bjsxt</groupId><artifactId>dubbo_cms_pojo</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>2.com\bjsxt\cms\serviceapi
package com.bjsxt.cms.serviceapi;import com.bjsxt.pojo.CmsType;import java.util.List;/*** 服務(wù)接口。代表dubbo遠(yuǎn)程訪問的標(biāo)準(zhǔn)*/ public interface CmsTypeServiceAPI {// 新增數(shù)據(jù)int save(CmsType type);// 根據(jù)主鍵更新數(shù)據(jù)int modify(CmsType type);// 查詢所有List<CmsType> findAll(); }5、provider服務(wù)提供者實(shí)現(xiàn)
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">-<parent><artifactId>dubbo_cms</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>dubbo_cms_provider</artifactId><version>1.0-SNAPSHOT</version>-<dependencies>-<dependency><groupId>com.bjsxt</groupId><artifactId>dubbo_cms_serviceapi</artifactId><version>1.0-SNAPSHOT</version></dependency>-<dependency><groupId>com.bjsxt</groupId><artifactId>dubbo_cms_mapper</artifactId><version>1.0-SNAPSHOT</version></dependency>-<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency>-<dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency>-<dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId></dependency>-<dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId></dependency></dependencies></project>2.java\com\bjsxt\cms Provider啟動(dòng)類
package com.bjsxt.cms;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication @EnableDubbo @MapperScan(basePackages = {"com.bjsxt.mapper"}) public class CmsProviderApp {public static void main(String[] args) {SpringApplication.run(CmsProviderApp.class, args);} }3.com\bjsxt\cms\service\impl服務(wù)實(shí)現(xiàn)
package com.bjsxt.cms.service.impl;import com.bjsxt.cms.serviceapi.CmsTypeServiceAPI; import com.bjsxt.mapper.CmsTypeMapper; import com.bjsxt.pojo.CmsType; import org.apache.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired;import java.util.List;/*** Dubbo服務(wù)提供者*/ @Service public class CmsTypeServiceImpl implements CmsTypeServiceAPI {// mapper對(duì)象注入@Autowiredprivate CmsTypeMapper cmsTypeMapper;// 新增數(shù)據(jù)到數(shù)據(jù)庫@Overridepublic int save(CmsType type) {return cmsTypeMapper.insertSelective(type);}// 根據(jù)主鍵更新數(shù)據(jù)庫數(shù)據(jù)@Overridepublic int modify(CmsType type) {return cmsTypeMapper.updateByPrimaryKeySelective(type);}// 查詢所有數(shù)據(jù)@Overridepublic List<CmsType> findAll() {return cmsTypeMapper.selectByExample(null);} }4.配置文件application.yml
dubbo:application:name: dubbo-cms-providerregistry:address: zookeeper://192.168.89.140:2181protocol:port: 20880name: dubbospring:profiles:active: db6、consumer服務(wù)消費(fèi)者實(shí)現(xiàn)
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">-<parent><artifactId>dubbo_cms</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>dubbo_cms_consumer</artifactId><version>1.0-SNAPSHOT</version>-<dependencies>-<dependency><groupId>com.bjsxt</groupId><artifactId>dubbo_cms_serviceapi</artifactId><version>1.0-SNAPSHOT</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-thymeleaf</artifactId></dependency>-<dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency>-<dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId></dependency>-<dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId></dependency></dependencies></project>2.com\bjsxt\cms\client\controller
package com.bjsxt.cms.client.controller;import com.bjsxt.cms.client.service.CmsTypeService; import com.bjsxt.pojo.CmsType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;import java.util.List;/*** 服務(wù)消費(fèi)者控制器。與客戶直接交互。提供控制邏輯*/ @Controller public class CmsTypeController {@Autowiredprivate CmsTypeService cmsTypeService;/*** 查詢所有的CmsType數(shù)據(jù),通過Model作用域傳遞到視圖,做顯示。* @param model - 作用域* @return*/@GetMapping("/findAll")public String findAll(Model model){List<CmsType> list = cmsTypeService.findAll();model.addAttribute("list", list);return "list";} }3.service服務(wù)實(shí)現(xiàn)com\bjsxt\cms\clientservice
package com.bjsxt.cms.client.service;import com.bjsxt.pojo.CmsType;import java.util.List;// 本地服務(wù)接口 public interface CmsTypeService {// 查詢所有CmsType數(shù)據(jù)List<CmsType> findAll(); } package com.bjsxt.cms.client.service.impl;import com.bjsxt.cms.client.service.CmsTypeService; import com.bjsxt.cms.serviceapi.CmsTypeServiceAPI; import com.bjsxt.pojo.CmsType; import org.apache.dubbo.config.annotation.Reference; import org.springframework.stereotype.Service;import java.util.List;/*** 本地服務(wù)實(shí)現(xiàn)*/ @Service public class CmsTypeServiceImpl implements CmsTypeService {@Referenceprivate CmsTypeServiceAPI serviceAPI;// 遠(yuǎn)程訪問provider中的方法,實(shí)現(xiàn)數(shù)據(jù)查詢。@Overridepublic List<CmsType> findAll() {return serviceAPI.findAll();} }4.啟動(dòng)類
package com.bjsxt.cms.client;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication @EnableDubbo public class CmsTypeConsumerApp {public static void main(String[] args) {SpringApplication.run(CmsTypeConsumerApp.class, args);} }5.配置
5.1、application.yml
5.2、resources\templates\list.html
總結(jié)
- 上一篇: 幺爸什么意思 幺爸是什么方言
- 下一篇: 图片管理应用