下载zookeeper
在官网下载zookeeper,
复制并重命名conf下的zoo_sample.cfg为zoo.cfg
并打开bin目录下的zkServer.cmd文件
创建项目
创建一个maven项目
springboot--dubbo-interface
创建两个springboot的springweb项目作为生产者与消费者
springboot-dubbo-provider
springboot-dubbo-consumer
添加依赖
在生产者与消费者下的pom.xml文件内添加依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--你的interface工程-->
<dependency>
<groupId>com.who.springboot</groupId>
<artifactId>springboot-dubbo-interface</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
修改application
生产者项目
server.port=8081
dubbo.server=true
dubbo.application.name=springboot-dubbo-provider
dubbo.registry.address=zookeeper://localhost:2181
消费者项目
server.port=9090
dubbo.application.name=springboot-dubbo-consumer
dubbo.registry.address=zookeeper://localhost:2181
接口类实现
在接口项目下添加接口services/IndexService
package services;
public interface IndexService {
String queryVersion();
}
生产者实现
先在Application类下 加上@DubboComponentScan
注解
package com.who.springboot;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
新建接口实现类Impl.IndexServiceImpl
package com.who.springboot.Impl;
import org.apache.dubbo.config.annotation.Service;//注意!是dubbo的Service,不是Spring的!
import org.springframework.stereotype.Component;
import services.IndexService;
@Component //将类放入Spring容器内
@Service(interfaceClass = IndexService.class,version = "1.0.0",timeout = 15000)
public class IndexServiceImpl implements IndexService {
@Override
public String queryVersion() {
return "1.0.0";
}
}
消费者实现
同样的在Application类 加上@DubboComponentScan
注解
package com.who.springboot;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
新建类controller/IndexController
package com.who.springboot.controller;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import services.IndexService;
@RestController
@RequestMapping("/")
public class IndexController {
@Reference(interfaceClass = IndexService.class,version = "1.0.0",check = false)
private IndexService indexService;
@GetMapping("version")
public Object version(){
String version = indexService.queryVersion();
return "当前版本为" + version;
}
}
常见报错
https://blog.csdn.net/qq_33985931/article/details/123062690?spm=1001.2014.3001.5502