一碳抄、Dubbo 快速入門
Dubbo核心功能解釋
dubbo 阿里開源的一個(gè)SOA服務(wù)治理框架涣觉,從目前來看把它稱作是一個(gè)RPC遠(yuǎn)程調(diào)用框架更為貼切灯荧。單從RPC框架來說海铆,功能較完善,支持多種傳輸和序列化方案踪少。所以想必大家已經(jīng)知道他的核心功能了:就是遠(yuǎn)程調(diào)用塘安。
Java Dubbo的遠(yuǎn)程調(diào)用
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.4.1</version>
</dependency>
<!--注冊(cè)中心為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>
服務(wù)端
public class DubboServer {
public static void main(String[] args) throws IOException {
// 開始 暴露 UserService 服務(wù)
// application
// protocol -dubbo 協(xié)議
// 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("服務(wù)已暴露");
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包的時(shí)候注意dubbo版本問題,新版本引用的是apache下面的jar包援奢。
public class DubboClient {
public static void main(String[] args) throws IOException {
RegistryConfig registryConfig = new RegistryConfig("zookeeper://192.168.0.147:2181"); // 虛擬的注冊(cè)中心 局域網(wǎng)
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的遠(yuǎn)程調(diào)用
共用接口
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 是否要檢查注冊(cè)中心可用 默認(rèn)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>
服務(wù)端
public class SpringServer {
public static void main(String[] args) throws IOException {
new ClassPathXmlApplicationContext("provide.xml");
System.out.println("服務(wù)已暴露");
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的遠(yuǎn)程調(diào)用
共用接口
public interface UserService {
User getUser(Integer id);
}
服務(wù)端
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("服務(wù)已開啟");
}
}
@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常規(guī)配置說明
標(biāo)簽 | 用途 | 解釋 |
---|---|---|
<dubbo:application/> | 公共 | 用于配置當(dāng)前應(yīng)用信息,不管該應(yīng)用是提供者還是消費(fèi)者 |
<dubbo:registry/> | 公共 | 用于配置連接注冊(cè)中心相關(guān)信息 |
<dubbo:protocol/> | 服務(wù) | 用于配置提供服務(wù)的協(xié)議信息集漾,協(xié)議由提供方指定切黔,消費(fèi)方被動(dòng)接受 |
<dubbo:service/> | 服務(wù) | 用于暴露一個(gè)服務(wù),定義服務(wù)的元信息具篇,一個(gè)服務(wù)可以用多個(gè)協(xié)議暴露纬霞,一個(gè)服務(wù)也可以注冊(cè)到多個(gè)注冊(cè)中心 |
<dubbo:provider/> | 服務(wù) | 當(dāng) ProtocolConfig 和 ServiceConfig 某屬性沒有配置時(shí),采用此缺省值驱显,可選 |
<dubbo:consumer/> | 引用 | 當(dāng) ReferenceConfig 某屬性沒有配置時(shí)诗芜,采用此缺省值,可選 |
<dubbo:reference/> | 引用 | 用于創(chuàng)建一個(gè)遠(yuǎn)程服務(wù)代理埃疫,一個(gè)引用可以指向多個(gè)注冊(cè)中心 |
<dubbo:method/> | 公共 | 用于 ServiceConfig 和 ReferenceConfig 指定方法級(jí)的配置信息 |
<dubbo:argument/> | 公共 | 用于指定方法參數(shù)配置 |
配置關(guān)系圖
配置優(yōu)先級(jí)
三伏恐、Dubbo調(diào)用模塊概述
dubbo調(diào)用模塊核心功能是發(fā)起一個(gè)遠(yuǎn)程方法的調(diào)用并順利拿到返回結(jié)果,其體系組成如下:
- 透明代理:通過動(dòng)態(tài)代理技術(shù)熔恢,屏蔽遠(yuǎn)程調(diào)用細(xì)節(jié)以提高編程友好性脐湾。
- 負(fù)載均衡:當(dāng)有多個(gè)提供者是,如何選擇哪個(gè)進(jìn)行調(diào)用的負(fù)載算法叙淌。
- 容錯(cuò)機(jī)制:當(dāng)服務(wù)調(diào)用失敗時(shí)采取的策略
- 調(diào)用方式:支持同步調(diào)用秤掌、異步調(diào)用