一、为什么要用Dubbo
1.远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2.软负载均衡及容错机制: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
3.服务自动注册与发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器
4.提供完善的管理控制台dubbo-admin与简单的控制中心dubbo-monitor
5.Dubbo提供了伸缩性很好的插件模型,很方便进行扩展(ExtensionLoader)
6.支持多协议
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,
只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
二、搭建SpringBoot+maven+Dubbo+Zookeeper(soa服务)
1、windows搭建zookeeper服务和启动服务
1>下载zookeeper,地址:http://apache.fayea.com/zookeeper/
2>解压下载的文件, 然后把解压后文件放到你要的位置。D:\zookeeper\zookeeper-3.4.11
3>进入D:\zookeeper\zookeeper-3.4.11\conf下,将里面的zoo_sample.cfg文件,做一个备份,然后改名为zoo.cfg,因为zookeeper启动后,只认识zoo.cfg中的所有设置和配置
4>在zoo.cfg中输入:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=D:\\zookeeper\\zookeeper-3.4.11\\data
dataLogDir=D:\\zookeeper\\zookeeper-3.4.11\\log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
5>在D:\zookeeper\zookeeper-3.4.11\下创建log和data文件夹(默认不创建会自动创建,自己创建以免万一)
6>使用cmd,进入D:\zookeeper\zookeeper-3.3.6\bin,运行zkServer.cmd,启动服务,如下图(port:2181是zookeeper的专用监听端口)。
三、springboot-dubbo-server
1>项目结构:
2、本文基于eclipse4.7.2+sts(springsource-tool-suite)+maven3.5.2搭建的,所以之前eclipse必须安装了sts和配置好了maven仓库,sts安装地址:https://jingyan.baidu.com/article/1612d5005fd087e20f1eee10.html。此处不累述了。
sts插件安装好之后.
1>File-new-other-SpringBoot-Spring Starter Project, Next.
2>填写相应的信息,Next
3>选择依赖web,Finish
4>pom.xml,注意:dubbo内部强制要用log4j,所以此处加上log4j的依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hzg.boot</groupId>
<artifactId>springboot-dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-dubbo-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>
<dependencies>
<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>
<!-- 注意:dubbo强制内置log4J 如果没有则会报错 -->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5>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
#dubbo 资源扫描文件
spring.dubbo.scan=com.hzg.boot.server.dubbo
#日志配置 dubbo强制内置log4j
#logging.config=classpath:logback-boot.xml
6>建立以下相关class
<1>EmployeeService
package com.hzg.boot.server.dubbo;
import com.hzg.boot.server.entity.Employee;
/**
* 员工服务层接口
* @author hzg1214
*
*/
public interface EmployeeService {
/**
* 根据员工姓名查询
* @param employeeName
* @return
*/
Employee queryEmployeeByName(String employeeName);
}
<2>EmployeeServiceImpl
package com.hzg.boot.server.dubbo.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.hzg.boot.server.dubbo.EmployeeService;
import com.hzg.boot.server.entity.Employee;
//注册为 Dubbo 服务 此@Service有dubbo提供
@Service(version="1.0.0")
public class EmployeeServiceImpl implements EmployeeService {
@Override
public Employee queryEmployeeByName(String employeeName) {
return new Employee(1,"上海","大城市");
}
}
<3>Employ
package com.hzg.boot.server.entity;
import java.io.Serializable;
/**
* 员工表
*
* @author hzg1214
*
*/
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String employee_name;
private String employee_age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmployee_name() {
return employee_name;
}
public void setEmployee_name(String employee_name) {
this.employee_name = employee_name;
}
public String getEmployee_age() {
return employee_age;
}
public void setEmployee_age(String employee_age) {
this.employee_age = employee_age;
}
@Override
public String toString() {
return "Employee [id=" + id + ", employee_name=" + employee_name + ", employee_age=" + employee_age + "]";
}
public Employee(Integer id, String employee_name, String employee_age) {
super();
this.id = id;
this.employee_name = employee_name;
this.employee_age = employee_age;
}
public Employee() {
super();
}
}
7>打包项目并运行,右击项目-Run As-Maven clean,成功之后在Run As-Maven build-Goals中输入compile,
启动SpringbootDubboServerApplication
四、搭建springboot-dubbo-client
1、项目结构
2、相关添加的class
<1>application.properties
#防止服务消费者和提供者端口冲突(一致)
server.port=8888
## Dubbo 消费者
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.hzg.boot.client.dubbo
<2>EmployeeService
package com.hzg.boot.client.dubbo;
import com.hzg.boot.client.entity.Employee;
/**
* 员工服务层接口
* @author hzg1214
*
*/
public interface EmployeeService {
/**
* 根据员工姓名查询
* @param employeeName
* @return
*/
Employee queryEmployeeByName(String employeeName);
}
<3>EmployeeConsumerService
package com.hzg.boot.client.dubbo;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Reference;
import com.hzg.boot.client.entity.Employee;
/**
* dubbo 员工消费者
* @author hzg1214
*
*/
@Component
public class EmployeeConsumerService {
@Reference(version="1.0.0")
EmployeeService employeeService;
public EmployeeService getEmployeeService() {
return employeeService;
}
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public void printEmployee() {
String employeeName = "上海";
if(employeeService == null){
System.out.println("employeeService 服务对象 为空");
}else {
Employee employee = employeeService.queryEmployeeByName(employeeName);
System.out.println(employee.toString());
}
}
}
<4>Employee
package com.hzg.boot.client.entity;
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String employee_name;
private String employee_age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmployee_name() {
return employee_name;
}
public void setEmployee_name(String employee_name) {
this.employee_name = employee_name;
}
public String getEmployee_age() {
return employee_age;
}
public void setEmployee_age(String employee_age) {
this.employee_age = employee_age;
}
@Override
public String toString() {
return "Employee [id=" + id + ", employee_name=" + employee_name + ", employee_age=" + employee_age + "]";
}
public Employee(Integer id, String employee_name, String employee_age) {
super();
this.id = id;
this.employee_name = employee_name;
this.employee_age = employee_age;
}
public Employee() {
super();
}
}
<5>SpringbootDubboZookeeperClientApplication 中添加
package com.hzg.boot.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import com.hzg.boot.client.dubbo.EmployeeConsumerService;
@SpringBootApplication
public class SpringbootDubboZookeeperClientApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringbootDubboZookeeperClientApplication.class, args);
EmployeeConsumerService employeeService = run.getBean(EmployeeConsumerService.class);
employeeService.printEmployee();
}
}
<6>启动项目,和server一致。注意先启动server,在启动client
控制即会答应相应的信息。