完整代碼見:github.com/Caizhenhao/…html
我使用的是阿里雲服務器java
tar -zxvf zookeeper-3.4.11.tar.gz複製代碼
mv zookeeper-3.4.11 zookeeper複製代碼
rm -rf zookeeper-3.4.11.tar.gz複製代碼
mkdir data複製代碼
cp zoo_sample.cfg zoo.cfg複製代碼
vim zoo.cfg
命令修改配置文件
修改配置文件中的 dataDir 屬性:git
dataDir=/usr/local/zookeeper/data複製代碼
進入 /zookeeper/bin 目錄而後執行下面的命令啓動zookeepergithub
./zkServer.sh start複製代碼
./zkServer.sh status複製代碼
或者運行 netstat -lntup 命令查看網絡狀態,能夠看到 zookeeper 的端口號 2181 已經被佔用
web
項目分紅三個模塊spring
關鍵依賴
apache
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>複製代碼
抽象服務提供者的接口供服務消費者調用vim
package com.czh.provider.dubboprovider.serviceImpl.service;
public interface HelloService {
String sayHello(String name);
}複製代碼
主要分爲下面幾步:api
<dependencies>
<dependency>
<groupId>com.czh.api</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>複製代碼
# 配置端口
spring.application.name = dubbo-provider
server.port = 9090
#指定當前服務/應用的名字(一樣的服務名字相同,不要和別的服務同名)
dubbo.application.name = dubbo-provider
demo.service.version = 1.0.0
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880
#指定註冊中心的位置
dubbo.registry.address = zookeeper://ip:2181
#統一設置服務提供方的規則
dubbo.provider.timeout = 1000複製代碼
服務提供者,包含真正的服務實現代碼安全
package com.czh.provider.dubboprovider.serviceImpl;
import com.alibaba.dubbo.config.annotation.Service;
import com.czh.provider.dubboprovider.serviceImpl.service.HelloService;
@Service(version = "${demo.service.version}")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello " + name + ",This is springboot-dubbo test";
}
}複製代碼
package com.czh.provider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
複製代碼
spring.application.name = dubbo-consumer
server.port = 9091
#指定當前服務/應用的名字(一樣的服務名字相同,不要和別的服務同名)
dubbo.application.name = dubbo-consumer
demo.service.version = 1.0.0
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880
#指定註冊中心的位置
dubbo.registry.address = zookeeper://ip:2181
#統一設置服務提供方的規則
dubbo.consumer.timeout = 5000複製代碼
package com.czh.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.czh.api.service.HelloService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloConsumerController {
@Reference(version = "${demo.service.version}")
private HelloService helloService;
@RequestMapping("/sayHello/{name}")
public String sayHello(@PathVariable("name") String name) {
return helloService.sayHello(name);
}
}複製代碼
package com.czh.consumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
複製代碼
使用springboot+dubbo+zookeeper進行搭建 dubbo 的項目應該說是比較簡單的,官方也有集成Springboot的依賴,這裏就很少說了,實踐出真知~
完整代碼見:github.com/Caizhenhao/…