下载链接:https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.5.8/apache-zookeeper-3.5.8-bin.tar.gz
解压后找 bin>>zkServer.cmd 双击运行即可,可能出现闪退的情况。
解决办法:以文本方式打开zkServer,在这个位置添加pause,就不会闪退了。
双击运行即可。成功运行之后就可以。
ls /
三者关系:comsumer向外提供服务,api向comsumer提供接口实现,provider实现api接口。
整体结构如图:
父级Pom.xml
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<groupId>com.wdj</groupId>
<artifactId>demo-dubbo</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<boot.dubbo.version>2.0.0</boot.dubbo.version>
<zkclient.version>0.10</zkclient.version>
</properties>
<modules>
<module>dubbo-api</module>
<module>provider</module>
<module>comsumer</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- alibaba dubbo-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${boot.dubbo.version}</version>
</dependency>
<!-- ZooKeeper client -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
</project>
作用:定义接口给Comsumer调用;Provider同时会实现这些接口。API还定义实体类。
dobbo-api整体结构如下图:
Pom.xml
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>
<parent>
<groupId>com.wdj</groupId>
<artifactId>demo-dubbo</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.wdj</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0</version>
<name>dubbo-api</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--com.dubbo-springBoot依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
</project>
Service:
/**
* @类名称:UserService
* @类描述:
* @日期:2018年6月17日
* @版本:V1.0
*/
public interface UserService {
/**
* 通过id查询用户
* @param id
* @return T
* @日期:2018年6月17日
*/
public UserDO findById(@NotBlank Long id);
/**
* 查询用户列表
* @return List
* @日期:2018年6月17日
*/
public List<UserDO> listUser();
}
Entity:
public class UserDO implements Serializable {
/**
* @字段名:serialVersionUID
* @字段描述:{todo}
*/
private static final long serialVersionUID = 5438739092677089251L;
private Long id;
private String name;
private String passwrod;
public UserDO(Long id, String name, String passwrod) {
this.id = id;
this.name = name;
this.passwrod = passwrod;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPasswrod() {
return passwrod;
}
public void setPasswrod(String passwrod) {
this.passwrod = passwrod;
}
}
作用:实现api接口,处理业务逻辑,对数据库进行操作。
provider整体结构如图:
pom.xml
<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>
<parent>
<groupId>com.wdj</groupId>
<artifactId>demo-dubbo</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.wdj</groupId>
<artifactId>dubbo-provider</artifactId>
<version>${revision}</version>
<name>dubbo-provider</name>
<dependencies>
<dependency>
<groupId>com.wdj</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--持久层框架-->
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mybatis框架-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<!--基础插件-->
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ServiceImpl:
这里用了假数据,没有连接数据库。
package com.wdj.provider.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.wdj.dubboapi.entity.UserDO;
import com.wdj.dubboapi.service.UserService;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
/**
* @类名称:UserServiceImpl
* @类描述:实现用户管理接口
* @作者:
* @日期:2018年6月17日
* @版本:V1.0
*/
@Service(interfaceClass = UserService.class)
@Component
public class UserServiceImpl implements UserService {
@SuppressWarnings("serial")
private List<UserDO> users = new ArrayList<UserDO>(){{
add(new UserDO(1L, "熊大", "123"));
add(new UserDO(2L, "熊二", "234"));
add(new UserDO(3L, "熊三", "456"));
}};
/**
* 覆盖方法:findById
* 描述:通过id查询用户
* @param id
* @return
*/
@Override
public UserDO findById(Long id) {
return users.stream().filter(user -> user.getId() == id).findFirst().get();
}
/**
* 覆盖方法:listUser
* 描述:查询用户列表
* @return
*/
@Override
public List<UserDO> listUser() {
return users;
}
}
启动类:添加注解@EnableDubboConfiguration
application.yml配置文件
server:
port: 7001
spring:
#dubbo的配置
dubbo:
application:
id: demo-dubbo-provider
name: demo-dubbo-provider
registry:
address: zookeeper://127.0.0.1:2181
server: true
protocol:
name: dubbo
port: 20800
#数据库连接配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
#mybatis的相关配置
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.wdj.provider.entity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
作用:向外提供服务,通过RPC调用Provider。
Comsumer整体结构:
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wdj</groupId>
<artifactId>comsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>comsumer</name>
<description>comsumer</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.wdj</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Swagger接口框架-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Controller:要注意引入的包
package com.wdj.comsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.wdj.dubboapi.entity.UserDO;
import com.wdj.dubboapi.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class HelloController {
@Reference
UserService userService;
@GetMapping("/findById/{id}")
public UserDO findById(@PathVariable Long id){
return userService.findById(id);
}
@GetMapping("/listUser")
public List<UserDO> listUser(){
return userService.listUser();
}
}
启动类:添加注解@EnableDubboConfiguration
@SpringBootApplication
@EnableDubboConfiguration
@EnableSwagger2
public class ComsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ComsumerApplication.class, args);
}
}
application.yml配置文件
server.port=7002
spring.dubbo.application.name=demo-dubbo-consumer
spring.dubbo.application.id=demo-dubbo-consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.port=20800
spring.dubbo.protocol.name=dubbo
解决方法:在父级的pom.xml添加以下片段。
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>