使用springboot+dubbo+zookeeper构建搭建生产者平台
引入 maven pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
dubbo 使用的是最新的2.5.7 spring boot 使用的最新的release版本
启动类:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
duboo的配置类
@Component
@DubboComponentScan(basePackages="com.wy.demo.service.impl") //配置dubbo 注解扫描
public class DubboConfig {
//app
@Bean("dubbo-app")
ApplicationConfig app() {
return new ApplicationConfig("dubbo");
}
//注册配置
@Bean
RegistryConfig registry() {
return new RegistryConfig("zookeeper://localhost:2181");
}
//暴露协议
@Bean
ProtocolConfig protocol(){
return new ProtocolConfig("dubbo", 20000);
}
}
添加一个接口 此类通常会放在一个接口项目中
public interface HelloService {
String say(String name);
}
使用注解的方式进行暴露
import com.alibaba.dubbo.config.annotation.Service;
import com.wy.demo.service.HelloService;
@Service(protocol={"dubbo"})
public class HelloServiceImpl implements HelloService{
@Override
public String say(String name) {
return name + ": hello";
}
}
项目结构
运行 此时 一个最简单的dubbo provider 就搭建完成了