官网有中文版 ------》dubbo.apache.org
PRC 远程过程调用
Remote Ptocedure Call
(分布式构架下的难点)
业务拆分 需要一个比较好的PRC框架
可以是我们像调用本地方法一样调用网络服务
RPC框架
Dubbo GRPC Thrift(跨语言 )
Dubbo
软负载均衡与容错, 依赖分析与降级
高性能NIO通讯及多协议集成
服务动态寻址与路由
Netty Mina
下载zookeeper
打开网络连接 将前两个都禁用掉
将conf下的cfg文件名改成zoo
12G
1.maven依赖
com.alibaba:dubbo:2.6.5
org.apache.curator:curator-farmework:2.13.0
2.创建maven 项目
- dubbo-hello pom (统一管理依赖)
- dubbo-hello-interface jar
- dubbo-hello-provider jar
- dubbo-hello-consumer jar
只留pom文件
d
同样的方式,创建第三个
如果这么引入依赖的话其他三个项目都会引入
这样的话就没了
<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.etoak.et1903.dubbo</groupId>
<artifactId>dubbo-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>dubbo-hello-interface</module>
<module>dubbo-hello-provider</module>
<module>dubbo-hello-consumer</module>
</modules>
<!-- 引入Dubbo依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.etoak.et1903.dubbo</groupId>
<artifactId>dubbo-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-hello-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.etoak.et1903.dubbo</groupId>
<artifactId>dubbo-hello-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
</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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.etoak.et1903.dubbo</groupId>
<artifactId>dubbo-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-hello-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.etoak.et1903.dubbo</groupId>
<artifactId>dubbo-hello-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
</project>
父工程有什么东西,他们三个里面都有
但是这里用了dependencyManagement
consumer 和 provider 都依赖 interface
package com.etoak.service;
public interface HelloService {
public String sayHello(String name);
}
package com.etoak.service.impl;
import com.etoak.service.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello :" + name;
}
}
<?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://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">
<!-- 1.配置提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-hello-provider" />
<!-- 2.使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 3.用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 4.声明需要暴露的服务接口 -->
<dubbo:service interface="com.etoak.service.HelloService"
ref="helloService" />
<!-- 5.和本地bean一样实现服务 -->
<bean id="helloService"
class="com.etoak.service.impl.HelloServiceImpl" />
</beans>
package com.etoak;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public abstract class ProviderTest {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
System.in.read();
}
}
启动jar 包
输入localhost:7070 用户名和密码都是root
<?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://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="hello-consumer" />
<!-- 使用zookeeper注册中心暴露发现服务地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean-->
<dubbo:reference id="helloService"
interface="com.etoak.service.HelloService" />
</beans>
package com.etoak;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.etoak.service.HelloService;
public class ConsumerTest {
public static void main(String[] args) throws IOException {
//读取consumer.xml文件
ApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");
//获取HelloService的代理
HelloService helloService = ioc.getBean("helloService", HelloService.class);
//通过代理调用远程服务
String result = helloService.sayHello("et1903");
System.out.println(result);
System.in.read();
}
}