项目案例:链接: https://pan.baidu.com/s/1j30tumkgrYWZBVlBulxEmQ&shfl=shareset 提取码: bqjy
一、添加Maven依赖:
SpringBoot 2.0以上版本(包含):
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
SpringBoot2.0以下版本:
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.1</version>
</dependency>
二、修改服务提供者application.properties配置文件:
#指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)
dubbo.application.name=user-service-provider
#指定注册中心的位置
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#指定通信规则(通信协议?通信端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
#连接监控中心
#dubbo.monitor.protocol=registry
三、暴露服务:
在接口实现类上添加com.alibaba.dubbo.config.annotation.Service包下的@Service注解和org.springframework.stereotype.Service下的@Service注解(也可以替换成org.springframework.stereotype.Component包下的@Component注解):
import com.huihuixu.service.UserService;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
@Service
@Component
public class UserServiceImpl implements UserService {
@Override
public String getUserName(String userId) {
return null;
}
}
在SpringBoot的启动类上添加@EnableDubbo注解。
四、修改服务消费者application.properties配置文件:
#修改项目启动端口
server.port=8081
#指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)
dubbo.application.name=boot-order-service-consumer
#指定注册中心的位置
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#连接监控中心
#dubbo.monitor.protocol=registry
五、服务消费者远程调用服务提供者:
将Controller中的@Autowired注解替换成@Reference注解并在在SpringBoot的启动类上添加@EnableDubbo注解。