kobe:"I don’t want to be the next Michael Jordan, I only want to be Kobe Bryant".
1.搭建环境
1.1工具
VMware12+CentOS6.5 64位+JDK1.8+Mysql5.5+Tomcat7+Zookeeper3.4.6+Idea2018+maven3.3.9
1.2安装zookeeper
cd /usr/soft
#使用工具上传“zookeeper-3.4.6.tar.gz”
#解压
tar -zxvf zookeeper-3.4.6.tar.gz -C /usr/local/
#创建data目录
cd /usr/local/zookeeper-3.4.6
mkdir data
#修改zoo.cfg
cd conf
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg
dataDir=/usr/local/zookeeper-3.4.6/data(新建的data目录所在的位置)
#启动
/usr/local/zookeeper-3.4.6/bin/zkServer.sh start
#查看状态
#查看状态
/usr/local/zookeeper-3.4.6/bin/zkServer.sh status
#启动前要关闭防火墙
#关闭命令:
service iptables stop
#永久关闭防火墙:
chkconfig iptables off
#查看防火墙的状态
service iptables status
1.3安装监控中心
#复制一份tomcat,用来部署监控中心
cp -r /usr/apache-tomcat-7.0.57 /usr/local/tomcat
cd /usr/local/tomcat/apache-tomcat-7.0.57/webapps/
#删除所有目录
rm -rf *
#新建一个ROOT目录
mkdir ROOT
#上传dubbo-admin-2.5.7.war 阿里提供
rz
#解压
jar -xvf dubbo-admin-2.5.7.war
#删除war包
rm -f dubbo-admin-2.5.7.war
#修改tomcat端口
#修改22行的server port端口为:8008
#修改71行的http port端口为:8088
#修改93行的connector port端口为:8018
vi /usr/local/tomcat/apache-tomcat-7.0.57/conf/server.xml
cd WEB-INF
vim dubbo.properties
#修改其中的dubbo.registry.address=zookeeper://127.0.0.1:2181为注册中心的实际地址(若注册中心和监控中心在同一##台机器则上述地址可以不用修改了)因为我的zookeeper和监控中心是在同一台机器上,所以不做修改。
#启动tomcat
/usr/local/web/tomcat-dubbo-monitor/bin/startup.sh
#在window上访问
#http://192.168.46.130:8088
#用户名:root 密码:root
在window下访问监控中心
2.项目结构
2.1.创建springboot-dubbo-interface
1.提供接口
package cn.gobon.service; import cn.gobon.pojo.Department; /** * @version V1.0 * @ClassName: ValidateActionJson * @Description: TODO * @Auther: Jianxiong Yi * @Date: 2018/8/19 13:34 */ public interface DepartmentServcie { public String sayHi(String str); public Department getDepartmentById(int id); }
2.实体类(这里要注意的是,要实现序列化接口,否则后面访问报错)
package cn.gobon.pojo; import java.io.Serializable; /** * @version V1.0 * @ClassName: ValidateActionJson * @Description: TODO * @Auther: Jianxiong Yi * @Date: 2018/8/19 13:35 */ public class Department implements Serializable { private Integer id; private String name; private String descr; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescr() { return descr; } public void setDescr(String descr) { this.descr = descr; } }
到这里接口项目完成,利用maven工具,install到本地仓库
2.2创建springboot-dubbo-service(依赖springboot-dubbo-interface项目)
1.导入依赖
<?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>cn.gobon</groupId> <artifactId>springboot-dubbo-service</artifactId> <version>1.0-SNAPSHOT</version> <!-- 本次使用springboot2.0.3版本,在1.5.1版本整合,发现有的包找不到--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--dubbo 依赖--> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>1.0.2</version> </dependency> <!-- zookeeper客户端连接 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!--mybatis整合springboot--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- Mysql数据库连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 导入接口项目依赖 --> <dependency> <groupId>cn.gonbon</groupId> <artifactId>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> <!-- 为了解决用idea开发,能找到mapper接口和xml --> <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> </build> </project>
2.mapper接口和xml文件
package cn.gobon.mapper; import cn.gobon.pojo.Department; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface DepartmentMapper { int deleteByPrimaryKey(Integer id); int insert(Department record); int insertSelective(Department record); Department selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Department record); int updateByPrimaryKey(Department record); //查询所有部门 List<Department> findAll(); }
<?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="cn.gobon.mapper.DepartmentMapper" > <resultMap id="BaseResultMap" type="cn.gobon.pojo.Department" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="descr" property="descr" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, name, descr </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from department where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from department where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="cn.gobon.pojo.Department" > insert into department (id, name, descr ) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{descr,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="cn.gobon.pojo.Department" > insert into department <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="name != null" > name, </if> <if test="descr != null" > descr, </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="descr != null" > #{descr,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="cn.gobon.pojo.Department" > update department <set > <if test="name != null" > name = #{name,jdbcType=VARCHAR}, </if> <if test="descr != null" > descr = #{descr,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="cn.gobon.pojo.Department" > update department set name = #{name,jdbcType=VARCHAR}, descr = #{descr,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <select id="findAll" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from department </select> </mapper>
3.服务接口实现类(这里要注意的是service的包不要导错)
package cn.gobon.service.impl; import cn.gobon.mapper.DepartmentMapper; import cn.gobon.pojo.Department; import cn.gobon.service.DepartmentServcie; import com.alibaba.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * @version V1.0 * @ClassName: ValidateActionJson * @Description: TODO * @Auther: Jianxiong Yi * @Date: 2018/8/19 13:48 */ @Component @Service public class DepartmentServiceImpl implements DepartmentServcie { @Autowired private DepartmentMapper departmentMapper; @Override public String sayHi(String str) { return "springboot dubbo" + str; } @Override public Department getDepartmentById(int id) { return departmentMapper.selectByPrimaryKey(id); } }
3.Application.java启动类
package cn.gobon; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @version V1.0 * @ClassName: ValidateActionJson * @Description: springboot启动类 * @Auther: Jianxiong Yi * @Date: 2018/8/11 15:17 */ @SpringBootApplication @EnableDubboConfiguration//开启springboot dubbo注解的支持 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
4.application.properties
#指定端口号和上下文路径 server.port=8080 #指定mapper配置文件路径 mybatis.mapper-locations=classpath:cn/gobon/mapper/*.xml #配置数据库连接信息 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8 #dubbo配置信息 #服务名称,不重复即可 spring.dubbo.appname=springboot-dubbo-service #zookeeper的地址和端口 spring.dubbo.registry=zookeeper://192.168.46.130:2181
5.运行启动类
没有报错则为成功
2.3创建springboot-dubbo-consumer
1.导入依赖
<?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>cn.gobon</groupId> <artifactId>springboot-dubbo-consumer</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--dubbo 依赖--> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>1.0.2</version> </dependency> <!-- zookeeper客户端连接 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>cn.gonbon</groupId> <artifactId>springboot-dubbo-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
2.application.properties
#消费者端口 server.port=9090 #dubbo配置信息 spring.dubbo.appname=springboot-dubbo-consumer spring.dubbo.registry=zookeeper://192.168.46.130:2181
3.编写controller
package cn.gobon.controller; import cn.gobon.pojo.Department; import cn.gobon.service.DepartmentServcie; import com.alibaba.dubbo.config.annotation.Reference; import org.apache.zookeeper.data.Id; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @version V1.0 * @ClassName: ValidateActionJson * @Description: TODO * @Auther: Jianxiong Yi * @Date: 2018/8/19 15:24 */ @RestController public class DepartmentController { @Reference private DepartmentServcie departmentServcie; @RequestMapping("/getDepartmentById/{id}") public Department getDepartmentById(@PathVariable("id") Integer id){ return departmentServcie.getDepartmentById(id); } }
4.Application.java启动类
package cn.gobon; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; /** * @version V1.0 * @ClassName: ValidateActionJson * @Description: TODO * @Auther: Jianxiong Yi * @Date: 2018/8/19 15:23 */ @SpringBootApplication @EnableDubboConfiguration //开启 springboot dubbo注解的支持 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
5.启动测试
本人实力有限,若有错误,欢迎指点!