springboot-dubbo and Thymeleaf使用

SpringBoot集成Dubbo

使用SpringBoot搭建基于SSM的分布式框架。按照dubbo官方的推荐,一个基于dubbo的微服务项目至少包含三个模块。接口模块,生产者模块,消费者模块。

1 创建接口模块

1.1 基于 Maven 创建服务接口(mycinema-interface)

指定打包方式和编译级别:

    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

2 创建服务提供者模块

2.1 创建基于SpringBoot的提供者web项目,提供数据服务

2.2 添加依赖:web、mybatis、mysql、dubbo 以及 zookeeper 等核心依赖

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.2.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!--springboot集成dubbo-->
		<dependency>
			<groupId>com.alibaba.spring.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>
		<!--zookeeper客户端依赖-->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.11</version>
		</dependency>

		<!--myincema服务接口依赖-->
		<dependency>
			<groupId>com.bjpowernode</groupId>
			<artifactId>mycinema-interface</artifactId>
			<version>1.0.0</version>
		</dependency>
	</dependencies>

2.3 配置application.properties/yml,设置数据库连接,dubbo服务等信息

#数据库信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mycinema?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

#设置内嵌 Tomcat 端口号
server.port=8090
#设置上下文根
server.servlet.context-path=/

#配置 dubbo 的服务提供者信息
#服务提供者应用名称(必须写,且不能重复)
spring.application.name=mycinema-provider
#设置当前工程为服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181

2.4 使用逆向工程生成实体类,数据访问接口,以及sql-mapper映射文件

(1)在pom的<plugins>中添加MyBatis逆向工程的插件

          <!--mybatis代码自动生成插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>GeneratorMapper.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

(2)在项目的根目录下面添加插件的配置文件GeneratorMapper.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径 -->
    <classPathEntry location="C:\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/>
    <!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
    <context id="tables" targetRuntime="MyBatis3">

        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/mycinema"
                        userId="root"
                        password="">
        </jdbcConnection>

        <!-- 生成model类,targetPackage指定model类的包名, 
             targetProject指定生成的model放在接口工程下面-->
        <javaModelGenerator targetPackage="com.bjpowernode.model"
                            targetProject="E:\mycinema-interface\src\main\java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="false" />
        </javaModelGenerator>

        <!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, 
           targetProject指定生成的mapper.xml放在当前工程src -->
        <sqlMapGenerator targetPackage="com.bjpowernode.mapper"                targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />  
        </sqlMapGenerator>

        <!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, 
          targetProject指定生成的Mapper接口放当前工程src目录 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.bjpowernode.mapper"    targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 数据库表名及对应的Java模型类名 -->
        <table tableName="category" domainObjectName="Category"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="movie" domainObjectName="Movie"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

        <table tableName="user" domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

(3)在maven窗口中执行插件,生成代码

注意:执行插件时,会解析pom文件中的所有依赖,已经导入了 mycinema-interface 的依赖,所以会去本地仓库查找这个包,找不到就会报错。解决方法如下:

​ 1)可以先注释这个包依赖,等生成代码之后再添加

​ 2)将mycinema-interface项目打包安装到本地仓库mvn install

​ 3)将生成的实体类都实现序列化接口,用于远程传输数据

public class Movie implements Serializable { }

​ 4)插件生成的Mapper.xml文件和Mapper接口在同一个包下,但是idea不会编译,需要手动指定资源文件,修改

提供者mycinema-provider的pom.xml文件,手动指定资源路径

       		<resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
            </resources>

(4)在服务接口工程mycinema-interface工程中添加接口内容

public interface CategoryService {
    List<Category> getAll();
    Category getById(Integer id);
}
public interface MovieService {
    List<Movie> getAll();
    Movie getById(Integer id);
}

(5)在提供者工程mycinema-provider中创建业务的实现包service.impl实现业务接口,并添加业务注解

@Component,dubbo服务注解@Service

@Component
@Service(interfaceClass = MovieService.class,version = "1.0.0",timeout = 15000)
public class MovieServiceImpl implements MovieService {    
    @Autowired    
    private MovieMapper movieMapper;    
    @Override    
    public List<Movie> queryAll() {        
        return movieMapper.selectAll();    
    }    
    @Override    
    public Movie queryById(Integer id) {        
        return movieMapper.selectByPrimaryKey(id);    
    }
}
@Component
@Service(interfaceClass = CategoryService.class,timeout = 15000,version = "1.0.0")
public class CategoryServiceImpl implements CategoryService {    
    @Autowired    
    private CategoryMapper categoryMapper;    
    @Override    
    public List<Category> queryAll() {        
        return categoryMapper.selectAll();    
    }    
    @Override    
    public Category queryById(Integer id) {        
        return categoryMapper.selectByPrimaryKey(id);    
    }
}

2.5 开启Dubbo配置——@EnableDubboConfiguration

在启动类上开启dubbo配置支持注解(@EnableDubboConfiguration

@SpringBootApplication
@MapperScan("com.bjpowernode.mapper") //扫描数据访问接口
@EnableDubboConfiguration  			//开启dubbo注解支持
public class MycinemaProviderApplication {    
    public static void main(String[] args) {        
        SpringApplication.run(MycinemaProviderApplication.class, args);    
    }
}

开启zooker服务,启动提供者项目

3.创建服务消费者项目

3.1 创建基于SpringBoot的web项目,添加以下必要的依赖

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--dubbo依赖-->
		<dependency>
			<groupId>com.alibaba.spring.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>
		<!--zookeeper客户端依赖-->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.10</version>
		</dependency>
		<!--服务接口依赖-->
		<dependency>
			<groupId>com.bjpowernode</groupId>
			<artifactId>mycinema-interface</artifactId>
			<version>1.0.0</version>
		</dependency>
	</dependencies>

3.2 修改application.properties添加配置信息

#设置内嵌 Tomcat 端口号
server.port=8080
#设置上下文根
server.servlet.context-path=/
#设置 dubbo 配置
#设置服务消费者名称
spring.application.name=mycinema-consumer
#配置 dubbo 注册中心
spring.dubbo.registry=zookeeper://localhost:2181

3.3 添加Controller,依赖服务提供者提供的服务信息

@Controller
public class IndexController {
    //组件由dubbo提供,使用@Reference注解
    @Reference(interfaceClass = MovieService.class,version = "1.0.0")
    private MovieService movieService;

    @RequestMapping("/api/movies")
    @ResponseBody
    public List<Movie> findAqueryAllll(){
        return movieService.queryAll();
    }
}

3.4 开启Dubbo配置——@EnableDubboConfiguration

@EnableDubboConfiguration
@SpringBootApplication
public class WebApplication {
	public static void main(String[] args) {
		SpringApplication.run(WebApplication.class, args);
	}
}

3.5 测试

开启zookeeper 再启动provide和comsumer
在这里插入图片描述

3.6 集成Thymeleaf实现界面

(1)添加Thymeleaf坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

(2)添加视图配置

spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/

(3)编写控制器

  	@GetMapping("/index")
    public String index(Model model){
        model.addAttribute("movies",movieService.getAll());
        return "index";
    }

(4)在templates下面添加index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table>
        <tr><td>编号</td><td>名称</td><td>导演</td><td>日期</td></tr>
        <tr th:each="movie:${movies}">
            <td th:text="${movie.id}"></td>
            <td th:text="${movie.title}"></td>
            <td th:text="${movie.director}"></td>
            <td th:text="${movie.datereleased}"></td>
        </tr>
    </table>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huangshaohui00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值