項目背景
最近在做公司項目的微服務(wù)改造,在dubbo和spring-cloud這兩個主流的微服務(wù)框架之前技術(shù)選型徘徊了好久,兩個框架在性能,設(shè)計,社區(qū)支持都非常地完美。然而我為什么要選擇自己去封裝ignite去實現(xiàn)RPC服務(wù)呢?
- 原因一:
公司缺乏專門的運維人員去維護(hù)第三方的中間件服務(wù),例如 Redis,Zookeeper,Rabbitmq等等的這些中間件。 - 原因二:
應(yīng)用本身要求的高并發(fā)不多,但是需要做高可用,引入dubbo和spring-cloud視乎有點小題大做的感覺 - 原因三:
使用dubbo和spring-cloud對現(xiàn)有項目的代碼改造工作量有點大,本身公司項目有部分已經(jīng)基于ignite做服務(wù)網(wǎng)格了 - 原因四:
工作這么久了,還是要嘗試封裝一下框架,所以這個框架是我的處女做,希望大家能夠批評和指正
項目概述
底層基于apache ignite,特點是可以做到不依賴外部中間件,實現(xiàn)RPC服務(wù)
,分布式緩存
,分布式計算
,分布式消息
等功能特性
框架也基于ignite的集群管理,實現(xiàn)了基于集群組的顆粒度的服務(wù)調(diào)用,即針對集群組的調(diào)用
- JDK和Spring boot版本
JDK版本為1.8
Spring boot 版本要求1.5.3
以上
框架說明
服務(wù)中啟動的 Spring boot 應(yīng)用同時啟動了ignite的server和client模式,注入到了Spring容器中
@Autowired
@Qualifier("igniteClient")
private Ignite igniteClient;
@Autowired
@Qualifier("igniteServer")
private Ignite igniteServer;
因此你可以無縫地使用框架沒有封裝的ignite功能,更多的ignite的功能,請參考中文官網(wǎng)
(https://www.ignite-service.cn/doc/java/)
功能概述
- RPC服務(wù)
- 分布式消息
- 分布式廣播
- 分布式計算
quick-start
構(gòu)建
git clone https://github.com/konglinghai123/ignite-spring-project.git
cd ignite-spring-boot-starter
mvn clean install
構(gòu)建一個基于ignite的 spring boot 項目
- 添加依賴:
<dependency>
<groupId>com.github.kong.spring.boot</groupId>
<artifactId>ignite-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>
在application-yml添加ignite的相關(guān)配置信息,樣例配置如下:
zookeeper 發(fā)現(xiàn)
#zookeeper發(fā)現(xiàn)
ignite-cluster:
name: hello_client_1 #節(jié)點名稱
role: client
des: 測試服務(wù)
zookeeperUrl: 192.168.56.100:2181
localAddress: 127.0.0.1
localPort: 47600
- 動態(tài)ip發(fā)現(xiàn)
ignite-cluster:
name: hello_server #節(jié)點名稱
role: server
des: 測試服務(wù)端
multicast-group: 224.0.1.111 #組播地址
localAddress: 127.0.0.1
localPort: 48600
- 為了開發(fā)方便,如果Spring boot Appliction 類的不在包名
com.github.kong
目錄下,接下來在Spring Boot Application的上添加@ComponentScan("com.github.kong.*")
,這樣idea可以通過看到一些Bean是否已經(jīng)注入了,當(dāng)然也可以不添加,框架也有寫掃描注入
@SpringBootApplication
@ComponentScan("com.github.kong.*")
public class HelloWorldServerApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldServerApplication.class);
}
}
RPC服務(wù)的創(chuàng)建與消費
發(fā)布服務(wù)基于ignite的RPC服務(wù)
- 編寫你的ignite服務(wù),需要添加要發(fā)布的服務(wù)實現(xiàn)上添加
@IgniteRpcService
注解,繼承IgniteService
. -
HelloWorld
是定義的接口
@Service
@IgniteRpcService(des = "這是一個例子")
public class HelloWorldService extends IgniteService implements HelloWorld {
}
-
@IgniteRpcService
注解的定義
/**
* 服務(wù)提供者注解
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface IgniteRpcService {
/**
* @return
*/
String version() default "1.0";
//接口描述
String des() default "";
//單個節(jié)點部署的實例數(shù)
int maxPerNodeCount() default 1;
//整個集群部署的最大實例數(shù),0:無限制
int total() default 0;
}
- 啟動你的Spring Boot應(yīng)用,觀察控制臺,可以看到ignite啟動相關(guān)信息.
調(diào)用已經(jīng)發(fā)布的RPC服務(wù)
- Spring boot 應(yīng)用配置同上,唯一不同的是,需要更改配置
#zookeeper發(fā)現(xiàn)
ignite-cluster:
name: hello_client_1 #節(jié)點名稱 (必須在集群中唯一)
- 通過
@IgniteRpcReference
注入需要使用的interface.
@Controller
public class HelloWorldController {
@IgniteRpcReference
private HelloWorld helloWorldService;
@RequestMapping("/helloworld")
@ResponseBody
public String test(){
return helloWorldService.sayHello("kong");
}
}
- 調(diào)用不同版本的RPC服務(wù)
@IgniteRpcReference(version = "1.1")
private HelloWorld helloWorldService;
-
@IgniteRpcReference
注解的定義
/**
* 網(wǎng)格服務(wù)注入注解
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface IgniteRpcReference {
String version() default "1.0";
//默認(rèn)使用負(fù)載均衡
boolean isLoadbalance() default true;
//默認(rèn)不設(shè)超時
long timeout() default 0;
}
分布式消息
分布式消息是基于內(nèi)存的消息訂閱系統(tǒng),如果需要持久化請使用外部的消息系統(tǒng)
定義話題消費者
@Service
@IgniteMessageListener(topic = "hello",isBroadcast = false)
public class HelloWorldMessage implements IgniteMessageRecevicer<String> {
@Override
public boolean apply(UUID uuid, MessageModel<String> messageModel) {
System.out.println(messageModel);
return true;
}
}
-
@IgniteMessageListener
注解的定義
/**
* 服務(wù)提供者注解
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface IgniteMessageListener {
//消息主題
String topic();
//消息描述
String des() default "";
//是否針對集群內(nèi)的所有節(jié)點(是否允許重復(fù)消費)
boolean isBroadcast() default true;
}
-
MessageModel
是一個消息封裝,發(fā)送消息時必須用它來發(fā)送
發(fā)送話題消息
@Controller
@RequestMapping("/message")
public class MessageController {
@Autowired
private IgniteMessageSender sender;
@RequestMapping("/sayHello")
@ResponseBody
public String test(){
sender.toRemote("hello", new MessageModel<>("1212"));
return "1212";
}
}
-
IgniteMessageSender
是框架注入的Bean,可以直接引用
<span id="BroadCast">分布式廣播</span>
分布式廣播是指:對集群組的所有節(jié)點發(fā)送消息,然后獲取所有節(jié)點返回的結(jié)果,原來是基于ignite的分布式閉包利用反射機制調(diào)用spring容器內(nèi)Bean的方法
發(fā)送一個分布式廣播
@Controller
@RequestMapping("/broadcast")
public class BroadcastController {
@Autowired
private BroadcastServiceExecutor broadcastServiceExecutor;
@RequestMapping("/sayHello")
@ResponseBody
public List<String> test(){
return (List<String>) broadcastServiceExecutor.broadcast("server", TestBroadService.class,"sayHello","12123");
}
}
BroadcastServiceExecutor
是框架注入的Bean,可以直接引用broadcast
方法提供3個方法定義
/**
* 向其他集群廣播
* @param targetRole 集群標(biāo)識
* @param targetClass api類
* @param methodName 方法名稱
* @param args 參數(shù)
* @return
*/
public List broadcast(String targetRole,Class targetClass,String methodName,Object... args){...}
/**
* 向遠(yuǎn)端集群廣播消息
* @param targetClass
* @param methodName
* @param args
* @return
*/
public List broadcastRemote(Class targetClass,String methodName,Object... args){...}
/**
* 向集群內(nèi)廣播消息
* @param targetClass
* @param methodName
* @param args
* @return
*/
public List broadcastLocal(Class targetClass,String methodName,Object... args){...}
分布式計算
分布式計算允許用戶執(zhí)行基于內(nèi)存的Map-Reduce任務(wù)
- 創(chuàng)建
Map-Reduce
任務(wù) ,需繼承ComputeTaskSplitAdapter
(import org.apache.ignite.compute.ComputeTaskSplitAdapter),泛型<T,R> - T:入?yún)ⅲ琑:返回類型
//字?jǐn)?shù)統(tǒng)計測試
@Service
public class MapExampleCharacterCountTask extends ComputeTaskSplitAdapter<List<String>,Integer> {
@Nullable
@Override
public Integer reduce(List<ComputeJobResult> results) throws IgniteException {
return results.stream().mapToInt(ComputeJobResult::<Integer>getData).sum();
}
@Override
protected Collection<? extends ComputeJob> split(int gridSize, List<String> arg) throws IgniteException {
LinkedList jobs = new LinkedList();
List<List<String>> list = CollectionUtils.split(arg,10000);
for(final List<String> words : list){
jobs.add(new ComputeJobAdapter() {
@Override
public Object execute() throws IgniteException {
int i = 0;
for(String s : words){
i = i + s.length();
}
return i;
}
});
}
return jobs;
}
}
- 執(zhí)行
Map-Reduce
任務(wù)
@Controller
@RequestMapping("/mr")
public class MRTestController {
@Autowired
private MapReduceTaskExecutor<List<String>,Integer> mapReduceTaskExecutor;
@RequestMapping("/test")
@ResponseBody
public Object test(){
try {
List<String> records = new ArrayList<>();
// 創(chuàng)建CSV讀對象
CsvReader csvReader = new CsvReader(new FileInputStream("D:\\data\\cs2.csv"), Charset.forName("GBK"));
while (csvReader.readRecord()){
// 讀一整行
records.add(csvReader.getRawRecord());
}
List<String> bigRecords = new ArrayList<>();
for(int i = 0; i < 5; i++){
bigRecords.addAll(records);
}
return mapReduceTaskExecutor.execute(MapExampleCharacterCountTask.class,bigRecords);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
-
MapReduceTaskExecutor
是框架注入的Bean,可以直接引用
集群管理api
框架注入了IgniteManager
這個bean,可以實現(xiàn)以下功能
public interface IgniteManager {
/**
* 獲取節(jié)點列表
*
* @return
*/
List<NodeInfo> list();
/**
* 獲取節(jié)點的詳細(xì)信息
*
* @param nodeId
* @return
*/
ClusterMetrics info(String nodeId);
/**
* 獲取微服務(wù)的基本信息
*
* @return
*/
List<ServiceInfo> servieInfos();
/**
* 集群消息信息
* @return
*/
List<MessageInfo> messagInfos();
}
- 使用
@Autowired
注入即可
@Controller
@RequestMapping("/admin")
public class AdminCotroller {
@Autowired
private IgniteManager igniteManager;
@RequestMapping("/nodes")
@ResponseBody
public List<NodeInfo> nodes(){
return igniteManager.list();
}
@RequestMapping("/nodeInfo/{id}")
@ResponseBody
public ClusterMetrics info(@PathVariable("id") String id){
return igniteManager.info(id);
}
@RequestMapping("/services")
@ResponseBody
public List<ServiceInfo> services(){
return igniteManager.servieInfos();
}
}