編寫一個dubbo demo
阿新 • • 發佈:2020-11-19
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;View Codeimport 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); } }View Code
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>View Code
編寫服務提供者的啟動類
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 } }View Code
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>View Code
服務消費者
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; } }View Code
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>View Code
編寫服務消費者啟動類
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(); } }View Code
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>View Code
注意,必須先啟動 服務提供者啟動類,然後再啟動 服務消費者啟動類,才能正確呼叫到 服務提供者暴露出來的介面。
3、監控中心 dubbo-admin
訪問dubbo官網,點選 GITHUB,下拉找到 Dubbo Admin,進入後選擇“master”分支,把程式碼克隆下來。
程式碼解壓後,啟動 dubbo-admin ,然後訪問預設的埠 7001, localhost:7001 預設使用者名稱密碼:root/root