一、Dubbo 快速入门
Dubbo核心功能解释
dubbo 阿里开源的一个SOA服务治理框架,从目前来看把它称作是一个RPC远程调用框架更为贴切。单从RPC框架来说,功能较完善,支持多种传输和序列化方案。所以想必大家已经知道他的核心功能了:就是远程调用。
Java Dubbo的远程调用
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.4.1</version>
</dependency>
<!--注册中心为zookeeper-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>2.7.4.1</version>
</dependency>
<!--接口组件-->
<dependency>
<groupId>application-compent</groupId>
<artifactId>member-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
服务端
public class DubboServer {
public static void main(String[] args) throws IOException {
// 开始 暴露 UserService 服务
// application
// protocol -dubbo 协议
// register
// service
ApplicationConfig applicationConfig = new ApplicationConfig("sample-app");
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setSerialization("fastjson");
protocolConfig.setPort(-1);//20880
RegistryConfig registryConfig = new RegistryConfig("zookeeper://192.168.0.147:2181");
ServiceConfig serviceConfig = new ServiceConfig();
serviceConfig.setInterface("com.tuling.client.UserService");
serviceConfig.setRef(new UserServiceImpl());
serviceConfig.setRegistry(registryConfig);
serviceConfig.setProtocol(protocolConfig);
serviceConfig.setApplication(applicationConfig);
// serviceConfig.setToken(true);
// setLoadbalance(serviceConfig);
serviceConfig.export();
System.out.println("服务已暴露");
System.in.read();
}
public static void setLoadbalance(ServiceConfig serviceConfig) {
serviceConfig.setLoadbalance("consistenthash");
MethodConfig methodConfig = new MethodConfig();
methodConfig.setName("findUser");
Map<String, String> paramter = new HashMap<>();
paramter.put("hash.arguments", "0,1");
paramter.put("hash.nodes", "320");
methodConfig.setParameters(paramter);
serviceConfig.setMethods(Arrays.asList(methodConfig));
}
public static void setMock(ServiceConfig serviceConfig, String server) {
serviceConfig.setRef(new MockService(server));
}
}
注意:在引用Dubbo包的时候注意dubbo版本问题,新版本引用的是apache下面的jar包。
public class DubboClient {
public static void main(String[] args) throws IOException {
RegistryConfig registryConfig = new RegistryConfig("zookeeper://192.168.0.147:2181"); // 虚拟的注册中心 局域网
ApplicationConfig applicationConfig = new ApplicationConfig("young-app");
ReferenceConfig referenceConfig = new ReferenceConfig();
referenceConfig.setRegistry(registryConfig);
referenceConfig.setApplication(applicationConfig);
referenceConfig.setTimeout(3000);
referenceConfig.setInterface(UserService.class);
referenceConfig.setFilter("-firstFilter");
UserService userService = (UserService) referenceConfig.get();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String line = bufferedReader.readLine();
if (line.equals("quit")) {
break;
}
System.out.println(userService.getUser(1));
}
}
}
Spring Dubbo的远程调用
共用接口
public interface UserService {
User getUser(Integer id);
}
provide.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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="young-app"/>
<dubbo:registry address="multicast://224.1.1.1:3333"/>
<!-- check 是否要检查注册中心可用 默认true-->
<dubbo:protocol name="dubbo" port="-1"/>
<dubbo:service interface="com.tuling.client.UserService" ref="userService"
timeout="4000">
<dubbo:method name="getUser" timeout="2000"/>
</dubbo:service>
<bean id="userService" class="com.tuling.dubbo.UserServiceImpl"></bean>
</beans>
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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="simple-app"/>
<dubbo:registry address="multicast://224.1.1.1:3333"/>
<dubbo:consumer timeout="5000"/>
<dubbo:reference id="userService" interface="com.tuling.client.UserService" async="false">
</dubbo:reference>
</beans>
服务端
public class SpringServer {
public static void main(String[] args) throws IOException {
new ClassPathXmlApplicationContext("provide.xml");
System.out.println("服务已暴露");
System.in.read();
}
}
客户端
public class SpringClient {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
UserService userService = context.getBean(UserService.class);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
if (bufferedReader.readLine().equals("quit")) {
break;
}
System.out.println(userService.getUser(1));
}
}
}
SpringBoot Dubbo的远程调用
共用接口
public interface UserService {
User getUser(Integer id);
}
服务端
dubbo.application.name=simple
dubbo.registry.address=multicast://224.1.1.1:3333
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
@EnableDubbo
@SpringBootApplication
public class BootServerApplication {
public static void main(String[] args) {
SpringApplication.run(BootServerApplication.class, args);
System.out.println("服务已开启");
}
}
@Service
@Component
public class UserServiceImpl implements UserService {
@Override
public User getUser(Integer id) {
User user = new User();
user.setId(id);
user.setName("luban:" + ManagementFactory.getRuntimeMXBean().getName());
user.setSex("man");
return user;
}
}
客户端
dubbo.application.name=young
dubbo.registry.address=multicast://224.1.1.1:3333
@EnableDubbo
@SpringBootApplication
public class DemoApplication {
@Reference
UserService userService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args).close();
}
@Bean
public ApplicationRunner getBean() {
return args -> {
System.out.println(userService.getUser(1));
};
}
}
二、Dubbo常规配置说明
标签 | 用途 | 解释 |
---|---|---|
<dubbo:application/> | 公共 | 用于配置当前应用信息,不管该应用是提供者还是消费者 |
<dubbo:registry/> | 公共 | 用于配置连接注册中心相关信息 |
<dubbo:protocol/> | 服务 | 用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受 |
<dubbo:service/> | 服务 | 用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心 |
<dubbo:provider/> | 服务 | 当 ProtocolConfig 和 ServiceConfig 某属性没有配置时,采用此缺省值,可选 |
<dubbo:consumer/> | 引用 | 当 ReferenceConfig 某属性没有配置时,采用此缺省值,可选 |
<dubbo:reference/> | 引用 | 用于创建一个远程服务代理,一个引用可以指向多个注册中心 |
<dubbo:method/> | 公共 | 用于 ServiceConfig 和 ReferenceConfig 指定方法级的配置信息 |
<dubbo:argument/> | 公共 | 用于指定方法参数配置 |
配置关系图
配置优先级
三、Dubbo调用模块概述
dubbo调用模块核心功能是发起一个远程方法的调用并顺利拿到返回结果,其体系组成如下:
- 透明代理:通过动态代理技术,屏蔽远程调用细节以提高编程友好性。
- 负载均衡:当有多个提供者是,如何选择哪个进行调用的负载算法。
- 容错机制:当服务调用失败时采取的策略
- 调用方式:支持同步调用、异步调用