启动Zookeeper和Dubbo-Admin
参考:https://blog.csdn.net/qq_37598011/article/details/86685467
启动Dubbo-Monitor-Simple
进入后执行mvn package命令
进入target目录
然后解压dubbo-monitor-simple-2.0.0-assembly.tar.gz这个压缩包
其目录如下:
进入conf目录中有dubbo.properties,这个文件是用来具体配置的:
启动
进入assembly.bin目录
执行start.bat文件
OK~
PS:如果想要修改这个端口号则修改配置文件的:dubbo.jetty.port=9999
Dubbo(服务提供者+服务消费者)
基础公共部分
一个用户类:
import java.io.Serializable;/*** @author zzf**/
public class User implements Serializable {private Integer id;private String userAddress; //用户地址private String userId; //用户idprivate String consignee; //收货人private String phoneNum; //电话号码private String isDefault; //是否为默认地址 Y-是 N-否public User() {super();// TODO Auto-generated constructor stub}public User(Integer id, String userAddress, String userId, String consignee, String phoneNum,String isDefault) {super();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;}@Overridepublic String toString() {return "UserAddress [id=" + id + ", userAddress=" + userAddress + ", userId=" + userId + ", consignee="+ consignee + ", phoneNum=" + phoneNum + ", isDefault=" + isDefault + "]";}}
接口类:
订单服务类:
package com.dubbo.basics_interface.service;import java.util.List;import com.dubbo.basics_interface.dto.User;public interface OrderService {/*** 初始化订单* @param userId*/public List<User> initOrder(String userId);}
用户服务类:
package com.dubbo.basics_interface.service;import java.util.List;import com.dubbo.basics_interface.dto.User;/*** 用户服务* @author zzf**/
public interface UserService {/*** 按照用户id返回所有的收货地址* @param userId* @return*/public List<User> getUserAddressList(String userId);}
此时该类的pom文件如下:
<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.dubbo</groupId><artifactId>basics-interface</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>basics-interface</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies>
</project>
服务提供者
pom文件如下:
<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.dubbo</groupId><artifactId>service-provider</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-provider</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- 引入dubbo --><!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.2</version></dependency><!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>2.12.0</version></dependency><!--引入公共信息--><dependency><groupId>com.dubbo</groupId><artifactId>basics-interface</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
</project>
实现用户服务的接口:
package com.dubbo.service_provider.service.impl;import java.util.Arrays;
import java.util.List;import org.springframework.stereotype.Component;import com.dubbo.basics_interface.dto.User;
import com.dubbo.basics_interface.service.UserService;/*
* @author zzf
* @date 2019年1月29日 下午3:40:16
*/
@Component
public class UserServiceImpl implements UserService{public List<User> getUserAddressList(String userId) {// TODO Auto-generated method stubUser 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);}}
创建src/main/resources目录并创建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://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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 包扫描 --><context:component-scan base-package="com.dubbo.service_provider.service.impl"></context:component-scan><!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) --><dubbo:application name="service-provider"></dubbo:application><!-- 2、指定注册中心的位置 --><!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> --><dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry><!-- 3、指定通信规则(通信协议?通信端口) --><dubbo:protocol name="dubbo" port="22288"></dubbo:protocol><!-- 4、暴露服务 ref:指向服务的真正的实现对象 --><dubbo:service interface="com.dubbo.basics_interface.service.UserService" ref="userServiceImpl" ></dubbo:service><!-- 连接监控中心 --><dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>
最后创建main方法测试:
package com.dubbo.service_provider;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Hello world!**/
public class App
{public static void main( String[] args ) throws IOException{String a=new String("provider.xml");ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(a);ioc.start();System.in.read();}
}
运行如下:
服务消费者
pom文件跟服务提供者一致
实现订单服务类:
package com.dubbo.service_consumer.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;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{@AutowiredUserService userService;public List<User> initOrder(String userId) {// TODO Auto-generated method stubSystem.out.println("用户id:"+userId);//1、查询用户的收货地址List<User> addressList = userService.getUserAddressList(userId);for (User userAddress : addressList) {System.out.println(userAddress);}return addressList;}}
创建src/main/resources目录并创建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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 包扫描 --><context:component-scan base-package="com.dubbo.service_consumer.service.impl"></context:component-scan><dubbo:application name="service-consumer"></dubbo:application><dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry><!-- 配置本地存根--><!--声明需要调用的远程服务的接口;生成远程服务代理 --><!-- 1)、精确优先 (方法级优先,接口级次之,全局配置再次之)2)、消费者设置优先(如果级别一样,则消费方优先,提供方次之)--><!-- timeout="0" 默认是1000ms--><!-- retries="":重试次数,不包含第一次调用,0代表不重试--><!-- 幂等(设置重试次数)【查询、删除、修改】、非幂等(不能设置重试次数)【新增】 --><dubbo:reference interface="com.dubbo.basics_interface.service.UserService" id="userService" ><!-- <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> --></dubbo:reference><!-- 连接监控中心 --><dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>
创建main方法测试:
package com.dubbo.service_consumer;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.dubbo.basics_interface.service.OrderService;/*** Hello world!**/
public class App {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");OrderService orderService = applicationContext.getBean(OrderService.class);orderService.initOrder("2222222");System.out.println("调用完成....");System.in.read();}
}
再看看监控中心:
OK~
官网文档:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html