简单实现springboot中使用dubbo
行动前准备:
1.搭建zookeeper集群:https://blog.csdn.net/ws_kfxd/article/details/94554018
2.安装dubbo控制台和监控中心:https://blog.csdn.net/ws_kfxd/article/details/87902693
搭建项目:
总共三部分:中间暴露接口项目(API)、服务提供者项目(provider)、消费者项目(customer)
三步走:
一、先搭建API项目:
(1)创建maven项目;
(2)pom文件引入以下依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- zookeeper依赖 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- zkclient依赖 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
(3)新建一个普通接口:
package com.ws.test;
public interface TestAPI {
String sayHello(String name);
}
二、接着搭建provider项目:
(1)创建简单的springboot项目;
(2)pom文件引入第一步中的API项目的依赖:
解释:provider项目也得引入dubbo、zookeeper等依赖,但我自己的api项目中已经有这些依赖,provider又有api的依赖,所以我不用再写了;各位根据自己实际情况定!
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<!-- web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- api项目依赖 -->
<dependency>
<groupId>com.ws.test</groupId>
<artifactId>test-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
(3)创建application.properties文件,配置dubbo信息
#WEB端口
server.port=8081
# dubbo
#作为消费者时的应用名
spring.dubbo.application.id=test01-id-provider
#作为提供者时的应用名
spring.dubbo.application.name=test01-name-provider
#zookeeper集群地址
spring.dubbo.registry.address=zookeeper://192.168.176.130:2182?backup=192.168.176.130:2181,192.168.176.130:2183
spring.dubbo.server=true
spring.dubbo.protocol.name=dubbo
#提供服务的端口
spring.dubbo.protocol.port=20880
#连接监控中心 法2:spring.dubbo.monitor.address=监控中心ip:监控中心通信端口
spring.dubbo.monitor.protocol=registry
(4)项目启动类加入@EnableDubboConfiguration注解,用来加载dubbo配置
package com.ws.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
@EnableDubboConfiguration
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
(5)在provider中新建接口实现类:
package com.ws.test.dubbo;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.ws.test.TestAPI;
@Component
@Service //dubbo包里的
public class TestAPIImpl implements TestAPI{
@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
System.out.println("--------------------------------Hello," + name + "------------------------------");
return "Hello," + name;
}
}
三、最后搭建customer项目:
(1)创建简单的springboot项目;
(2)pom文件引入第一步中的API项目的依赖;
(3)创建application.properties文件,配置dubbo信息:
server.port=8082
spring.dubbo.application.name=test01-id-consumer
spring.dubbo.application.id=test01-name-consumer
spring.dubbo.protocol.port=20800
spring.dubbo.protocol.name=dubbo
spring.dubbo.registry.address=zookeeper://192.168.176.130:2182?backup=192.168.176.130:2181,192.168.176.130:2183
spring.dubbo.monitor.protocol=registry
(4)直接创建类调用api项目接口:
@Reference
private TestAPI testAPI;
String dubbore = testAPI.sayHello(username);
log.info("dubbo--------------------{}",dubbore);
测试:
先启动zookeeper三个节点,在启动provider项目,最后启动customer项目:
provider:
customer:
控制台:
监控中心:(发了5次请求)