本文将经过具体的远程服务发布与消费案例展现4种RPC远程调用方法.html
java.rmi
包, 方便开发者进行远程服务的部署与消费, 下面将经过具体案例进行讲解.UserService
接口.//将要发布的服务的接口
public interface UserService extends Remote {
public String helloRmi(String name) throws RemoteException;
}
复制代码
UserServiceImpl
实现类UserServiceImpl
除了实现UserService
接口外, 还要继承UnicastRemoteObject
类, 你能够理解为它是一个发布出去供他人调用的类, 当UserServiceImpl
实现了这个类后, UserServiceImpl
就能被发布出去供别人调用.//将要发布的服务的实现类
public class UserServiceImpl extends UnicastRemoteObject implements UserService {
public UserServiceImpl() throws RemoteException {
super();
}
public String helloRmi(String name) throws RemoteException {
return "hello " + name;
}
}
复制代码
public static void main(String[] args) {
try {
//完成远程服务的发布
LocateRegistry.createRegistry(8888);//将远程服务发布在本地的8888端口
String name = "rmi://localhost:8888/rmi";//发布的远程服务被访问的url
UserService userService = new UserServiceImpl();//建立一个提供具体服务的远程对象
Naming.bind(name, userService);//给远程服务绑定一个url
System.out.println("--- 已发布rmi远程服务 ---");
} catch (Exception e) {
e.printStackTrace();
}
}
复制代码
rmi-provider
项目种的UserService
接口与UserServiceImpl
实现类复制到本rmi-consumer
项目中.(这一步能够进行优化解耦, 咱们能够多建立一个rmi-resource
项目, 让rmi-provider
和rmi-consumer
共同依赖rmi-resource
项目, 而后把资源文件好比远程服务所用到的UserService
等放入rmi-resource
项目中)public static void main(String[] args) {
try {
//发布远程服务的访问url
String name = "rmi://localhost:8888/rmi";
//经过发布远程服务的url, 获取远程服务的代理对象
UserService userService = (UserService) Naming.lookup(name);
System.out.println("得到的远程服务的代理对象:" + userService.getClass().getName());
String result = userService.helloRmi("rmi");//拿到远程方法调用的结果
System.out.println("result: " + result);
}catch (Exception e) {
e.printStackTrace();
}
}
//最后输出
得到的远程服务的代理对象:com.sun.proxy.$Proxy0
result: hello rmi
复制代码
http + xml
进行通讯.UserService
及其实现类UserServiceImpl
.WebService
时须要对远程服务加上注解@WebService
@WebService
public interface UserService {
public String sayHello(String name);
}
@WebService
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "hello " + name + "~";
}
}
复制代码
rmi
差很少, 须要提供远程服务的访问地址和具体的远程服务实现类, 使用Endpoint
类的publish()
方法进行发布, 这都是JDK封装好的.public class WsProviderApp {
public static void main(String[] args) {
//发布的WebService的被访问地址
String address = "http://localhost:9999/ws";
//建立远程服务对象
UserService userService = new UserServiceImpl();
//发布服务
Endpoint.publish(address, userService);
System.out.println("远程服务已经发布...");
}
}
复制代码
rmi
不一样的是, WebService发布后, 调用者能够经过查看它的文档对远程服务发起调用.?wdsl
, 好比本案例中是http://localhost:9999/ws?wsdl
bin
目录下, 名字是wsimport
wsimport -keep -d C:\githubRepositories\shopping\ws-consumer\src\main\java -p com.shenghao.client http://localhost:9999/ws?wsdl
解释:
1. wsimport 是命令的名字
2. -keep 用于保留生成的类, 若是没有该指令会只生成class文件 3. -d 后面接项目中存放这些工具类的包, 填绝对路径 4. -p 填wdsl文档的地址 复制代码
5. 能够看到命令执行完后, 指定的包中出现一堆相关的类, 最直接调用到的类是UserServiceImplService
. 下面演示对远程方法进行调用.java
public static void main(String[] args) {
//建立服务类对象
UserServiceImplService service = new UserServiceImplService();
//得到远程服务的代理对象
UserServiceImpl userService = service.getUserServiceImplPort();
System.out.println(userService.getClass().getName());
//对远程服务对象的方法进行调用
String result = userService.sayHello("炭烧生蚝");
System.out.println(result);
}
//结果输出
com.sun.proxy.$Proxy32
hello 炭烧生蚝~
复制代码
<html>
标签, 而是json
字符串. 微信小程序先后端通讯也是这个原理.order-sys
的Maven项目, 指定打包为war
包.
<properties> <!-- spring 依赖 --> <spring.version>4.3.18.RELEASE</spring.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <jackson.version>2.9.0</jackson.version> </properties> <dependencies> <!-- jsp相关依赖 --> <!-- servlet依赖 --> <!-- jstl依赖 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <!-- springmvc依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> </dependencies> <build> <finalName>order</finalName> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/order</path> <port>7070</port> </configuration> </plugin> </plugins> </build></code></p> 复制代码
复制代码<properties> <!-- spring 依赖 --> <spring.version>4.3.18.RELEASE</spring.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <jackson.version>2.9.0</jackson.version> </properties> <dependencies> <!-- jsp相关依赖 --> <!-- servlet依赖 --> <!-- jstl依赖 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <!-- springmvc依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> </dependencies> <build> <finalName>order</finalName> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/order</path> <port>7070</port> </configuration> </plugin> </plugins> </build></code></p> 复制代码
2. 建立订单类git
public class Order {
private String id;
private Double total;
private String date;
//get / set ...
}
复制代码
- 对外提供服务, 发布时打包发布到
Tomcat
上
@Controller
public class OrderController {
/** * 接收http请求, 响应订单集合, 异步响应 * 将list集合序列化为json串响应 * @param uid * @return */
@RequestMapping("/loadOrderList2")
@ResponseBody
public List<Order> loadOrderList2(String uid){
System.out.println("uid: " + uid);
//模拟订单数据
Order o1 = new Order();
o1.setId("111");
o1.setTotal(333.33);
o1.setDate("2019-4-29");
Order o2 = new Order();
o2.setId("222");
o2.setTotal(444.44);
o2.setDate("2019-5-29");
Order o3 = new Order();
o3.setId("333");
o3.setTotal(555.55);
o3.setDate("2019-6-29");
List<Order> list = new ArrayList<>();
list.add(o1);
list.add(o2);
list.add(o3);
return list;
}
}
复制代码
远程服务消费者实现
- 在服务消费端使用
HttpClient
发送请求, 能够理解为模拟浏览器发送post/get请求. HttpClient
为咱们封装了拼接一个请求的细节, 使得发送一个请求变得容易.
public static void main(String[] args) throws IOException {
//发送远程的http请求的地址
String url = "http://localhost:7070/order/loadOrderList2";
//建立HttpClient对象
CloseableHttpClient client = HttpClients.createDefault();
//建立HttpPost对象, 发送post请求
HttpPost method = new HttpPost(url);
//封装发送到服务提供者的参数
NameValuePair id = new BasicNameValuePair("uid", "10001");
List<NameValuePair> params = new ArrayList<>();
params.add(id);
//封装请求体数据
method.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//发送具体的http请求
HttpResponse response = client.execute(method);
//得到服务提供者响应的具体数据
HttpEntity entity = response.getEntity();
//得到http的响应体
InputStream is = entity.getContent();
int len = 0;
char[] buf = new char[1024];
//使用字符流读
InputStreamReader reader = new InputStreamReader(is);
StringBuffer sb = new StringBuffer();
while((len = reader.read(buf)) != -1){
sb.append(String.valueOf(buf, 0, len));
}
System.out.println(sb);
//将响应回来的json字符串解析为Order集合
List<Order> list = JSON.parseArray(sb.toString(), Order.class);
for(Order o : list){
System.out.println(o.getId() + "\t" + o.getTotal() + "\t" + o.getDate());
}
}
复制代码
四. 经过spring提供的RestTemplate实现远程服务的生产与消费
- 经过一个红包系统和订单系统进行演示, 红包系统访问订单系统, 得到某个用户的订单信息, 派发红包.
- 订单系统继续沿用
HttpClient
中的订单系统, 经过访问loadOrderList2
方法能返回一个订单集合Json字符串.
远程服务消费者实现.
@Controller
public class RedController {
//注入由spring提供的RestTemplate对象
@Autowired
private RestTemplate restTemplate;
/** * 发送远程的http请求, 消费http服务 * 得到订单对象的集合 */
@RequestMapping("/loadOrderList3")
@ResponseBody
public List<ResponseEntity<Order[]>> loadOrderList3(String uid){
//发送远程http请求的url
String url = "http://localhost:7070/order/loadOrderList2";
//发送到远程服务的参数
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("uid", uid);
//经过RestTemplate对象发送post请求
ResponseEntity<Order[]> entitys = restTemplate.postForEntity(url, params, Order[].class);
//查看响应的状态码
System.out.println(entitys.getStatusCodeValue());
//查看响应头
HttpHeaders headMap = entitys.getHeaders();
for(Map.Entry<String, List<String>> m : headMap.entrySet()){
System.out.println(m.getKey() + ": " + m.getValue());
}
return Arrays.asList(entitys);
}
}
复制代码