微服務架構 - 微服務實戰



本章中,我們將構建一個微服務應用程式,它將使用不同的可用服務。我們都知道,微服務並不是構建應用程式的經濟有效方式,因為我們構建的每個服務本質上都是全棧的。在本地環境中構建微服務需要高階的系統配置,因為您需要四個伺服器例項保持執行,以便在某個時間點可以訪問它。為了構建我們的第一個微服務,我們將使用一些可用的 SOA 端點,並在我們的應用程式中使用它們。

系統配置和設定

在進一步進入構建階段之前,請相應地準備您的系統。您將需要一些公共網路服務。您可以輕鬆地透過谷歌搜尋找到這些服務。如果您想使用 SOAP 網路服務,那麼您將獲得一個 WSDL 檔案,然後您需要使用該檔案來使用特定的網路服務。對於 REST 服務,您只需要一個連結來使用它。在本例中,您將在一個應用程式中整合三個不同的網路服務:“SOAP”、“REST”和“自定義”。

應用程式架構

您將使用微服務實現計劃建立一個 Java 應用程式。您將建立一個自定義服務,此服務的輸出將作為其他服務的輸入。

以下是開發微服務應用程式的步驟。

步驟 1:建立 SOAP 服務客戶端 - 有許多免費的 Web API 可用於學習 Web 服務。“http://www.webservicex.net/”的 GeoIP 服務可用於本教程。WSDL 檔案位於其網站上的以下連結:“webservicex.net”。要從此 WSDL 檔案生成客戶端,您只需在終端中執行以下命令。

wsimport http://www.webservicex.net/geoipservice.asmx?WSDL

此命令將在名為“SEI”(服務端點介面)的資料夾下生成所有必需的客戶端檔案。

步驟 2:建立您的自定義 Web 服務 - 按照本教程前面階段提到的相同過程,構建一個名為“CustomRest”的基於 Maven 的 REST api。完成後,您將找到一個名為“MyResource.java”的類。繼續使用以下程式碼更新此類。

package com.tutorialspoint.customrest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("myresource")
public class MyResource {
   @GET
   @Produces(MediaType.TEXT_PLAIN)
   
   public String getIt() {
      return "IND|INDIA|27.7.65.215";
   }
}

一切完成後,繼續在伺服器上執行此應用程式。您應該在瀏覽器中看到以下輸出。

Web Service

這是一個 Web 伺服器,它在被呼叫時返回一個字串物件。這是一個輸入服務,它提供可被其他應用程式使用的輸入來生成記錄。

步驟 3:配置另一個 REST API - 在此步驟中,使用位於 services.groupkt.com 的另一個 Web 服務。呼叫時,這將返回一個 JSON 物件。

步驟 4:建立 JAVA 應用程式 - 透過選擇“新建專案”->“JAVA 專案”並點選“完成”來建立一個普通的 Java 應用程式,如下面的螢幕截圖所示。

JAVA Application

步驟 5:新增 SOAP 客戶端 - 在步驟 1 中,您已經為 SOAP 網路服務建立了客戶端檔案。繼續將這些客戶端檔案新增到當前專案中。成功新增客戶端檔案後,您的應用程式目錄將如下所示。

SOAP web service

步驟 6:建立您的主應用程式 - 建立您的主類,您將在其中使用所有這三個 Web 服務。右鍵單擊源專案並建立一個名為“MicroServiceInAction.java”的新類。接下來的任務是從這裡呼叫不同的 Web 服務。

步驟 7:呼叫您的自定義 Web 服務 - 為此,繼續新增以下程式碼集來實現呼叫您自己的服務。

try {
   url = new URL("https://:8080/CustomRest/webapi/myresource");
   conn = (HttpURLConnection) url.openConnection();
   conn.setRequestMethod("GET");
   conn.setRequestProperty("Accept", "application/json");
   
   if (conn.getResponseCode() != 200) {
      throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
   }
   
   BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream())));
   while ((output = br.readLine()) != null) {
      inputToOtherService = output;
   }
   conn.disconnect();

} catch (MalformedURLException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}

