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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjsxt</groupId>
<artifactId>dubbo_cms</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
-<modules>
<module>dubbo_cms_pojo</module>
<module>dubbo_cms_mapper</module>
<module>dubbo_cms_serviceapi</module>
<module>dubbo_cms_provider</module>
<module>dubbo_cms_consumer</module>
</modules>
-<dependencyManagement>
-<dependencies>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
-<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- dubbo和Springboot整合使用的启动器 -->
-<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.5</version>
</dependency>
<!-- dubbo使用Zookeeper作为注册中心时,必须依赖的资源。 -->
-<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
-<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2、实体类型工程
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>
实体类:
3、mapper数据访问工程
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
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ry?serverTimezone=GMT%2B8
username: root
password: root
mybatis:
mapper-locations: classpath:com/bjsxt/mapper/*.xml
2.com.bjsxt.mapper
4、服务标准工程
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;
/**
* 服务接口。代表dubbo远程访问的标准
*/
public interface CmsTypeServiceAPI {
// 新增数据
int save(CmsType type);
// 根据主键更新数据
int modify(CmsType type);
// 查询所有
List<CmsType> findAll();
}
5、provider服务提供者实现
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启动类
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服务实现
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服务提供者
*/
@Service
public class CmsTypeServiceImpl implements CmsTypeServiceAPI {
// mapper对象注入
@Autowired
private CmsTypeMapper cmsTypeMapper;
// 新增数据到数据库
@Override
public int save(CmsType type) {
return cmsTypeMapper.insertSelective(type);
}
// 根据主键更新数据库数据
@Override
public int modify(CmsType type) {
return cmsTypeMapper.updateByPrimaryKeySelective(type);
}
// 查询所有数据
@Override
public List<CmsType> findAll() {
return cmsTypeMapper.selectByExample(null);
}
}
4.配置文件application.yml
dubbo:
application:
name: dubbo-cms-provider
registry:
address: zookeeper://192.168.89.140:2181
protocol:
port: 20880
name: dubbo
spring:
profiles:
active: db
6、consumer服务消费者实现
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;
/**
* 服务消费者控制器。与客户直接交互。提供控制逻辑
*/
@Controller
public class CmsTypeController {
@Autowired
private CmsTypeService cmsTypeService;
/**
* 查询所有的CmsType数据,通过Model作用域传递到视图,做显示。
* @param model - 作用域
* @return
*/
@GetMapping("/findAll")
public String findAll(Model model){
List<CmsType> list = cmsTypeService.findAll();
model.addAttribute("list", list);
return "list";
}
}
3.service服务实现com\bjsxt\cms\clientservice
package com.bjsxt.cms.client.service;
import com.bjsxt.pojo.CmsType;
import java.util.List;
// 本地服务接口
public interface CmsTypeService {
// 查询所有CmsType数据
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;
/**
* 本地服务实现
*/
@Service
public class CmsTypeServiceImpl implements CmsTypeService {
@Reference
private CmsTypeServiceAPI serviceAPI;
// 远程访问provider中的方法,实现数据查询。
@Override
public List<CmsType> findAll() {
return serviceAPI.findAll();
}
}
4.启动类
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
dubbo:
application:
name: dubbo-cms-consumer
registry:
address: zookeeper://192.168.89.140:2181
server:
port: 80
5.2、resources\templates\list.html