一、环境的要求
在linux上搭建zookeeper (本项目采用的是zookeeper-3.4.14) 在linux上tomcat下部署dubbo-admin(dubbo-admin-2.5.4) springboot版本2.1.4.RELEASE,dubbo版本2.0.0,zkclient版本0.8
注:
如果项目运行没有报错误,但是服务者和消费者无法注入zookeeper可能该版本有问题,可以试一下换其他的版本号
二、微服务项目的搭建
搭建springboot-dubbo-interface模块
新建springboot的服务接口springboot-bubbo-interface
新建springboot-bubbo-interface完成后的目录结构
pom.xml的代码块
<? xml version ="1.0" encoding ="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0.0 </modelVersion> <groupId> com.demo.springboot </groupId> <artifactId> 01-springboot-dubbo-interface </artifactId> <version> 1.0-SNAPSHOT </version> </project>
在springboot-dubbo-interface下面新建model和service包
student类:
package com.demo.springboot.model;
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
//getter和setter方法
}
StudentService接口
package com.demo.springboot.service;
import com.demo.springboot.model.Student;
public interface StudentService {
public String sayHi(String name);
public Student getStudent(int id);
}
将springboot-bubbo-interface打包jar包
搭建springboot-dubbo-provider模
服务的提供者springboot-dubbo-provider
新建完成后的springboot-dubbo-provider的目录结构如下图所示
新建mapper和service的包以及其中用的类,目录结构如下
代码:
StudentMapper类
package com.demo.springboot.mapper ; import com.demo.springboot.model.Student ; import org.apache.ibatis.annotations. Mapper ; import java.util.List ; @Mapper // 把它变成一个spring的一个bean public interface StudentMapper { int deleteByPrimaryKey ( Integer id) ; int insert ( Student record) ; int insertSelective ( Student record) ; Student selectByPrimaryKey ( Integer id) ; int updateByPrimaryKeySelective ( Student record) ; List < Student > selectAllStudent () ; int updateByPrimaryKey ( Student record) ; }
StudentMappe.xml
<? xml version ="1.0" encoding ="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.demo.springboot.mapper.StudentMapper" > <resultMap id ="BaseResultMap" type ="com.demo.springboot.model.Student" > <id column ="id" jdbcType ="INTEGER" property ="id" /> <result column ="name" jdbcType ="VARCHAR" property ="name" /> <result column ="age" jdbcType ="INTEGER" property ="age" /> </resultMap> <sql id ="Base_Column_List" > id, name, age </sql> <!-- 查询所有的学生--> <select id ="selectAllStudent" resultMap ="BaseResultMap" > select <include refid ="Base_Column_List" /> from student </select> <select id ="selectByPrimaryKey" parameterType ="java.lang.Integer" resultMap ="BaseResultMap" > select <include refid ="Base_Column_List" /> from student where id = #{id,jdbcType=INTEGER} </select> <delete id ="deleteByPrimaryKey" parameterType ="java.lang.Integer" > delete from student where id = #{id,jdbcType=INTEGER} </delete> <insert id ="insert" parameterType ="com.demo.springboot.model.Student" > insert into student (id, name, age ) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER} ) </insert> <insert id ="insertSelective" parameterType ="com.demo.springboot.model.Student" > insert into student <trim prefix ="(" suffix =")" suffixOverrides ="," > <if test ="id != null" > id, </if> <if test ="name != null" > name, </if> <if test ="age != null" > age, </if> </trim> <trim prefix ="values (" suffix =")" suffixOverrides ="," > <if test ="id != null" > #{id,jdbcType=INTEGER}, </if> <if test ="name != null" > #{name,jdbcType=VARCHAR}, </if> <if test ="age != null" > #{age,jdbcType=INTEGER}, </if> </trim> </insert> <update id ="updateByPrimaryKeySelective" parameterType ="com.demo.springboot.model.Student" > update student <set> <if test ="name != null" > name = #{name,jdbcType=VARCHAR}, </if> <if test ="age != null" > age = #{age,jdbcType=INTEGER}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id ="updateByPrimaryKey" parameterType ="com.demo.springboot.model.Student" > update student set name = #{name,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} </update> </mapper>
UserServiceImpl
package com.demo.springboot.service.impl ; import com.alibaba.dubbo.config.annotation. Service ; import com.demo.springboot.mapper.StudentMapper ; import com.demo.springboot.model.Student ; import com.demo.springboot.service.StudentService ; import org.springframework.beans.factory.annotation. Autowired ; import org.springframework.stereotype. Component ; @Component // 注解成一个spring的一个bean,即该注解是spring的 @Service ( version = "1.0.0" , timeout = 10000 ) // 该注解是dubbo <dubbo:service interface=... ref= version => public class UserServiceImpl implements StudentService { @Autowired private StudentMapper studentMapper ; @Override public String sayHi ( String name) { return "Hi, Springboot " +name ; } @Override public Student getStudent ( int id) { System . out .println( " 查询ID =" +id) ; return studentMapper .selectByPrimaryKey(id) ; } }
Pom.xml
<? xml version ="1.0" encoding ="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0.0 </modelVersion> <parent> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 2.1.4.RELEASE </version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId> com.demo.springboot </groupId> <artifactId> 01-springboot-dubbo-provider </artifactId> <version> 0.0.1-SNAPSHOT </version> <name> 01-springboot-dubbo-provider </name> <description> project for Spring Boot </description> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-web </artifactId> </dependency> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-test </artifactId> <scope> test </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.8 </version> </dependency> <!--dubbo 接口项目的jar依赖--> <dependency> <groupId> com.demo.springboot </groupId> <artifactId> 01-springboot-dubbo-interface </artifactId> <version> 1.0-SNAPSHOT </version> </dependency> <!-- 加载mybatis整合springboot--> <dependency> <groupId> org.mybatis.spring.boot </groupId> <artifactId> mybatis-spring-boot-starter </artifactId> <version> 1.2.0 </version> </dependency> <!--MYSQL 的jdbc驱动包--> <dependency> <groupId> mysql </groupId> <artifactId> mysql-connector-java </artifactId> <version> 5.1.43 </version> </dependency> </dependencies> <build> <resources> <resource> <directory> src/main/java </directory> <includes> <include> **/*.xml </include> </includes> </resource> <resource> <directory> src/main/resources </directory> <includes> <include> **/*.* </include> </includes> </resource> <resource> <directory> src/main/webapp </directory> <targetPath> META-INF/resources </targetPath> <includes> <include> **/*.* </include> </includes> </resource> </resources> <plugins> <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build> </project>
Application.properties
# 内嵌的Tomcat服务端口 server.port = 8085 #Dubbo 配置,要自己搭建的dubbo里面的配置一致 dubbo.application.name = provider dubbo.registry.address = zookeeper://192.168.101.22:2181 spring.datasource.url = jdbc:mysql://localhost:3306/springdb?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.username = root spring.datasource.password = 123456 spring.datasource.driver-class-name = com . mysql . jdbc . Driver
Application
package com.demo.springboot ; import com.alibaba.dubbo.config.spring.context.annotation. EnableDubbo ; import org.springframework.boot.SpringApplication ; import org.springframework.boot.autoconfigure. SpringBootApplication ; @SpringBootApplication @EnableDubbo // 开启dubbo的自动配置 public class Application { public static void main ( String [] args) { SpringApplication .run ( Application . class, args) ; } }
搭建springboot-dubbo-consumer模块
新建springboot-dubbo-consumer
springboot-dubbo-consumer模块创建和springboot-dubbo-provider的创建步骤一样
目录结构如下
StudentController
package com.demo.springboot.controller ; import com.alibaba.dubbo.config.annotation. Reference ; import com.demo.springboot.service.StudentService ; import org.springframework.web.bind.annotation. RequestMapping ; import org.springframework.web.bind.annotation. RequestParam ; import org.springframework.web.bind.annotation. RestController ; @RestController public class StudentController { @Reference ( version = "1.0.0" ) //<dubbo:reference id=""> private StudentService studentService ; @RequestMapping ( "/boot/student" ) public Object getStudent ( @RequestParam ( "id" ) Integer id){ System . out .println( " 查询学生的ID =" +id) ; return studentService .getStudent(id) ; } }
Application
package com.demo.springboot ; import com.alibaba.dubbo.config.spring.context.annotation. EnableDubbo ; import com.alibaba.dubbo.config.spring.context.annotation. EnableDubboConfig ; import org.springframework.boot.SpringApplication ; import org.springframework.boot.autoconfigure. SpringBootApplication ; @SpringBootApplication //@EnableDubboConfig // 开启Dubbo自动配置的支持 @EnableDubbo public class Application { public static void main ( String [] args) { SpringApplication .run ( Application . class, args) ; } }
Application.properties
# 内嵌的Tomcat服务端口 server.port = 9090 #Dubbo 配置 dubbo.application.name = customer dubbo.registry.address = zookeeper://192.168.101.22:2181
Pom.xml
<? xml version ="1.0" encoding ="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0.0 </modelVersion> <parent> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 2.1.4.RELEASE </version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId> com.demo.springboot </groupId> <artifactId> 01-springboot-dubbo-consumer </artifactId> <version> 0.0.1-SNAPSHOT </version> <name> 01-springboot-dubbo-consumer </name> <description> project for Spring Boot </description> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-web </artifactId> </dependency> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-test </artifactId> <scope> test </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.8 </version> </dependency> <!--dubbo 接口项目的jar依赖--> <dependency> <groupId> com.demo.springboot </groupId> <artifactId> 01-springboot-dubbo-interface </artifactId> <version> 1.0-SNAPSHOT </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build> </project>
运行步骤
运行linux上的zookeeper 运行linux上tomcat(dubbo-admin-5.4放在tomcat下面) 运行springboot-dubbo-provider 运行springboot-dubbo-consumer 运行的结果
三、注意事项
注意版本是否兼容 注意项目导入的包是dubbo的包还是springboot的里面的包
四、遇到的问题
针对该微服务搭建时遇到的情况
问题一:项目运行没有报错但是服务无法注入到dubbo中
原因:我用@EnableDubboConfig 注解在启动项目,但是控制台没有关于Dubbo其他的打印
解决办法:把@EnableDubboConfig换成@EnableDubbo(这两者有啥区别?)
问题二:Fail to start server(url: dubbo://192.168.101.1:20880/com.demo.springbo........
原因:pom.xml的zkcliet的jar的版本不太适合导致无法注入
解决的办法:更换zkclient的版本
详细代码见:https://download.csdn.net/download/u012918886/11106448
(很奇怪,CSDN上传的文件默认是5个积分,不能设置。如果嫌积分比较高,直接看上面代码)