步驟 8:使用 SOAP 服務 - 您已經生成了客戶端檔案,但是您不知道應該在整個包中呼叫哪個方法?為此,您需要再次參考用於生成客戶端檔案的 WSDL。每個 WSDL 檔案都應該有一個“wsdl:service”標籤,搜尋此標籤。它應該是該 Web 服務的入口點。以下是此應用程式的服務端點。

WSDL Service

現在您需要在您的應用程式中實現此服務。以下是您需要實現 SOAP 網路服務的 Java 程式碼集。

GeoIPService newGeoIPService = new GeoIPService();
GeoIPServiceSoap newGeoIPServiceSoap = newGeoIPService.getGeoIPServiceSoap();
GeoIP newGeoIP = newGeoIPServiceSoap.getGeoIP(Ipaddress);  
// Ipaddress is output of our own web service.

System.out.println("Country Name from SOAP Webserivce ---"+newGeoIP.getCountryName());

步驟 9:使用 REST 網路服務 - 到目前為止,已經使用了兩個服務。在此步驟中,將使用自定義 URL 的另一個 REST 網路服務以及您的自定義 Web 服務。使用以下程式碼集來做到這一點。

String url1="http://services.groupkt.com/country/get/iso3code/";//customizing the Url
url1 = url1.concat(countryCode);

try {
   URL url = new URL(url1);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setRequestMethod("GET");
   conn.setRequestProperty("Accept", "application/json");
   
   if (conn.getResponseCode() != 200) {
      throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
   }
   
   BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream())));
   while ((output = br.readLine()) != null) {
      System.out.println(output);
   }
   conn.disconnect();

} catch (MalformedURLException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}

步驟 10:使用所有服務 - 假設您的“CustomRest”Web 服務正在執行並且您已連線到網際網路,如果一切順利完成,那麼以下應該是您的合併主類。

package microserviceinaction;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;

import net.webservicex.GeoIP;
import net.webservicex.GeoIPService;
import net.webservicex.GeoIPServiceSoap;

public class MicroServiceInAction {
   static URL url;
   static HttpURLConnection conn;
   static String output;
   static String inputToOtherService;
   static String countryCode;
   static String ipAddress;
   static String CountryName;
   public static void main(String[] args) {
      //consuming of your own web service
      try {
         url = new URL("https://:8080/CustomRest/webapi/myresource");
         conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         conn.setRequestProperty("Accept", "application/json");
         
         if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
         }
         
         BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));
         while ((output = br.readLine()) != null) {
            inputToOtherService = output;
         }
         conn.disconnect();
      
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      
      //Fetching IP address from the String and other information
      StringTokenizer st = new StringTokenizer(inputToOtherService);
      countryCode = st.nextToken("|");
      CountryName = st.nextToken("|");
      ipAddress = st.nextToken("|");
      
      // Call to SOAP web service with output of your web service--- 
      // getting the location of our given IP address
      String Ipaddress = ipAddress;
      GeoIPService newGeoIPService = new GeoIPService();
      GeoIPServiceSoap newGeoIPServiceSoap = newGeoIPService.getGeoIPServiceSoap();
      GeoIP newGeoIP = newGeoIPServiceSoap.getGeoIP(Ipaddress);
      System.out.println("Country Name from SOAP Webservice ---"+newGeoIP.getCountryName());
      
      // Call to REST API --to get all the details of our country
      String url1 = "http://services.groupkt.com/country/get/iso3code/"; //customizing the Url
      url1 = url1.concat(countryCode);
      
      try {
         URL url = new URL(url1);
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         conn.setRequestProperty("Accept", "application/json");
			
         if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
         }
      
         BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));
         while ((output = br.readLine()) != null) {
            System.out.println(output);
         }
      
         conn.disconnect();
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

執行此檔案後,您將在控制檯中看到以下輸出。您已成功開發您的第一個微服務應用程式。

Output in The Console
廣告