SpringBoot作为Spring家族近几年的新生力量,由于其“约定大于配置”的特性,使得SpringBoot应用的也越来越广泛。Dubbo作为高性能RPC框架,在微服务的领域也有一定的应用。本文章将SpringBoot和Dubbot框架进行结合,完成一个简单的应用。
源代码
1.共用API(模型、服务接口)
源代码结构如图:
=
public class Message implements Serializable {
public String name;
public String msg;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Message() { }
public Message(String name, String msg) {
this.name = name;
this.msg = msg;
}
@Override
public String toString() {
return "Message{" +
"name='" + name + '\'' +
", msg='" + msg + '\'' +
'}';
}
}
在dubbo服务中,共用的模型需要实现Serializable接口
public interface MessageService {
public String getMessage(Message message);
}
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.blue</groupId>
<artifactId>dubbo-hello-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
共用API部分同传统的Spring和Dubbo框架结合的共用API部分保持一致Spring和Dubbo框架简单应用
2.服务提供者
源代码结构如图:
@Service
@Component
public class MessageServiceImpl implements MessageService {
@Override
public String getMessage(Message message) {
return "To " + message.getName() + "\n\t" + message.getMsg();
}
}
此处@Service标签不是SpringFramwork中的标签,而是Dubbo的标签
import com.alibaba.dubbo.config.annotation.Service;
@EnableDubbo
@SpringBootApplication
public class BootHelloProviderApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(BootHelloProviderApplication.class, args);
System.in.read();
}
}
@EnableDubbo表示开启Dubbo服务,加入此标签之后Dubbo的@Service标签才会生效,将MessageServiceImpl暴露为服务的提供者
application.yml
dubbo:
application:
name: boot-hello-provider
registry:
address: zookeeper://192.168.56.102:2181
protocol:
name: dubbo
port: 20880
SpringBoot中有两种配置文件:application.properties和application.yml。
yml文件相较于properties文件层次更加的清晰,看上去更加简洁。
application.yml中配置的内容与Spring和Dubbo框架结合中xml配置的内容一致Spring和Dubbo框架简单应用
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.0.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.blue</groupId>
<artifactId>boot-hello-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-hello-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>
<dependency>
<groupId>com.blue</groupId>
<artifactId>dubbo-hello-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom.xml文件中由之前引入dubbo和curator-framework改为引入dubbo-spring-boot-starter,并且需要注意starter的版本适配问题
starter | Java | Spring Boot | Dubbo |
---|---|---|---|
0.2.0 | 1.8+ | 2.0.x | 2.6.2+ |
0.1.1 | 1.7+ | 1.5.x | 2.6.2+ |
3.服务消费者
源代码结构如图:
@Component
public class ShowMessage {
@Reference
MessageService messageService;
public void showMessage(Message message){
String msg = messageService.getMessage(message);
System.out.println(msg);
}
}
@EnableDubbo
@SpringBootApplication
public class BootHelloConsumerApplication {
public static void main(String[] args) throws IOException {
ApplicationContext context = SpringApplication.run(BootHelloConsumerApplication.class, args);
ShowMessage showMessage = context.getBean(ShowMessage.class);
Message message = new Message("Dubbo", "Hello World!");
showMessage.showMessage(message);
System.in.read();
}
}
为了便于测试,在启动类直接传入Message对象。为了便于查看Dubbo Amin中的消费者数量,使用read方法阻塞主进程
测试
启动Zookeeper和Duboo Admin服务,然后启动服务提供者和消费者,可以在服务消费者一端看到返回的消息。
在浏览器输入http://localhost:7001可以看到服务者和消费者数量均为1。