搭建 SpringBoot+zk+dubbo
一, zk
- 下载zookeeper :zookeeper官网
- 解压完成后,将 conf 目录下的 zoo_sample.cfg 复制一份,重命名为:zoo.cfg
- 在zookeeper目录下,新建一个data文件夹,编辑zoo.cfg,将 dataDir 的值改为data文件夹的路径
- 进入bin目录,编辑zkServer.cmd,在倒数第二行增加 pause(运行出错后不会闪退)
SpringBoot+dubbo
导入依赖(注意Springboot和Dubbo版本):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- SpringBoot的版本号 -->
</parent>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
生产者
TestService
package com.producer.service;
public interface TestService {
public String sayHello(String name);
}
TestServiceImpl
package com.producer.service.impl;
import com.producer.service.TestService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Service(version = "1.0.0",interfaceClass = TestService.class)
@Component
public class TestServiceImpl implements TestService{
@Override
public String sayHello(String name){
return "hello,"+name;
}
}
ProducerApplication
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//生产者和消费者都要加这个注解,开启dubbo
@EnableDubbo
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
application.properties
//端口号,防止冲突
server.port=8001
//应用名称
dubbo.application.name=dubbo-producer
//注册地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
//协议名称
dubbo.protocol.name=dubbo
//暴露端口号,-1为随机
dubbo.protocol.port=-1
//扫描服务注册bean,即service实现类所在的包
dubbo.scan.base-packages=com.producer.service
消费者
生产者和消费者的 TestService 的目录一定要一样,连一个字母都不能错,负责 生产者 会无法找到
TestClass
package com.producer.service.impl;
import com.producer.service.TestService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class TestClass {
@Reference(version = "1.0.0", url = "dubbo://127.0.0.1:20880?version=1.0.0")
//url后面要跟上版本号,不然会报错
TestService testService;
public String sayHello(String name){
return testService.sayHello(name);
}
}
TestController
package com.producer.conrtroller;
import com.producer.service.impl.TestClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
TestClass testClass;
@RequestMapping("/sayHello/{name}")
public String sayHello(@PathVariable("name") String name){
return testClass.sayHello(name);
}
}
application.properties
server.port=8002
dubbo.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.base-packages=com.consumer.service
启动
- 启动zookeeper bin 目录下的 zkServer.cmd,zkCli.cmd
- 启动生产者 和 消费者
- 访问:http://www.localhost:8002/sayHello/kobe