Springboot整合dubbo+zookeeper
1.创建一个springboot项目dubbo_springboot(删除一些没必要的文件)
2.创建module
一、dubbo_comsumer(springboot)
二、dubbo_interface(maven)
三、dubbo_provider(springboot)
3.编写公共接口
package com.example;
public interface UserService {
public String getUser(Long userId);
}
4.编写生产者dubbo_provider
###### 一、填加依赖
1.继承公共接口
<dependency>
<artifactId>dubbo_interface</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.添加dubbo和zookeeper客户端依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- zookeeper客户端 2.6以前的版本使用zkclient,dubbo 2.6+使用curator-->
<!--
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
二、编写公共接口
package com.example.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.UserService;
@Service
public class UserServiceImpl implements UserService {
@Override
public String getUser(Long userId) {
System.out.println("###会员服务接受参数开始userId:" + userId);
if (userId == 1) {
return "小明";
}
if (userId == 2) {
return "小暗";
}
System.out.println("###会员服务接受参数结束###");
return "未找到用户...";
}
}
注意:@Service(com.alibaba.dubbo.config.annotation.Service)
三、编写yml文件
server:
port: 7003
dubbo:
application:
name: dubbo-provider #服务名称
registry:
protocol: zookeeper #注册中心的协议
address: 127.0.0.1:2181 #注册中心的地址
protocol:
name: dubbo #通信协议
port: 20880 #通信端口
monitor:
protocol: registry
四、启动类填加@EnableDubbo注解
package com.example;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class DubboProvideApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProvideApplication.class, args);
}
}
五.编写消费者dubbo_consumer
###### 一、填加依赖
1.继承公共接口
<dependency>
<artifactId>dubbo_interface</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.添加dubbo和zookeeper客户端依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- zookeeper客户端 2.6以前的版本使用zkclient,dubbo 2.6+使用curator-->
<!--
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
二、编写CallUserService实现类
package com.example.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.UserService;
import org.springframework.stereotype.Service;
@Service
public class CallUserService {
@Reference
private UserService userService;
public String getUser(Long userId){
return userService.getUser(userId);
};
}
注意:@Service(org.springframework.stereotype.Service)
@Reference(import com.alibaba.dubbo.config.annotation.Reference)
三、编写controller
import com.example.service.CallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CallController {
@Autowired
private CallUserService callUserService;
@GetMapping("/get")
public String get(){
return callUserService.getUser(1l);
}
}
四、编写yml文件
server:
port: 7004
dubbo:
application:
name: dubbo-provider #服务名称
registry:
protocol: zookeeper #注册中心的协议
address: 127.0.0.1:2181 #注册中心的地址
monitor:
protocol: registry