Springboot 最简单的整合Dubbo框架实战案例
分布式框架,目前比较热门的是springcloud和dubbo,虽然本人也是用cloud比较多,但是避免不了,有的项目就是需要用dubbo。
那么现在就给大家整一篇,简单清晰的spirngboot从零整合dubbo。
先看看这个简单的整合完毕后,项目结构是这样的:
里面包含的有,公共model模块、公共interface模块; 消费者consumer1服务、消费者consumer2服务、提供者provider服务;
里面的关联关系大致是,
a.提供者provider服务 是需要导入 公共model模块、公共interface模块 的,因为写的接口实现类会用到model和service;
b.消费者consumer1服务、消费者consumer2服务 是需要导入 公共interface模块 的(看实际需要导入model模块或是重建相关的业务model)
对了,消费者consumer、提供者provider都是需要注册到同一个zookpeer上去的,这样提供者才能通过注册中心将需要向外提供的接口暴露;消费者才能通过注册中心扫描使用相关的接口。
简单介绍先到这(深入理解可以额外去网上研究,这里只做整合实战介绍)。
首先我们创建一个Maven项目,然后建一个Module 起名为dubbomodel,
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">
<parent>
<artifactId>testdubbo</artifactId>
<groupId>springboot-dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-model</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
</project>
ps:上面导入了lombok jar包其实仅仅为了省事,通过注解去弄set、get、构造等等方法,也可以不导入
然后简单建一个User.java:
package top.test.model.user.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author:JCccc
* @Description:
* @Date: created in 10:09 2019/5/5
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
OK,公共model模块就这样先。
接着,再建一个Module,起名为dubbointerface:
pom文件不需要导入额外jar包:
<?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">
<parent>
<artifactId>testdubbo</artifactId>
<groupId>springboot-dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-interface</artifactId>
</project>
,建一个简单的Service,准备暴露到注册中心zookeeper去,HelloService.java:
/**
* @Author:JCccc
* @Description:
* @Date: created in 11:00 2019/4/25
*/
public interface HelloService {
public String sayHello(String name);
}
OK,公共interface模块就这样先。
接着,我们开始写一个 提供者provider服务,建一个Module(Springboot项目),起名为dubbo-provider:
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>test-dubbo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>Demo 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>
<!--公共interface模块-->
<dependency>
<groupId>springboot-dubbo</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--公共model模块-->
<dependency>
<groupId>springboot-dubbo</groupId>
<artifactId>dubbo-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入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.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
可以看到,我们在provider服务里引入了公共的model、公共的interface、以及重要的dubbo依赖和zookeeper依赖。
接着是application.properties:
# 配置端口
server.port=8333
# 注册到本地zookeeper
spring.dubbo.application.name=dubbo-provider
spring.dubbo.application.registry=zookeeper://127.0.0.1:2181
然后写一个接口实现类HelloServiceImpl.java ,用于实现需要用到的接口(来自于导入的公共接口模块):
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import top.test.service.HelloService;
/**
* @Author:JCccc
* @Description:
* @Date: created in 11:08 2019/4/25
*/
@Component
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
特别提醒,这里的@Service使用的是dubbo依赖包提供的com.alibaba.dubbo.config.annotation.Service
在项目启动类需要开启dubbo自动配置注解,@EnableDubboConfiguration:
package testdubbo.dubboprovider;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// 开启dubbo的自动配置
@EnableDubboConfiguration
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
OK,provider就这样先。
接着我们开始写一个消费者consumer ,建一个Module,起名为dubbo-consumer:
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>testdubbo</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-consumer</name>
<description>Demo 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>
<dependency>
<groupId>springboot-dubbo</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入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.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后是application.properties:
# 配置端口
server.port=8433
spring.dubbo.application.name=dubbo-consumer
spring.dubbo.application.registry=zookeeper://127.0.0.1:2181
接着写一个Controller来消费一下刚刚provider暴露到注册中心的接口,HelloController.java:
package testdubbo.dubboconsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.test.service.HelloService;
/**
* @Author:JCccc
* @Description:
* @Date: created in 11:12 2019/4/25
*/
@RestController
public class HelloController {
@Reference
private HelloService helloService;
@RequestMapping("/hello")
public String hello() {
String hello = helloService.sayHello("world");
System.out.println(helloService.sayHello("JCccc"));
return hello;
}
}
特别提醒,@Reference 这个注解是使用dubbo去zookeeper注册中心去扫描已经暴露的接口
在项目启动类需要开启dubbo自动配置注解,@EnableDubboConfiguration:
package testdubbo.dubboconsumer;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
OK,消费者consumer2可以自己按照这个模式扩展,这里不用重复介绍了,我们来开始测试,
先将provider服务跑起来,再将consumer服务跑起来,
用postman调下消费者consumer的接口,
可以看到成功调用到消费者服务的接口,证明简单的一个流程走通。
OK,简单的springboot+dubbo框架整合使用到此。