RPC框架:
RPC是遠程過程調用(Remote Procedure Call)的縮寫形式。SAP系統RPC調用的原理其實很簡單,有一些類似於三層構架的C/S系統,第三方的客戶程序通過接口調用SAP內部的標準或自定義函數,獲得函數返回的數據進行處理後顯示或打印
RPC 是一種技術思想而非一種規範或協議,常見 RPC 技術和框架有:
- 應用級的服務框架:阿里的 Dubbo/Dubbox、Google gRPC、Spring Boot/Spring Cloud。
- 遠程通信協議:RMI、Socket、SOAP(HTTP XML)、REST(HTTP JSON)。
- 通信框架:MINA 和 Netty。
Dubbo admin
github : https://github.com/apache/dubbo-ops
springboot 簡易版用dubbo調用
生產者
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.chinaoly</groupId>
<version>1.0.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<artifactId>producer</artifactId>
<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>com.chinaoly</groupId>
<artifactId>baseCore</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
</project>
application.properties
dubbo.application.name=producer
dubbo.registry.address=zookeeper://192.168.52.34:2181?backup=192.168.52.33:2181,192.168.52.35:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
啓動類
@EnableDubbo //基於註解的dubbo功能
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(ProducerApplication.class);
}
}
業務
package com.chinaoly.service.Impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.chinaoly.bean.UserAddress;
import com.chinaoly.service.UserService;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Service //使用的是dubbo的Service
@Component
public class UserServiceImpl implements UserService {
public List<UserAddress> getUserAddressList(String userId) {
UserAddress u1 = new UserAddress(1,"北京。。。3樓","1","李老師","010-28292929","");
UserAddress u2 = new UserAddress(2,"深圳。。。3層","1","王老師","010-36228293","");
return Arrays.asList(u1,u2);
}
}
消費者
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.chinaoly</groupId>
<version>1.0.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.chinaoly</groupId>
<artifactId>baseCore</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
</project>
application.properties
server.port=20880
dubbo.application.name=consumer
dubbo.registry.address=zookeeper://192.168.52.34:2181?backup=192.168.52.33:2181,192.168.52.35:2181
啓動類
package com.chinaoly;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.chinaoly.service.OrderService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class);
}
}
業務:
package com.chinaoly.controller;
import com.chinaoly.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class OrderController {
@Autowired
private OrderService orderService;
@ResponseBody
@RequestMapping("/initOrder")
public Object getOrderInfo(String userId){
return orderService.initOrder(userId);
}
}
// 還有一個OrderService接口類,放在dubbo的api項目中
package com.chinaoly.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.chinaoly.bean.UserAddress;
import com.chinaoly.service.OrderService;
import com.chinaoly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
// @Autowired
@Reference
private UserService userService;
public Object initOrder(String userId) {
List<UserAddress> addressList = userService.getUserAddressList(userId);
for (UserAddress u : addressList){
System.out.println(u.getUserAddress());
}
return addressList;
}
}