可以转载,但请在文章开头注明出去,谢谢!
上一篇介绍了一个能一次跑通的入门springboot dubbo demo,地址:https://blog.csdn.net/a704397849/article/details/91904085
本篇在上一篇的基础上做了修改,公共api服务独立作为一个maven项目被dubbo提供者和消费者引用。dubbo消费者增加了web使用。
注册中心 zookeeper 3.4.14 和 管理平台 dubbo-admin 2.5x 的安装使用 本文就不再介绍了,看上篇博文
开发工具
- idea 版本:ULTIMATE 2018.1
公共api服务 (maven项目)
创建maven项目
添加要注册的服务
- User.java ,注意要序列化Serializable
package bean;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
public int id;
public String name;
public String sex;
public User(){}
public User(int id, String name, String sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
- UserService.java
package service;
import bean.User;
public interface UserService {
User getUser(Integer id);
}
maven打包生成jar包 , 这个jar包会被下面的provider 和 consumer 项目导入
注:上面打包命令是在idea自带的命令行终端执行的,也可以在系统终端执行: win + R 输入cmd , 工作目录跳转到公共api服务项目根目录 执行命令 mvn clean install
在项目根目录下 执行 mvn clean install 后 会在项目根目录下生成target目录,生成的jar包就在target里面
mvn install 打包后会将jar包部署到maven仓库中
provider
注: provider 和consumer 的运行都需要注册中心zookeeper已经启动了
创建 提供者 springboot项目
provider 项目目录结构,下面箭头指向文件需要添加或更改内容
pom.xml 里的 dependencies 内容替换掉
注:这里有导入之前部署到本地maven仓库的 公共api服务jar包
注:替换后, 右键pom.xml -> Maven - > Reimport
<dependencies>
<!--公共api服务jar包-->
<dependency>
<groupId>com.dubbo</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
UserServiceImpl.java
package com.dubbo.provider.service;
import bean.User;
import com.alibaba.dubbo.config.annotation.Service;
import service.UserService;
import java.util.ArrayList;
/**
* @author
*/
@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {
static ArrayList<User> userList = new ArrayList<>();
static {
userList.add(new User(1,"zhangsan","男"));
userList.add(new User(2,"lisi","男"));
userList.add(new User(3,"wangwu","女"));
userList.add(new User(4,"zhaoliu","女"));
}
@Override
public User getUser(Integer id) {
for (User user:userList){
if(id == user.getId()){
return user;
}
}
return null;
}
}
application.properties
#应用名
spring.dubbo.application.name=provider
#注册中心地址
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
#协议端口
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#扫描包
spring.dubbo.scan=com.dubbo.provider.service
启动 provider项目
dubbo-admin 启动后,浏览器输入http://127.0.0.1:8080/dubbo-admin-2.5.10/ 查看提供者信息 (没有安装dubbo-admin,看文章头介绍的上一篇博文)
点击 172.16.1.112:20880 查看详细信息
consumer
创建 消费者 springboot项目
consumer 项目目录结构,下面箭头指向文件需要添加或更改内容
pom.xml 里的 dependencies 内容替换掉
注:这里有导入之前部署到本地maven仓库的 公共api服务jar包
注:替换后, 右键pom.xml -> Maven - > Reimport
<dependencies>
<!--公共api服务jar包-->
<dependency>
<groupId>com.dubbo</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
<!-- 避免依赖冲突,也就是避免阿里巴巴的dubbo中自带的spring依赖跟我们自己添加的依赖产生冲突 -->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
</dependencies>
UserController.java
package com.dubbo.consumer.controller;
import bean.User;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Reference(version = "1.0.0")
public UserService userService;
@RequestMapping(value = "/query",method = RequestMethod.GET)
@ResponseBody
public String getUser(@RequestParam(value="id",defaultValue="1",required=true) int id){
User user = userService.getUser(id);
if(user != null){
System.out.println(user.toString());
return user.toString();
}
System.out.println("user is not exist!");
return "user is not exist!";
}
}
ConsumerApplication.java , 注意 注解
package com.dubbo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations = {"classpath:dubbo-consumer.xml"})
@ComponentScan(basePackages = "com.dubbo.consumer")
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
application.properties
server.port=8082
#dubbo相关配置都放到 dubbo-consumer.xml 中, 原因是dubbo配置写在这里的话,dubbo的@refrence注解会和spring的注解产生冲突
#如果需要更深入需要了解spring启动加载配置的顺序
dubbo-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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 需要使用到提供者服务的包路径,多个包时用逗号隔开。注: 网上好多这里写错了 -->
<dubbo:annotation package="com.dubbo.consumer.controller"/>
</beans>
启动 consumer项目
dubbo-admin 启动后,浏览器输入http://127.0.0.1:8080/dubbo-admin-2.5.10/ 查看消费者信息 (没有安装dubbo-admin,看文章头介绍的上一篇博文)
浏览器输入 127.0.0.1:8082/user/query
浏览器输入 127.0.0.1:8082/user/query?id=3
源码
链接:https://pan.baidu.com/s/13i03s2lZnEyy_PQnEo9hbA
提取码:lo46
注:首先 注册中心zookeeper要启动,生成公共api服务jar , 然后启动provider,最后启动consumer
最后
如果哪里写的不对,请评论指出,谢谢!