10分钟搭建你自己的SpringBoot+Dubbo简单工程
本片仅对SpringBoot+Dubbo的环境搭建做简要概述,不涉及晦涩难懂的高深技术,适合新手入门阅读,能快速构建自己的开发环境。
开发环境
window 10
jdk-1.8.91
idea2018
zookeeper-3.4.8
第一步,本地下载安装zookeeper,我本地使用的是zookeeper,修改confg文件夹下的zoo.cfg文件,注意:新安装的可能配置文件是zoo-simple.cfg,顺便改下名字为zoo.cfg,更改个人dataDir和dataLogDir存放路径,本次搭建意在简单,没有做集群,下面是我本机的zoo.cfg文件
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir=/tmp/zookeeper
dataDir=E:\\zookeeper\\data
dataLogDir=E:\\zookeeper\\log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
然后进入zookeeper的安装路径下的bin目录,双击zkServer.cmd,启动正常的话会弹出zookeeper的日志打印,若出现闪退情况,原因可能有多种,编辑zkServer.cmd文件,再文件最后添加上pause,这样启动失败,不会出现闪退的问题,方便查找错误,我在首次安装的时候出现了找不到jdk的问题,此时需要修改zkEnv.cmd文件,添加jdk路径即可
set JAVA=C:\Program Files\Java\jdk1.8.0_91\bin\java
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_91
第二步,项目搭建
idea下自建一个工程,然后添加子项目springboot-api,springboot-server,springboot-client,子项目创建的时候,项目右键->New->Moudel选择Spring initializr,Next到Finish
其中springboot-api用来定义接口和实体,springboot-server是服务提供者,springboot-client是服务的消费者
打开父工程的pom文件,然后配置如下
<?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.zdd</groupId>
<artifactId>springboot-dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springboot-dubbo-demo</name>
<description>Demo project for Spring Boot</description>
<modules>
<module>springboot-api</module>
<module>springboot-server</module>
<module>springboot-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
然后打开springboot-api的pom,配置如下
<?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.zdd</groupId>
<artifactId>springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-api</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.zdd</groupId>
<artifactId>springboot-dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后打开springboot-server的pom,配置如下
<?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.zdd</groupId>
<artifactId>springboot-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.zdd</groupId>
<artifactId>springboot-dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.zdd</groupId>
<artifactId>springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
最后打开springboot-client的pom,配置如下
<?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.zdd</groupId>
<artifactId>springboot-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.zdd</groupId>
<artifactId>springboot-dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.zdd</groupId>
<artifactId>springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这样环境基本就搭建好了,下面开始写代码,
先在springboot-api中的建一个实体类User和接口UserService,添加属性请随意,删掉项目自动创建的启动类,
package com.zdd.springbootapi.domain;
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private Long id;
private String name;
private Integer age;
public User(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public User() {
super();
}
}
package com.zdd.springbootapi.service;
import com.zdd.springbootapi.domain.User;
public interface UserService {
User findUser();
}
然后在springboot-server工程下,创建UserService的实现类UserServiceImpl
package com.zdd.springbootserver.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.zdd.springbootapi.domain.User;
import com.zdd.springbootapi.service.UserService;
import org.springframework.stereotype.Component;
@Service(version = "1.0.0",interfaceClass = UserService.class)
@Component
public class UserServiceImpl implements UserService {
@Override
public User findUser() {
return new User(1L,"里斯",30);
}
}
启动类上添加@EnableDubboConfiguration
package com.zdd.springbootserver;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class SpringbootServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootServerApplication.class, args);
}
}
然后编写application.properties
spring.dubbo.application.id=springboot-server
spring.dubbo.application.name=springboot-server
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.server=true
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
这样服务提供端就完成了,然后开始编写服务消费端
在springboot-client创建一个Controller
package com.zdd.springbootclient.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zdd.springbootapi.domain.User;
import com.zdd.springbootapi.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Reference(version = "1.0.0",interfaceClass = UserService.class)
private UserService userService;
@GetMapping("/findUser")
public User findUser(){
return userService.findUser();
}
}
然后同server一样在启动类上添加@EnableDubboConfiguration配置
package com.zdd.springbootclient;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class SpringbootClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootClientApplication.class, args);
}
}
修改application.properties
server.port=8081
spring.dubbo.application.id=springboot-client
spring.dubbo.application.name=springboot-client
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
至此代码开发完毕。
然后按顺序启动springboot-server,springboot-client,浏览器中访问http://localhost:8081/findUser,能正常打印结果。
github上有个dubbo-admin监控,可以直接去https://github.com/apache/incubator-dubbo-ops/tree/master这个地址拉下master分支代码,然后启动admin工程,访问http://localhost:7001
用户名和密码是root,登陆后即可查看相关的参数设置及服务运行状态等,非常方便。
按照上面的步骤,一般10分钟内即可快速搭建一套个人的dubbo工程。
我的工程源码可以去这个地址下载:https://download.****.net/download/qq_35918388/10829502
此次分享至此结束,欢迎交流。