Dubbo的學習總結(一)——遠端呼叫
阿新 • • 發佈:2018-11-05
在學習用dubbo進行分散式遠端呼叫之前,先來看一個簡單的有關訪問網路上的天氣預報介面的遠端呼叫例項。
1.先建立一個java專案,建立過程這裡不詳細講述,只附上一張截圖供入門級選手參考:
2.建立好一個java專案後,接下來就是建立一個Weather實體類(這裡可以使用lombok的@Data註解,就可以不用自己手寫get和set還有toString 這些程式碼了):
public class Weather { //城市 private String city; //溫度 private String temp; //溼度 privateString 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)?,這個。。。不告訴你。