最近在学习SpringtBoot+Dubbo,很多坑。GitHub的Demo层级关系比较复杂。很多博客的文章多多少少存在一些坑。
接下来。来点简单的Demo:
第一步:安装Zookeeper:
我用的版本是 zookeeper-3.4.9 ,下载地址:Zookeeper各版本下载
下载好后,解压到Windows目录下或者虚拟机目录下。进入conf。将zoo-sample.cfg改成zoo.cfg。
里面配置如下
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
保存后。进入bin目录。Lunix相关命令:
./zkServer.sh start
./zkServer.sh stop
./zkServer.sh restart
./zkServer.sh status
Window系统直接 双击 zkServer.cmd 。停止 关闭控制台即可。
第二步:安装Dubbo-admin: 代码路径: Dubbo-admin 。
下载后解压:
进入dubbo-admin-server\src\main\resources。修改application.properties。将zookeeper的地址改成自己的。
比如我的就是本机IP。
进入目录dubbo-admin-server目录 输入mvn clean package打包。
生成了jar文件后
java -jar XXXX.jar 运行起来。
第三步:写一个服务层接口。为的是不直接暴露业务层。需要服务和消费者都引用。
这是目录。命名不规范。仅供参考。
POM:
将文件打包到本地仓库。
第四步:写服务提供者。未涉及数据库
以上是目录结构
UserServiceImpl代码:
package com.yanjing.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.yanjing.api.UserService;
@Service(version = "${user.service.version}")
public class UserServiceImpl implements UserService {
@Override
public String sayHi() {
return "第一个Dubbo";
}
}
DubboDemoServiceApplication代码:
package com.yanjing.service;
import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboDemoServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DubboDemoServiceApplication.class, args);
Main.main(args);
}
}
application.yml代码
user:
service:
version: 1.0.0
spring:
application:
name: com.yanjing.service
dubbo:
scan:
basePackages: com.yanjing.service.impl #2.6版本以后都是这个没有横线
application:
name: com.yanjing.service
id: com.yanjing.service
qos-port: 22222 #这是telnet服务的接口。
qos-enable: true
protocol:
status: server #这是表示是服务提供者。
id: dubbo #这是dubbo协议。
name: dubbo #这是dubbo协议。
port: 12345 #这是dubbo协议端口。
registry:
address: zookeeper://192.168.50.29:2181
id: zookeeper
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 http://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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yanjing</groupId>
<artifactId>dubbo-demo-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>dubbo-demo-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<dubbo.version>2.7.0</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>com.yanjing</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第五步。消费者,这是一个web项目。
目录:
UserController代码:
package com.yanjing.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.yanjing.api.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Reference(version = "1.0.0")
UserService userService;
@RequestMapping(value = "hi", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
public String adminLogin() {
return userService.sayHi();
}
}
DubboDemoConsumerApplication代码:
package com.yanjing.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboDemoConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboDemoConsumerApplication.class, args);
}
}
application.yml代码:
dubbo:
application:
name: com.yanjing.consumer
id: com.yanjing.consumer
## ProtocolConfig
protocol:
id: dubbo
name: dubbo
port: 12345
scan:
basePackages: com.yanjing.consumer.controller
registry:
address: zookeeper://192.168.50.29:2181
server:
port: 9091
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 http://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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yanjing</groupId>
<artifactId>dubbo-demo-consumer</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>dubbo-demo-consumer</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.yanjing</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>