Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架,可以和Spring框架无缝集成。
Apache Dubbo 提供了六大核心能力:面向接口代理的高性能RPC调用
,智能容错和负载均衡
,服务自动注册和发现
,高度可扩展能力
,运行期流量调度
,可视化的服务治理与运维
。
服务治理(SOA governance),企业为了确保项目顺利完成而实施的过程,包括最佳实践、架构原则、治理规程、规律以及其他决定性的因素。服务治理指的是用来管理SOA的采用和实现的过程。
参考官网 服务治理
节点 | 角色名称 |
---|---|
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的服务消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行容器 负责启动 加载 运行服务提供者 |
调用关系说明
通过前面的Dubbo架构图可以看到,Registry(服务注册中心)在其中起着至关重要的作用。Dubbo官方推荐使用Zookeeper作为服务注册中心。Zookeeper 是 Apache Hadoop 的子项目,作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。
程序实现分为以下几步骤:
1、创建父工程demo_base,并引入maven依赖
<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.0modelVersion>
<groupId>com.cydgroupId>
<artifactId>demo_baseartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>service-apimodule>
<module>service-providermodule>
<module>service-consumermodule>
modules>
<properties>
<dubbo.version>2.7.1dubbo.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-commonartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-apiartifactId>
exclusion>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-commonartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-rpc-dubboartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-apiartifactId>
exclusion>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-commonartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-netty4artifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-apiartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-serialization-hessian2artifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-commonartifactId>
exclusion>
exclusions>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.16version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.5version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.5version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.3version>
<configuration>
<source>11source>
<target>11target>
configuration>
plugin>
plugins>
build>
project>
<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">
<parent>
<artifactId>demo_baseartifactId>
<groupId>com.cydgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>service-apiartifactId>
project>
2、提供API接口
public interface HelloService {
String sayHello(String name);
}
<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">
<parent>
<artifactId>demo_baseartifactId>
<groupId>com.cydgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>service-providerartifactId>
<dependencies>
<dependency>
<groupId>com.cydgroupId>
<artifactId>service-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>4.2.0version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-rpc-dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-netty4artifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-serialization-hessian2artifactId>
dependency>
dependencies>
project>
2、api接口实现类
package com.cyd.provider.service.impl;
import com.cyd.api.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello:" + name;
}
}
3、配置类ProviderConfiguration
package com.cyd.provider.config;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@EnableDubbo(scanBasePackages = "com.cyd.provider.service.impl")
@PropertySource("classpath:/dubbo-provider.properties")
public class ProviderConfiguration {
}
4、配置文件 dubbo-provider.properties
dubbo.application.name=service-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.registry.address=zookeeper://49.235.242.61:2181
5、启动类
package com.cyd.provider;
import com.cyd.provider.config.ProviderConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
public class ProviderMain {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
System.in.read();
}
}
<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">
<parent>
<artifactId>demo_baseartifactId>
<groupId>com.cydgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>service-consumerartifactId>
<dependencies>
<dependency>
<groupId>com.cydgroupId>
<artifactId>service-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-rpc-dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-netty4artifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-serialization-hessian2artifactId>
dependency>
dependencies>
project>
2、通过@Reference注解注入远程服务
package com.cyd.consumer.bean;
import com.cyd.api.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class ConsumerComponent {
@Reference
private HelloService helloService;
public String sayHello(String name) {
return helloService.sayHello(name);
}
}
3、配置类 ConsumerConfiguration
package com.cyd.consumer.config;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan("com.cyd.consumer.bean")
@EnableDubbo
public class ConsumerConfiguration {
}
4、配置文件 dubbo-consumer.properties
dubbo.application.name=service-consumer
dubbo.registry.address=zookeeper://49.235.242.61:2181
dubbo.consumer.timeout=4000
5、主函数 AnnotationConsumerMain
package com.cyd.consumer;
import com.cyd.consumer.bean.ConsumerComponent;
import com.cyd.consumer.config.ConsumerConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AnnotationConsumerMain {
public static void main(String[] args) throws Exception {
System.out.println("-------------");
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
// 获取消费者组件
ConsumerComponent service = context.getBean(ConsumerComponent.class);
while (true) {
System.in.read();
String hello = service.sayHello("world");
System.out.println("result:" + hello);
}
}
}
下面我们来使用不同的方式来对Dubbo进行配置。每种配置方式各有不同,一般可以分为以下几个。
我们一般XML会结合Spring应用去进行使用,将Service的注册和引用方式都交给Spring去管理。下面我们还是针对于上面的demo进行实现。
这里我们针对于api模块不做处理,还是使用原先的接口。从提供者和消费者做讲解。这了我们直接通过spring的方式去做讲解。
1、引入dubbo依赖。与原先的不同点在于,最后多了spring的依赖引入。
<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">
<parent>
<artifactId>demo_base_xmlartifactId>
<groupId>com.cydgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>service-providerartifactId>
<dependencies>
<dependency>
<groupId>com.cydgroupId>
<artifactId>service-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>4.2.0version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-rpc-dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-netty4artifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-serialization-hessian2artifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-config-springartifactId>
dependency>
dependencies>
project>
2、编写实现类,不需要引入任何的注解配置。
package com.cyd.service.impl;
import com.cyd.api.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public void sayHello(String name) {
System.out.println("sayHello()--------------->>>" + name);
}
}
3、编写Spring配置文件 dubbo-provider.xml 文件,用于对dubbo进行文件统一配置。并且对刚才的配置进行引入。
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-demo-xml-provider"/>
<dubbo:registry address="zookeeper://49.235.242.61:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<bean id="helloService" class="com.cyd.service.impl.HelloServiceImpl"/>
<dubbo:service interface="com.cyd.api.HelloService" ref="helloService"/>
beans>
4、编写模块启动类。
package com.cyd;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("dubbo-provider.xml");
context.start();
System.in.read();
}
}
<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">
<parent>
<artifactId>demo_base_xmlartifactId>
<groupId>com.cydgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>service-consumerartifactId>
<dependencies>
<dependency>
<groupId>com.cydgroupId>
<artifactId>service-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-rpc-dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-netty4artifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-serialization-hessian2artifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-config-springartifactId>
dependency>
dependencies>
project>
2、定义Spring的配置文件 dubbo-consumer.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-demo-xml-consumer" />
<dubbo:registry address="zookeeper://49.235.242.61:2181" />
<dubbo:reference id="helloService" interface="com.cyd.api.HelloService" />
beans>
3、引入启动模块。因为引用了Spring框架,所以再上一步的helloService会被当做一个bean注入到真实的环境中。在我们生产级别使用的时候,我们可以通过Spring中的包扫描机制,通过@Autowired 这种机制来进行依赖注入。
package com.cyd;
import com.cyd.api.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("dubbo-consumer.xml");
context.start();
// 获取远程服务代理
HelloService helloService = (HelloService) context.getBean("helloService");
// 执行远程方法
helloService.sayHello("Tom");
}
}
主要包含:服务管理 、 路由规则、动态配置、服务降级、访问控制、权重调整、负载均衡等管理功能
如我们在开发时,需要知道Zookeeper注册中心都注册了哪些服务,有哪些消费者来消费这些服务。我们可以通过部署一个管理中心来实现。其实管理中心就是一个web应用,原来是war(2.6版本以前)包需要部署到tomcat即可。现在是jar包可以直接通过java命令运行。
1、从git 上下载项目 https://github.com/apache/dubbo-admin
2、修改 dubbo-admin 项目下的application.properties文件
注意dubbo.registry.address对应的值需要对应当前使用的Zookeeper的ip地址和端口号
3、切换到项目所在的路径使用 mvn 打包
mvn clean package -Dmaven.test.skip=true
4、java 命令运行
java -jar 对应的jar包
对应 org.apache.dubbo.config.ApplicationConfig, 代表当前应用的信息
org.apache.dubbo.config.RegistryConfig, 代表该模块所使用的注册中心。一个模块中的服务可以将其注册到多个注册中心上,也可以注册到一个上。后面再service和reference也会引入这个注册中心。
org.apache.dubbo.config.ProtocolConfig, 指定服务在进行数据传输所使用的协议。
org.apache.dubbo.config.ServiceConfig, 用于指定当前需要对外暴露的服务信息,后面也会具体讲解。和 dubbo:reference 大致相同。
org.apache.dubbo.config.ReferenceConfig, 消费者的配置,这里只做简单说明,后面会具体讲解。
org.apache.dubbo.config.MethodConfig, 用于在制定的 dubbo:service 或者 dubbo:reference 中的更具体一个层级,指定具体方法级别在进行RPC操作时候的配置,可以理解为对这上面层级中的配置针对于具体方法的特殊处理。
这两个在dubbo中是我们最为常用的部分,其中有一些我们必然会接触到的属性。并且这里会讲到一些设置上的使用方案。
Schema 配置参考手册