springboot集成dubbo
本文介绍搭建两个集成dubbo的springboot项目并相互调用的过程
springboot集成dubbo
一、创建生产者项目
1、创建项目
然后直接next、Finish
2、引入依赖(写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 https://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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aloudata</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>dubbo-provider</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<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>
<version>2.5.5</version>
</plugin>
</plugins>
</build>
</project>
3、写配置文件
application.properties
dubbo.application.name=dubbo_provider
dubbo.registry.protocol=zookeeper
#zookeeper地址
dubbo.registry.address=zookeeper://192.168.108.130:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#将扫描到的包注册到zookeeper
dubbo.scan.base-packages=com.aloudata.service
4、写service
写一个service接口和实现类,供消费者项目访问
package com.aloudata.service.impl;
import com.aloudata.service.UserService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class UserServiceImpl implements UserService {
@Override
public String sayHello() {
System.out.println("hello,我是dubbo服务者");
return "hello,我是dubbo服务者";
}
}
注意:这里的@service
注解是dubbo的,不是spring的
5、执行maven的install命令
这里是将服务者项目打成jar包,并放到本地maven库中,让消费者项目能够引入此项目依赖。消费者项目不引入此项目依赖的话就无法得知可以调用服务者项目的哪些类的哪些方法。
6、启动服务
执行启动类即可
二、创建消费者项目
1、创建项目
这里比创建生产者项目步要多引用一个Spring Web,因为要通过前端访问controller
2、引入依赖(须额外引入生产者项目的jar)
消费者项目需要引入生产者项目的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 https://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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aloudata</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-consumer</name>
<description>dubbo-consumer</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
//这里是引入生产者项目的依赖
<dependency>
<groupId>com.aloudata</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<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>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<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>
<version>2.5.5</version>
</plugin>
</plugins>
</build>
</project>
注意:这里引入之后只是为了知道有哪些接口可以调用,具体要调用哪个项目的方法还是通过zookeeper(服务发现)来确定的。
3、写配置文件
application.properties
dubbo.application.name=dubbo-consumer
dubbo.registry.protocol=zookeeper
#zookeeper地址
dubbo.registry.address=zookeeper://192.168.108.130:2181
#修改一下tomcat的端口号,防止冲突
server.port=8085
4、写controller
写controller,供前端访问,方法里面调用生产者项目的service方法
package com.aloudata.controller;
import com.aloudata.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Reference
private UserService userService;
@RequestMapping("/dubbo")
public String sayHello(){
System.out.println("hello,我是dubbo消费者");
return userService.sayHello();
}
}
注意 这里的@Reference
是dubbo的注解
5、启动服务
执行启动类即可
三、在浏览器访问消费者项目接口
可以看到接口访问成功,消费者项目成功调用生产者项目的方法。