1、安装注册中心
(1)访问 https://downloads.apache.org/zookeeper/stable/ 选择后缀为bin.tar.gz 的压缩包下载解压。
(2)确保电脑配置了jdk系统环境变量。
(3)修改conf 目录下的 zoo_sample.cfg 为 zoo.cfg 因为 bin 目录下的 zkEnv 脚本文件配置中声明的是 zoo.cfg
2、创建一个 根目录 dubbo-demo,目录下创建三个 maven project 的 module,分别对应服务提供者、服务消费者和公共的API
公共API
bean
package com.atguigu.gmall.bean; import java.io.Serializable; public class UserAddress implements Serializable { private Integer id; private String userAddress; private String userId; private String consignee; private String phoneNum; private String isDefault; public UserAddress() { super(); } public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) { this.id = id; this.userAddress = userAddress; this.userId = userId; this.consignee = consignee; this.phoneNum = phoneNum; this.isDefault = isDefault; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getConsignee() { return consignee; } public void setConsignee(String consignee) { this.consignee = consignee; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getIsDefault() { return isDefault; } public void setIsDefault(String isDefault) { this.isDefault = isDefault; } }
service
package com.atguigu.gmall.service; import com.atguigu.gmall.bean.UserAddress; import java.util.List; public interface UserService { public List<UserAddress> getUserAddressList(String userId); }
package com.atguigu.gmall.service; import com.atguigu.gmall.bean.UserAddress; import java.util.List; public interface OrderService { public List<UserAddress> initOrder(String userId); }
服务提供者
UserService 实现
package com.atguigu.gmall.service.impl; import com.atguigu.gmall.bean.UserAddress; import com.atguigu.gmall.service.UserService; import java.util.Arrays; import java.util.List; public class UserServiceImpl implements UserService { public List<UserAddress> getUserAddressList(String userId) { UserAddress address1 = new UserAddress(1,"beijingshichangping","1", "lilaoshi","0536-0877651","yes"); UserAddress address2 = new UserAddress(2, "shenzhenshi", "1", "wanglaoshi", "0536-0671118", "no"); return Arrays.asList(address1, address2); } }
XML配置
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 服务提供者的应用名称 --> <dubbo:application name="user-service-provider"/> <!-- 声明使用的注册中心 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 声明协议以及端口 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- service implementation, as same as regular local bean --> <bean id="userService" class="com.atguigu.gmall.service.impl.UserServiceImpl"/> <!-- 暴露服务接口 --> <dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userService"/> </beans>
编写服务提供者的启动类
package com.atguigu.gmall; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApplication { public static void main(String[] args) throws Exception { System.setProperty("java.net.preferIPv4Stack", "true"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider"); context.start(); System.out.println("Provider started."); System.in.read(); // press any key to exit } }
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>org.example</groupId> <artifactId>user-service-provider</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.example</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> </dependencies> </project>
服务消费者
OrderService 实现
package com.atguigu.gmall.service.impl; import com.atguigu.gmall.bean.UserAddress; import com.atguigu.gmall.service.OrderService; import com.atguigu.gmall.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class OrderServiceImpl implements OrderService { @Autowired UserService userService; public List<UserAddress> initOrder(String userId) { List<UserAddress> addressList = userService.getUserAddressList(userId); System.out.println(addressList); return addressList; } }
XML 配置
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:http="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <context:component-scan base-package="com.atguigu.gmall.service.impl"></context:component-scan> <!-- provider's application name, used for tracing dependency relationship --> <dubbo:application name="user-service-consumer"/> <!-- use multicast registry center to export service --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- use dubbo protocol to export service on port 20880 --> <!-- declare the service interface to be exported --> <dubbo:reference interface="com.atguigu.gmall.service.UserService" id="userService"/> </beans>
编写服务消费者启动类
package com.atguigu.gmall; import com.atguigu.gmall.service.OrderService; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class MainApplication { @SuppressWarnings("resource") public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer"); OrderService orderService = context.getBean(OrderService.class); orderService.initOrder("1"); System.out.println("调用完成。。。"); System.in.read(); } }
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>org.example</groupId> <artifactId>order-service-consumer</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.example</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> </dependencies> </project>
注意,必须先启动 服务提供者启动类,然后再启动 服务消费者启动类,才能正确调用到 服务提供者暴露出来的接口。
3、监控中心 dubbo-admin
访问dubbo官网,点击 GITHUB,下拉找到 Dubbo Admin,进入后选择“master”分支,把代码克隆下来。
代码解压后,启动 dubbo-admin ,然后访问默认的端口 7001, localhost:7001 默认用户名密码:root/root