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;
}
}