在学习用dubbo进行分布式远程调用之前,先来看一个简单的有关访问网络上的天气预报接口的远程调用实例。
1.先创建一个java项目,创建过程这里不详细讲述,只附上一张截图供入门级选手参考:
2.创建好一个java项目后,接下来就是创建一个Weather实体类(这里可以使用lombok的@Data注解,就可以不用自己手写get和set还有toString 这些代码了):
public class Weather {
//城市
private String city;
//温度
private String temp;
//湿度
private String sd;
//天气概述
private String desc;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getSd() {
return sd;
}
public void setSd(String sd) {
this.sd = sd;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "天气信息{" +
"城市='" + city + '\'' +
", 温度='" + temp + '\'' +
", 湿度='" + sd + '\'' +
", 天气描述='" + desc + '\'' +
'}';
}
}
3.接下来就是WeatherService和实现类的创建,哦,对了,远程调用天气预报接口的代码部分因为是可以提取出来的,只需要在WeatherServiceImpl中写业务代码就好了,所以我使用了动态代理(很简单的,别怕哈),接下来就是代码了。
WeatherService:
public interface WeatherService {
/**
* 查询天气
* @param cityCode
* @return
*/
Weather queryWeather(String cityCode);
}
WeatherServiceProxy(这个是动态代理的代码):
public class WeatherServiceProxy implements InvocationHandler {
//目标对象
private Object target = null;
/**
* 定义获取代理对象
* @param target
* @return
*/
public Object getProxy(Object target){
this.target = target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//获取城市代码
String cityCode = (String) args[0];
Weather wea = new Weather();
//远程调用的地址
String remote_url = "http://www.weather.com.cn/data/sk/"+cityCode+".html";
try{
//使用URL表示地址
URL url = new URL(remote_url);
//获取url的连接对象
URLConnection urlconn = url.openConnection();
//转为HttpURLConnection
HttpURLConnection http_conn = (HttpURLConnection) urlconn;
//设置请求的方式
http_conn.setRequestMethod("GET");
//连接远程地址
http_conn.connect();
//判断连接的结果
if (http_conn.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStream in = http_conn.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
String line = bin.readLine();
if (line != null){
Object[] newargs = new Object[]{line};
//调用目标方法
wea = (Weather) method.invoke(target,newargs);
}
}
}catch (IOException e){
e.printStackTrace();
}
return wea;
}
WeatherServiceImpl(对了,在这部分代码中用到了jackson,就是用来通过序列化和反序列化操控json串的,用到的jar包有jackson-annotations-2.0.0.jar、jackson-core-2.0.0.jar和jackson-databind-2.0.0.jar。这些jar包网上是可以找到的,所以在这不给链接了,你要的话,我也不给,就是不给):
public class WeatherServiceImpl implements WeatherService {
/**
* 查询天气
* @param info
* @return
*/
@Override
public Weather queryWeather(String info) {
Weather weather = new Weather();
try{
if (info != null){
ObjectMapper objectMapper = new ObjectMapper();
//反序列化操作
Map map = objectMapper.readValue(info,Map.class);
Map<String,String> weamap = (Map<String, String>) map.get("weatherinfo");
weather.setCity(weamap.get("city"));
weather.setTemp(weamap.get("temp"));
weather.setSd(weamap.get("SD"));
weather.setDesc("0".equals(weamap.get("rain"))?"晴":"有雨");
}
}catch (IOException e){
e.printStackTrace();
}
return weather;
}
4.最后就是测试的代码了,在这附上查询天气的接口(http://www.weather.com.cn):
public class WeatherTest {
public static void main(String[] args){
//获取目标对象
WeatherService weatherService = new WeatherServiceImpl();
//获取代理对象
WeatherServiceProxy weatherServiceProxy = new WeatherServiceProxy();
WeatherService weatherService1 = (WeatherService) weatherServiceProxy.getProxy(weatherService);
Weather weather = weatherService1.queryWeather("101110101");
System.out.println("天气预报:" + weather.toString());
}
}
这篇博客讲解的东西很少,只是一个简单的远程调用的例子,这个是我学习dubbo前的小甜点,就是告诉大家远程调用的简单代码怎么写,至于什么是远程调用(RPC)?,这个。。。不告诉你。