一、安装zookeeper
1、复制新的配置文件,并且修改配置中的内容
2、启动zookeeper
3、使用zkCli.cmd 连接zookeeper
测试通过
二、安装控制台
1、在dubbo-admin下进行打包
2、输入以下命令
3、将生成的jar包放在根目录
4、 在 localhost:7001进行登录
5、输入账号密码进行登录 默认都是root
三、搭建测试环境
1、创建三个模块
并且将gmail-interface引入其他两个模块
在其他两个模块中的pom中加入依赖
(1)gmail-interface
1) UserAddress
package com.hei.gmall.bean;
import java.io.Serializable;
public class UserAddress implements Serializable {
private Integer id;
private String userAddress; //用户地址
private String userId; //用户id
private String consignee; //收货人
private String phoneNum; //电话号码
private String isDefault; //是否为默认地址 Y-是 N-否
public UserAddress() {
super();
// TODO Auto-generated constructor stub
}
public UserAddress(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;
}
}
2)OrderService
package com.hei.gmall.service;
public interface OrderService {
/**
* 初始化订单
*/
public void initOrder(String userId);
}
3)UserService
package com.hei.gmall.service;
import com.hei.gmall.bean.UserAddress;
import java.util.List;
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @param userId
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
}
(2)order-service-consumer
OrderServiceImpl
package com.hei.gmall.service.impl;
import com.hei.gmall.bean.UserAddress;
import com.hei.gmall.service.OrderService;
import com.hei.gmall.service.UserService;
import java.util.List;
/**
* 1、将服务提供者注册到注册中心(暴露服务)
* 1)、导入dubbo依赖(2.6.2)
* 2)、
*
* 2、让消费者去注册中心订阅服务提供者的服务地址
*/
public class OrderServiceImpl implements OrderService {
UserService userService;
@Override
public void initOrder(String userId) {
//1、查询用户的地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
System.out.println(addressList);
}
}
(3)user-service-provider
四、提供者引入Dubbo依赖进行注册
1、在user-service-provider的resource下建一个配置文件
<?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://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">
<!--1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)-->
<dubbo:application name="user-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="20880"></dubbo:protocol>
<!--4、暴露服务 让别人调用 ref指向服务的真正实现对象-->
<dubbo:service interface="com.hei.gmall.service.UserService" ref="userServiceImpl"></dubbo:service>
<!--服务的实现-->
<bean id="userServiceImpl" class="com.hei.gmall.service.impl.UserServiceImpl"></bean>
</beans>
2、建一个启动类
package com.hei.gmall;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
System.in.read();
}
}
3、启动测试类
(1)控制台效果
(2)页面显示效果
提供者
应用
五、消费者引入Dubbo依赖进行注册
1、在pom中引入依赖
2、在resource下创建counsumer.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.hei.gmall.service.impl"/>
<!--指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)-->
<dubbo:application name="order-service-consumer"></dubbo:application>
<!--指定注册中心的位置-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!--调用远程暴露的服务,生成远程服务代理-->
<dubbo:reference interface="com.hei.gmall.service.UserService" id="userService"></dubbo:reference>
</beans>
3、在OrderServiceImpl加上两个注解,并修改代码
package com.hei.gmall.service.impl;
import com.hei.gmall.bean.UserAddress;
import com.hei.gmall.service.OrderService;
import com.hei.gmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 1、将服务提供者注册到注册中心(暴露服务)
* 1)、导入dubbo依赖(2.6.2)
* 2)、
*
* 2、让消费者去注册中心订阅服务提供者的服务地址
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserService userService;
@Override
public void initOrder(String userId) {
System.out.println("用户id:"+userId);
//1、查询用户的地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
for (UserAddress userAddress : addressList) {
System.out.println(userAddress.getUserAddress());
}
}
}
4、写一个启动方法
package com.hei.gmall;
import com.hei.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 applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = applicationContext.getBean(OrderService.class);
//调用方法查询出数据
orderService.initOrder("1");
System.out.println("调用完成...");
System.in.read();
}
}
@SuppressWarnings注解 - morganlin - 博客园
5、测试
六、安装并启动简易控制台
localhost:8080
,可以看到一个监控中心。
在服务提供者和消费者的xml中配置以下内容,再次启动服务提供和消费者启动类。
<!--dubbo-monitor-simple监控中心发现的配置-->
<dubbo:monitor protocol="registry"></dubbo:monitor>
<!--<dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor>-->
七、Dubbo与SpringBoot整合
boot-user-service-provider 服务提供者
创建Maven项目 boot-user-service-provider
服务提供者
导入以下依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.hei.gmall</groupId>
<artifactId>gmail-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
把 user-service-provider
中的service拿到此项目中。
注意,以此方法为返回的需要更改 interface包中的void为 List
@Service
@Component
public class UserServiceImpl implements UserService {
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);
}
}
配置 application.properties
dubbo.application.name=boot-user-service-provider
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#连接监控中心
dubbo.monitor.protocol=registry
BootProviderApplication 启动类配置
@EnableDubbo //开启基于注解的dubbo功能
@SpringBootApplication
public class BootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootProviderApplication.class, args);
}
}
最终包目录
boot-order-service-consumer 服务消费者
创建Maven项目 boot-order-service-consumer
服务消费者
导入以下依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.hei.gmall</groupId>
<artifactId>gmail-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
把order-service-consumer项目中的service复制到当前项目。
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户id:"+userId);
//1、查询用户的地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
return addressList;
}
}
创建 OrderController 控制器
@Controller
public class OrderController {
/**
* 初始化订单
*/
@Autowired
OrderService orderService;
@RequestMapping("/initOrder")
@ResponseBody
public List<UserAddress> initOrder(@RequestParam("uid")String userId) {
return orderService.initOrder(userId);
}
}
创建application.properties 配置
server.port=8081
dubbo.application.name=boot-order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
#连接监控中心 注册中心协议
dubbo.monitor.protocol=registry
BootConsumerApplication 启动类创建
@EnableDubbo //开启基于注解的dubbo功能
@SpringBootApplication
public class BootConsumerApplication {
public static void main(String[] args){
SpringApplication.run(BootConsumerApplication.class,args);
}
}
最终包目录
配置完毕,此时启动zookeeper注册中心及监控。
启动springboot配置的服务提供者和消费者
在浏览器输入 localhost:7001 查看结果
查询到地址信息
duboo的springboot整合配置完成。