springboot+dubbo+zookeeper微服务项目

一、环境的要求

  1. 在linux上搭建zookeeper (本项目采用的是zookeeper-3.4.14)
  2. 在linux上tomcat下部署dubbo-admin(dubbo-admin-2.5.4)
  3. 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>

 

 

  • 运行结果

运行步骤

  1. 运行linux上的zookeeper
  2. 运行linux上tomcat(dubbo-admin-5.4放在tomcat下面)
  3. 运行springboot-dubbo-provider
  4. 运行springboot-dubbo-consumer
  5. 运行的结果

 

 

 

 

三、注意事项

  1. 注意版本是否兼容
  2. 注意项目导入的包是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个积分,不能设置。如果嫌积分比较高,直接看上面代码)

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一个开源的Java框架,用于快速构建独立的、基于Spring的应用程序。它简化了Spring应用程序的配置和部署过程,并提供了许多嵌入式服务器,如Tomcat、Jetty等。Spring Boot还提供了自动配置的特性,可以根据类路径中的依赖自动配置Spring应用程序。 Dubbo是一款高性能的分布式服务框架,也是阿里巴巴开源的项目。它提供了服务治理、远程通讯和分布式调用等功能,帮助开发人员轻松构建分布式服务化的应用。 Zookeeper是一个开源的分布式协调服务,可以用于实现分布式应用程序的一致性和可靠性。它提供了一个类似于文件系统的层次化的命名空间,并允许开发人员注册、协调和共享各种资源,如配置信息、服务注册和发现等。 当使用Spring Boot结合DubboZookeeper时,可以构建一个高性能、可扩展和可靠的微服务架构。Spring Boot提供了便利的开发和部署方式,Dubbo提供了分布式服务框架的支持,而Zookeeper则提供了分布式协调服务。开发人员可以使用Spring Boot快速构建独立的微服务应用程序,使用Dubbo进行服务间的通信和管理,同时通过Zookeeper进行服务的注册和发现。这样的架构可以方便地实现微服务架构中的资源共享和服务治理等功能,大大简化了开发人员的负担。 综上所述,Spring Boot结合DubboZookeeper可以构建高效、可靠的微服务架构,并提供了便利的开发和部署方式,帮助开发人员构建高性能的分布式应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值