前期条件:
启动Zookeeper、Dubbo-Monitor-Simple、Dubbo-Admin:https://blog.csdn.net/qq_37598011/article/details/86685467
基础组件信息:https://blog.csdn.net/qq_37598011/article/details/86698788
使用注解配置整合
服务提供者
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.dubbo</groupId>
<artifactId>boot-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-service-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.dubbo</groupId>
<artifactId>basics-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
这块依赖是核心,需要注意的是如果springboot的版本是2.0以后的则<version>0.2.0</version>,否则则是0.1.0
参考:https://github.com/apache/incubator-dubbo-spring-boot-project
<dependency>
<groupId>com.dubbo</groupId>
<artifactId>basics-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
这块请参照上篇博客……
启动类
在启动类上添加@EnableDubbo注解
application.properties如下:
#The service name
dubbo.application.name=boot-service-provider
#registration center
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#communication rule
dubbo.protocol.name=dubbo
dubbo.protocol.port=22288
#The monitoring center
dubbo.monitor.protocol=registry
#dubbo.scan.base-packages=com.dubbo.basics_interface
这里少了在原先xml配置中的第四步:
此时与dubbo跟springboot进行整合后,其用户实现接口改为了:
package com.service.provider.service.impl;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.basics_interface.dto.User;
import com.dubbo.basics_interface.service.UserService;
/*
* @author zzf
* @date 2019年1月29日 下午3:40:16
*/
@Component
@Service
public class UserServiceImpl implements UserService{
public List<User> getUserAddressList(String userId) {
// TODO Auto-generated method stub
User address1 = new User(1, "XXXXXXX1", "1", "小A", "88888888888", "Y");
User address2 = new User(2, "XXXXXXX2", "2", "小B", "77777777777", "N");
User address3 = new User(3, "XXXXXXX3", "3", "小C", "99999999999", "Y");
return Arrays.asList(address1,address2,address3);
}
}
这里的@Service是Dubbo中的注解(com.alibaba.dubbo.config.annotation.Service)
启动
服务消费者
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.dubbo</groupId>
<artifactId>boot-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-service-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.dubbo</groupId>
<artifactId>basics-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
在启动类上添加@EnableDubbo注解
application.properties文件
server.port=8081
#The service name
dubbo.application.name=boot-service-consumer
#registration center
dubbo.registry.address=zookeeper://127.0.0.1:2181
#The monitoring center
dubbo.monitor.protocol=registry
订单服务实现类
package com.service.consumer.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.basics_interface.dto.User;
import com.dubbo.basics_interface.service.OrderService;
import com.dubbo.basics_interface.service.UserService;
/*
* @author zzf
* @date 2019年1月29日 下午3:11:42
*/
@Service
public class OrderServiceImpl implements OrderService{
// @Autowired
@Reference
UserService userService;
public List<User> initOrder(String userId) {
// TODO Auto-generated method stub
System.out.println("用户id:"+userId);
//1、查询用户的收货地址
List<User> addressList = userService.getUserAddressList(userId);
for (User userAddress : addressList) {
System.out.println(userAddress);
}
return addressList;
}
}
原先在consumer.xml中的调用的远程服务的接口,生成远程服务代理的部分为:
此时在SpringBoot与Dubbo整合中通过@Reference来实现……
最后控制器代码如下:
package com.service.consumer.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dubbo.basics_interface.dto.User;
import com.dubbo.basics_interface.service.OrderService;
/*
* @author zzf
* @date 2019年1月30日 下午3:04:45
*/
@RestController
public class DemoController {
@Autowired
OrderService orderService;
@GetMapping("/demo")
public List<User> demo(String id) {
return orderService.initOrder(id);
}
}
启动
测试:
在监控台可以看到调用的信息:
OK~