最近在學習dubbo框架,dubbo是一個遠程RPC調用框架,提供了很多角色,如下所示:
節(jié)點角色說明
| 節(jié)點 | 角色說明 |
|---|---|
Provider |
暴露服務的服務提供方 |
Consumer |
調用遠程服務的服務消費方 |
Registry |
服務注冊與發(fā)現的注冊中心 |
Monitor |
統(tǒng)計服務的調用次數和調用時間的監(jiān)控中心 |
Container |
服務運行容器 |
調用關系說明
- 服務容器負責啟動,加載,運行服務提供者。
- 服務提供者在啟動時,向注冊中心注冊自己提供的服務。
- 服務消費者在啟動時,向注冊中心訂閱自己所需的服務。
- 注冊中心返回服務提供者地址列表給消費者,如果有變更,注冊中心將基于長連接推送變更數據給消費者。
- 服務消費者,從提供者地址列表中,基于軟負載均衡算法,選一臺提供者進行調用,如果調用失敗,再選另一臺調用。
- 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發(fā)送一次統(tǒng)計數據到監(jiān)控中心。
本文采用zookeeper作為注冊中心(版本3.6.1)
服務提供方實現如下:
- maven依賴如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
- 提供dubbo遠程調用的API
package com.huang.dubbo.api;
/**
* @Author: huangyichun
* @Date: 2021/3/30
*/
public interface GreetingsService {
String sayHi(String name);
}
package com.huang.dubbo.api;
import org.apache.dubbo.config.annotation.DubboService;
/**
* @Author: huangyichun
* @Date: 2021/3/30
*/
@DubboService
public class GreetingsServiceImpl implements GreetingsService {
@Override
public String sayHi(String name) {
return "hi, " + name;
}
}
- dubbo配置
# dubbo-provider.properties
dubbo.application.name=annotation-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
- 加載配置文件
package com.huang.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@EnableDubbo(scanBasePackages = "com.huang.dubbo.api")
@PropertySource("classpath:/dubbo-provider.properties")
public class DubboApplication {
public static void main(String[] args) {
SpringApplication.run(DubboApplication.class, args);
}
}
消費端實現:
- dubbo配置
<?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="consumer-of-helloworld-app" />
<!-- 使用multicast廣播注冊中心暴露發(fā)現服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
<dubbo:reference id="greetingsService" interface="com.huang.dubbo.api.GreetingsService" check="true"/>
</beans>
- 測試類
package com.huang.dubbo;
import com.huang.dubbo.api.GreetingsService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author: huangyichun
* @Date: 2021/3/30
*/
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
context.start();
GreetingsService demoService = (GreetingsService)context.getBean("greetingsService"); // 獲取遠程服務代理
String hello = demoService.sayHi("world ------------------------"); // 執(zhí)行遠程方法
System.out.println( hello );
}
}
輸出結果如下:
hi, world ------------------------
查看zookeeper節(jié)點信息如下:
ls /dubbo/com.huang.dubbo.api.GreetingsService/providers
[dubbo%3A%2F%2F10.12.214.93%3A20880%2Fcom.huang.dubbo.api.GreetingsService%3Fanyhost%3Dtrue%26application%3Dannotation-provider%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dtrue%26generic%3Dfalse%26interface%3Dcom.huang.dubbo.api.GreetingsService%26metadata-type%3Dremote%26methods%3DsayHi%26pid%3D10258%26release%3D2.7.9%26side%3Dprovider%26timestamp%3D1617332790089]