springboot-dubbo and Thymeleaf使用

SpringBoot集成Dubbo

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

1 创建接口模块

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

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

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

2 创建服务提供者模块

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

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

	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
        <dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-devtoolsartifactId>
			<scope>runtimescope>
			<optional>trueoptional>
		dependency>
		<dependency>
			<groupId>org.mybatis.spring.bootgroupId>
			<artifactId>mybatis-spring-boot-starterartifactId>
			<version>2.2.0version>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>mysqlgroupId>
			<artifactId>mysql-connector-javaartifactId>
			<scope>runtimescope>
		dependency>

		
		<dependency>
			<groupId>com.alibaba.spring.bootgroupId>
			<artifactId>dubbo-spring-boot-starterartifactId>
			<version>2.0.0version>
		dependency>
		
		<dependency>
			<groupId>com.101tecgroupId>
			<artifactId>zkclientartifactId>
			<version>0.11version>
		dependency>

		
		<dependency>
			<groupId>com.bjpowernodegroupId>
			<artifactId>mycinema-interfaceartifactId>
			<version>1.0.0version>
		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逆向工程的插件

          
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.7version>
                <configuration>
                    
                    <configurationFile>GeneratorMapper.xmlconfigurationFile>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
            plugin>

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


DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    
    <classPathEntry location="C:\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/>
    
    <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>

        
        <javaModelGenerator targetPackage="com.bjpowernode.model"
                            targetProject="E:\mycinema-interface\src\main\java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="false" />
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="com.bjpowernode.mapper"                targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />  
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.bjpowernode.mapper"    targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>

        
        <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/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                    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.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-devtoolsartifactId>
			<scope>runtimescope>
			<optional>trueoptional>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>

		
		<dependency>
			<groupId>com.alibaba.spring.bootgroupId>
			<artifactId>dubbo-spring-boot-starterartifactId>
			<version>2.0.0version>
		dependency>
		
		<dependency>
			<groupId>com.101tecgroupId>
			<artifactId>zkclientartifactId>
			<version>0.10version>
		dependency>
		
		<dependency>
			<groupId>com.bjpowernodegroupId>
			<artifactId>mycinema-interfaceartifactId>
			<version>1.0.0version>
		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
springboot-dubbo and Thymeleaf使用_第1张图片

3.6 集成Thymeleaf实现界面

(1)添加Thymeleaf坐标

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-thymeleafartifactId>
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>Titletitle>
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>

你可能感兴趣的:(dubbo,spring,boot,java)