微服务架构 - MSA 实践
在本章中,我们将构建一个微服务应用程序,该应用程序将使用不同的可用服务。我们都知道,微服务并不是一种经济有效的构建应用程序的方式,因为我们构建的每个服务本质上都是全栈的。在本地环境中构建微服务需要高端系统配置,因为您需要有四个服务器实例来保持运行,以便可以在某个时间点使用它。为了构建我们的第一个微服务,我们将使用一些可用的 SOA 端点,并在我们的应用程序中使用相同的端点。
系统配置和设置
在进一步进入构建阶段之前,请相应地准备您的系统。您将需要一些公共网络服务。你可以很容易地用谷歌搜索这个。如果您想使用 SOAP Web 服务,那么您将获得一个 WSDL 文件,然后您需要从那里使用特定的 Web 服务。对于 REST 服务,您只需要一个链接即可使用该服务。在此示例中,您将在一个应用程序中加入三种不同的 Web 服务“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服务器,一旦调用就会返回一个字符串对象。这是输入服务,提供可供其他应用程序使用以生成记录的输入。
步骤 3:配置另一个 Rest API - 在此步骤中,使用services.groupkt.com 上提供的另一个 Web 服务。调用时将返回一个 JSON 对象。
步骤 4:创建 JAVA 应用程序- 通过选择“新建项目”->“JAVA 项目”创建一个普通的 Java 应用程序,然后单击“完成”,如下面的屏幕截图所示。
步骤 5:添加 SOAP 客户端- 在步骤 1 中,您已经为 SOAP Web 服务创建了客户端文件。继续将这些客户端文件添加到您当前的项目中。成功添加客户端文件后,您的应用程序目录将如下所示。
第 6 步:创建主应用程序- 创建主类,您将在其中使用所有这三个 Web 服务。右键单击源项目并创建一个名为“MicroServiceInAction.java”的新类。下一个任务是从此调用不同的 Web 服务。
步骤 7:调用您的自定义 Web 服务- 为此,请继续添加以下代码集来实现调用您自己的服务。
try {
url = new URL("http://localhost: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 服务的入口点。以下是该应用程序的服务端点。
现在您需要在您的应用程序中实现此服务。以下是实现 SOAP Web 服务所需的一组 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 Web 服务- 到目前为止,其中两项服务已被使用。在此步骤中,将在您的自定义 Web 服务的帮助下使用另一个具有自定义 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”网络服务正在运行并且您已连接到互联网,如果一切都成功完成,那么以下应该是您的综合主类。
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("http://localhost: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();
}
}
}
运行此文件后,您将在控制台中看到以下输出。您已经成功开发了您的第一个微服务应用程序。
