dubbo的介绍可以查看官网说明,这里不做过多的介绍。
官网:http://dubbo.apache.org/zh-cn/index.html
github:https://github.com/apache/dubbo
dubbo spring boot:https://github.com/apache/dubbo-spring-boot-project
在日常工作中我们一般会使用SpringBoot作为基础开发框架,它的自动装配功能可以极大的提升我们的开发效率,故在本文中也采用SpringBoot集成dubbo进行演示。
<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.0modelVersion>
<parent>
<groupId>com.dubbogroupId>
<artifactId>providerartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.dubbogroupId>
<artifactId>provider-serverartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>provider-servername>
<description>Demo project for Spring Bootdescription>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>${spring-boot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>com.dubbogroupId>
<artifactId>provider-clientartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
UserDTO代码,对象需要实现Serializable接口:
package com.dubbo.provider.client.model;
import java.io.Serializable;
public class UserDTO implements Serializable {
private Integer id;
private String name;
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;
}
}
UserService代码:
package com.dubbo.provider.client;
import com.dubbo.provider.client.model.UserDTO;
public interface UserService {
public UserDTO findUserById(Integer id);
}
package com.dubbo.provider.server.adapter;
import com.dubbo.provider.client.UserService;
import com.dubbo.provider.client.model.UserDTO;
public class UserServiceAdapter implements UserService {
@Override
public UserDTO findUserById(Integer id) {
UserDTO userDTO = new UserDTO();
userDTO.setId(id);
userDTO.setName("Leo");
return userDTO;
}
}
在provider-server模块中的resources中新建provider.xml文件。
由于这是第一个dubbo演示项目,为了简单起见,这里采用直连模式,所以不配置注册中心。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="provider" />
<dubbo:registry address="N/A" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.dubbo.provider.client.UserService" ref="userService" />
<bean id="userService" class="com.dubbo.provider.server.adapter.UserServiceAdapter" />
beans>
在启动类上加载配置文件:
package com.dubbo.provider.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(locations = "classpath:provider.xml")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
消费者项目的创建可以参考生产者项目创建的过程,步骤基本类似,由于只是演示,消费者项目不需要是多模块的。但是需要依赖provider-client模块,所以需要先在provider-client模块目录中运行 mvn deploy命令将该模块打包后提交到本地仓库中备用。
项目结构:
pom.xml文件内容:
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.1.RELEASEversion>
<relativePath/>
parent>
<groupId>com.dubbogroupId>
<artifactId>consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>consumername>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<spring-boot.version>2.1.1.RELEASEspring-boot.version>
<dubbo.version>2.7.1dubbo.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>${spring-boot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>${spring-boot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.dubbogroupId>
<artifactId>provider-clientartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
consumer.xml文件内容:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="consumer" />
<dubbo:registry address="N/A" />
<dubbo:reference id="userService" interface="com.dubbo.provider.client.UserService"/>
beans>
HelloController代码:
package com.dubbo.consumer.controller;
import com.dubbo.provider.client.UserService;
import com.dubbo.provider.client.model.UserDTO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Reference(url = "dubbo://127.0.0.1:20880")
private UserService userService;
@GetMapping(value = "/hello")
public String hello() {
UserDTO userDTO = userService.findUserById(1);
return "hello world!!! ID:" + userDTO.getId() + " Name:" + userDTO.getName();
}
}
至此代码部分就开发完毕,启动项目查看效果。由于dubbo默认会开启启动时检查,所以我们需要先启动provider项目,再启动consumer项目,否则会报错。