在上一篇中已经就普通工程的duboo做了详细介绍,接下来介绍下如何和springboot集成。
环境搭建 就不再赘述了,和前面一篇一样,请参考:
1、新建项目
这里直接新建 springboot项目,删掉src目录作为一个父级项目存在。
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.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyzh</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、添加api模块
跟前面一篇一样,这里添加一个项目,用来存放服务api ,实体类等公共的契约
这里定义一个接口
public interface IMyService {
String helloService(String name);
}
3、添加provider模块
添加springboot 类型的模块,修改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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xyzh</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyzh</groupId>
<artifactId>springboot-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>com.xyzh</groupId>
<artifactId>springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改配置文件 application.properties,如下:
#WEB服务端口
server.port=8001
## dubbo 配置
spring.dubbo.application.id=springboot-provider
spring.dubbo.application.name=springboot-provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.server=true
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
添加服务的实现类
import com.alibaba.dubbo.config.annotation.Service;
import com.xyzh.springbootapi.service.IMyService;
import org.springframework.stereotype.Component;
@Service(interfaceClass = IMyService.class,retries = 0)
@Component
public class MyServiceImpl implements IMyService {
@Override
public String helloService(String name) {
return "hello,"+name;
}
}
注意这里加了一个retries = 0,默认情况下dubbo会超时自动重掉三次,这样很容易出问题,所以这里用retries = 0。
修改启动类,添加注解 @EnableDubboConfiguration
@EnableDubboConfiguration
@SpringBootApplication
public class SpringbootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootProviderApplication.class, args);
}
}
4、添加调用模块
添加springboot类型的模块,修改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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xyzh</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyzh</groupId>
<artifactId>springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
修改配置文件application.properties
server.port=8002
## Dubbo 配置
spring.dubbo.application.name=springboot-consumer
spring.dubbo.application.id=springboot-consumer
spring.dubbo.protocol.port=20885
spring.dubbo.protocol.name=dubbo
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
修改启动类:
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class SpringbootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootConsumerApplication.class, args);
}
}
增加controller
import com.alibaba.dubbo.config.annotation.Reference;
import com.xyzh.springbootapi.service.IMyService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Reference
IMyService myService;
@GetMapping(value = "/hello")
public String hello(){
return myService.helloService("test");
}
}
注意这里的 Refrence注解,通过引用可以调用。
启动后 访问http://localhost:8002/hello
截图如下:
查看下dubbo控制台
这是 服务提供方:
下图是调用者: