六、Springboot整合Dubbo+zookeeper(dubbo可视化)

1. 安装zookeeper (windows版)
解压后进入conf,将zoo_sample.cfg复制一份改名为zoo.cfg
注意配置中的参数:
dataDir=D:\\Setup\\zookeeper\\data
dataDirLog=D:\\Setup\\zookeeper\\log
clientPort=2181 (zookeeper的端口号)
返回bin目录,启动zookeeper:运行zkServer.cmd,运行完毕运行zkCli.cmd 测试

2.安装dubbo-admin管理控制台(windows版)
dubbo本身并不是一个服务软件。它其实就是一个jar包能够帮你的java程序连接到zookeeper,并利用zookeeper消费、提供服务。所以你不用在Linux上启动什么dubbo服务。
但是为了让用户更好的管理监控众多的dubbo服务,官方提供了一个可视化的监控程序,不过这个监控即使不装也不影响使用。
整个工程下下来
修改 src\main\resources\application.properties 指定zookeeper地址
cmd打包dubbo-admin mvn clean package -Dmaven.test.skip=true
(第一次打包时间可能有点长)
target中找到dubbo-admin-0.0.1-SNAPSHOT,cmd进去命令行 输入java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
访问: http://localhost:7001/ 看到如下界面
默认帐号密码都是root
3. 创建项目:
模拟需求:订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。
模块    功能
订单服务web模块    创建订单等
用户服务service模块    查询用户地址等
一、创建父工程Maven项目,springboot-dubbo
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>spring-boot-dubbo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mall-api</module>
</modules>
</project>
二、右键新建module创建maven项目:mall-api
工程结构如下:
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<artifactId>spring-boot-dubbo</artifactId>
<groupId>com.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mall-api</artifactId>
</project>
2.bean
package com.test.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();
}
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;
}
}
3.service,代码如下:
package com.test.service;
import com.test.bean.UserAddress;
import java.util.List;
public interface OrderService {
/**
* 初始化订单
* @param userId
*/
public List<UserAddress> initOrder(String userId);
public UserAddress getUserAddress(String userId);
}
package com.test.service;
import com.test.bean.UserAddress;
import java.util.List;
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @param userId
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
}
三、创建项目 mall-user-service-provider(非moudle)
工程结构如下:不需要创建Controller
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>mall-user-service-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>mall-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.启动类
package com.test;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String [] args){
SpringApplication.run(ProviderApplication.class, args);
}
}
3.配置文件
#1、指定当前服务/应用的名字
dubbo.application.name=mall-user-service-provider
#2、指定注册中心的位置
dubbo.registry.address=zookeeper://127.0.0.1:2181
#3、指定通信规则(通信协议、通信端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
#4连接监控中心
dubbo.monitor.protocol=registry
dubbo.scan.base-packages=com.test.service
4.service
package com.test.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.test.bean.UserAddress;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Service//5、暴露服务
@Component//6服务的实现
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
System.out.println("UserServiceImpl.....");
UserAddress address1 = new UserAddress(1, "深圳市福田区", "1", "test", "888888", "Y");
UserAddress address2 = new UserAddress(2, "深圳市南山区", "1", "test", "999999", "N");
return Arrays.asList(address1,address2);
}
}
注意:注解配置对应spring项目手动dubbo.xml配置参数的含义
<!-- 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连接监控中心 -->
<dubbo:monitor protocol="registry"/>
<!-- 5、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="com.shawn.gmall.service.UserService" ref="userServiceImp" timeout="1000" version="1.0.0">
    <dubbo:method name="getUserAddressList" timeout="1000"/>
</dubbo:service>
<!-- 6服务的实现 -->
<bean id="userServiceImp" class="com.shawn.gmall.service.impl.UserServiceImpl"/>
<!-- 7统一设置服务提供方的规则 -->
<dubbo:provider timeout="1000"/>
四、创建项目 mall-user-service-consumer(非moudle)
工程结构如下:
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>mall-order-service-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>mall-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.启动类
package com.test;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
3.配置文件
server.port=8084
#指定注册中心的位置
dubbo.application.name=mall-order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
#连接监控中心
dubbo.monitor.protocol=registry
4.service
package com.test.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.test.bean.UserAddress;
import com.test.service.OrderService;
import com.test.service.UserService;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 1、将服务提供者注册到注册中心(暴露服务)
* 1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator)
* 2)、配置服务提供者
*
* 2、让服务消费者去注册中心订阅服务提供者的服务地址
*
*/
@Service
public class OrderServiceImpl implements OrderService {
@Reference //使用dubbo提供的reference注解引用远程服务
UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户id:"+userId);
//1、查询用户的收货地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
for (UserAddress userAddress : addressList) {
System.out.println(userAddress.getUserAddress());
}
return addressList;
}
}
5.controller
package com.test.controller;
import com.test.bean.UserAddress;
import com.test.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class OrderController {
@Autowired
OrderService orderService;
@RequestMapping("/initOrder")
public List<UserAddress> initOrder(@RequestParam("uid") String userId){
return orderService.initOrder(userId);
}
6.启动提供者和消费者的启动类
7.访问: http://localhost:7001/ 看到如下界面
默认帐号密码都是root
返回结果
[{"id":1,"userAddress":"深圳市福田区","userId":"1","consignee":"test","phoneNum":"888888","isDefault":"Y"},{"id":2,"userAddress":"深圳市南山宝安区","userId":"1","consignee":"test","phoneNum":"999999","isDefault":"N"}]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值