1.基于以下图实现服务 提供者、消费者
2.前期工作
父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>
<groupId>com.tianxia</groupId>
<artifactId>dubbo-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>user-service-provider</module>
</modules>
<!-- 统一管理jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!--log4f-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3.创建通用模块 - api-common
POM
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<!--optional是maven依赖jar时的一个选项,表示该依赖是可选的,不会被依赖传递-->
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>false</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
暂无YML
业务类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 用户地址
* @author liqb
* @date 2023-05-28 16:39
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserAddress implements Serializable {
private Integer id;
/**
* 用户地址
*/
private String userAddress;
/**
* 用户id
*/
private String userId;
/**
* 收货人
*/
private String consignee;
/**
* 电话号码
*/
private String phoneNum;
/**
* 是否为默认地址 Y-是 N-否
*/
private String isDefault;
}
import com.tianxia.dubbo.entity.UserAddress;
import java.util.List;
/**
* 用户Service
* @author liqb
* @date 2023-05-28 16:47
*/
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @author liqb
* @date 2023-05-28 16:48
* @param userId 用户id
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
}
/**
* 订单服务
* @author liqb
* @date 2023-05-28 16:52
*/
public interface OrderService {
/**
* 初始化订单
* @author liqb
* @date 2023-05-28 16:52
* @param userID 用户id
*/
public void initOrder(String userID);
}
2.创建用户服务提供者 - user-service-provider
POM
<dependencies>
<!-- 通用模块 -->
<dependency>
<groupId>com.tianxia</groupId>
<artifactId>api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!--注册中心是 zookeeper,引入zookeeper客户端-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
</dependencies>
provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="user-service-provider"/>
<!-- 2、指定注册中心的位置 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"/> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!--3、指定通信规则(通信协议? 服务端口)-->
<dubbo:protocol name="dubbo" port="20881"/>
<!-- 4、暴露服务 让别人调用 ref指向服务的真正实现对象 -->
<dubbo:service interface="com.tianxia.dubbo.api.UserService" ref="userServiceImpl"/>
<!-- 服务的实现 -->
<bean id="userServiceImpl" class="com.tianxia.dubbo.service.impl.UserServiceImpl"/>
</beans>
业务类
import com.tianxia.dubbo.api.UserService;
import com.tianxia.dubbo.entity.UserAddress;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
/**
* 用户服务实现类
* @author liqb
* @date 2023-05-28 16:48
*/
@Service
public class UserServiceImpl implements UserService {
/**
* 按照用户id返回所有的收货地址
* @author liqb
* @date 2023-05-28 16:48
* @param userId 用户id
* @return
*/
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress address1 = new UserAddress(1, "河南省郑州巩义市宋陵大厦2F", "1", "安然", "150360313x", "Y");
UserAddress address2 = new UserAddress(2, "北京市昌平区沙河镇沙阳路", "1", "情话", "1766666395x", "N");
return Arrays.asList(address1,address2);
}
}
启动类
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* 启动类
* @author liqb
* @date 2023-05-28 17:27
*/
public class UserServiceProviderApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext= new ClassPathXmlApplicationContext("provider.xml");
applicationContext.start();
System.in.read();
}
}
zkServer.cmd、和zkCli.cmd服务
在dubbo-admin target中cmd运行 java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
再次启动项目,我们可以看到在zookeeper中已经发现服务提供者。
服务提供者的配置和测试完成
4.创建服务消费者(订单服务) - order-service-consumer
POM
<dependencies>
<!-- 通用模块 -->
<dependency>
<groupId>com.tianxia</groupId>
<artifactId>api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!--注册中心是 zookeeper,引入zookeeper客户端-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
</dependencies>
consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="com.tianxia.dubbo.service.impl"/>
<!-- 指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="order-service-consumer"/>
<!-- 指定注册中心的位置 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 调用远程暴露的服务,生成远程服务代理 -->
<dubbo:reference interface="com.tianxia.dubbo.api.UserService" id="userService"/>
</beans>
业务类
import com.tianxia.dubbo.api.OrderService;
import com.tianxia.dubbo.api.UserService;
import com.tianxia.dubbo.entity.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 订单服务实现类
* @author liqb
* @date 2023-05-28 16:54
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
public UserService userService;
/**
* 初始化订单
* @author liqb
* @date 2023-05-28 16:52
* @param userID 用户id
*/
@Override
public void initOrder(String userID) {
// 查询用户的收货地址
List<UserAddress> userAddressList = userService.getUserAddressList(userID);
// 为了直观的看到得到的数据,以下内容也可不写
System.out.println("当前接收到的userId=> "+userID);
System.out.println("**********");
System.out.println("查询到的所有地址为:");
for (UserAddress userAddress : userAddressList) {
// 打印远程服务地址的信息
System.out.println(userAddress.getUserAddress());
}
}
}
启动类
import com.tianxia.dubbo.api.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* 启动类
* @author liqb
* @date 2023-05-28 17:40
*/
public class OrderServiceConsumerApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = applicationContext.getBean(OrderService.class);
// 调用方法查询出数据
orderService.initOrder("1");
System.out.println("调用完成...");
System.in.read();
}
}
注意:消费者的运行测试需要先启动提供者。
启动服务提供者、消费者。及zookeeper的和dubbo-admin,查看监控信息。
localhost:7001