参考资料:
https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/
https://github.com/apache/dubbo-spring-boot-project
https://github.com/spring-cloud/spring-cloud-zookeeper/issues/36
1、spring boot配置
maven配置
<?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> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
java代码:
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}
到这里spring-boot的web程序就搭建好了;
2、整合dubbo
2.1、准备zk(zookeeper)环境
官网下载zk:https://zookeeper.apache.org/releases.html
下载后解压到本地目录;
编辑并配置zoo.cfg文件(zk目录下/config/zoo.cfg)
tickTime=2000
initLimit=10
syncLimit=5
dataDir=E:\zk\zookeeper-3.4.9\datas
clientPort=2181
执行 bin/zkServer.cmd 启动zk服务
ps:这里就准备好zk环境了,如果想直观的看到zk的节点及目录信息可以使用工具 https://github.com/DeemOpen/zkui 查看,java实现,mvn package 后通过java -jar执行即可访问;
2.2、dubbo配置
这里使用apache项目下的dubbo项目启动器(alibaba的启动器已经不维护了,不推荐使用)
项目此时maven依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 解决 java.lang.NoClassDefFoundError: org/apache/curator/framework/recipes/cache/TreeCacheListener 问题 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
新增服务及服务实现
package cn.jsu.wyk.service.impl;
import cn.jsu.wyk.service.PayService;
import com.alibaba.dubbo.config.annotation.Service;
@Service(version = "1.0.0")
public class PayServiceImpl implements PayService {
public String pay(){
return "pay success";
}
}
增加应用全局参数 application.properties
server.port=8081
## Dubbo 服务提供者配置
spring.application.name=dubbo-spring-boot-starter
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.scan.base-packages=cn.jsu.wyk.service
至此配置完毕,启动测试,在zkui中能看到注册服务即大功告成