References:
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 configuration
Configuring 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 code:
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); } }
Here spring-boot web application build better;
2, integration dubbo
2.1, ready zk (zookeeper) environment
Official website to download ZK: https://zookeeper.apache.org/releases.html
After downloading unzip to a local directory;
Edit and configure zoo.cfg file (zk directory /config/zoo.cfg)
tickTime=2000 initLimit=10 syncLimit=5 dataDir=E:\zk\zookeeper-3.4.9\datas clientPort=2181
Execute bin / zkServer.cmd start zk Service
ps: Here is ready zk environment, and if you want to visually see zk node directory information and can use the tool https://github.com/DeemOpen/zkui view, java realize, after mvn package that is executed by java -jar accessible;
2.2, dubbo configuration
As used herein dubbo project launcher under apache project (alibaba starter is no longer maintained, not recommended)
At this point the project maven dependent:
<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>
New services and service implementation
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"; } }
Increased use global parameters 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
Thus configured, start the test, can be seen in zkui registration service that is done