📅  最后修改于: 2023-12-03 15:20:47.342000             🧑  作者: Mango
Universal Description, Discovery, and Integration (UDDI)是一种用于在 Web 上发布和查找 Web 服务的规范。UDDI 使得 Web 服务可以通过互联网被普遍查找和访问,从而增强了 Web 服务的互操作性。本文将为程序员介绍 UDDI 的使用示例。
要注册一个 Web 服务,需要以下步骤:
创建服务描述:要将 Web 服务提供给其他人使用,需要将这项服务描述出来。该服务通常包含服务名称、服务描述、服务提供者信息、服务操作编排等信息。通常可以使用 WSDL(Web Services Description Language)来定义 Web 服务。
使用 UDDI 注册服务:要将 Web 服务注册到一个 UDDI 服务中。可以使用 UDDI 对服务进行分组和分类,并使用描述性信息描述服务。例如,下面是如何使用 UDDI 源代码将服务注册到 UDDI 中的示例:
import org.apache.juddi.v3.client.UDDIConstants;
import org.apache.juddi.v3.client.UDDIProxy;
import org.uddi.api_v3.*;
import javax.xml.ws.Holder;
public class RegisterService {
public static void main(String[] args) throws Exception {
UDDIProxy uddi = new UDDIProxy();
String businessName="MyBusiness";
String serviceName="MyService";
String wsdlURL="http://localhost:8080/MyService?wsdl";
uddi.setInquiryURL("http://localhost:8080/juddiv3/services/inquiry");
uddi.setPublishURL("http://localhost:8080/juddiv3/services/publish");
uddi.setSecurityUrl("http://localhost:8080/juddiv3/services/security");
BusinessEntity myBusEntity = new BusinessEntity();
Name name = new Name();
Description desc = new Description();
name.setValue(businessName);
desc.setValue("My business entity");
BindingTemplates bindingTemplates = new BindingTemplates();
BindingTemplate bindingTemplate = new BindingTemplate();
AccessPoint accessPoint = new AccessPoint();
accessPoint.setUseType(AccessPointType.WSDL_DEPLOYMENT.toString());
accessPoint.setValue(wsdlURL);
bindingTemplate.setAccessPoint(accessPoint);
bindingTemplate.setBindingKey("MyBinding");
CategoryBag categoryBag = new CategoryBag();
KeyedReference kr1 = new KeyedReference();
kr1.setTModelKey(UDDIConstants.WSDL_SPECIFIC_NAMESPACE);
kr1.setKeyName("uddi-org:wsdl");
kr1.setKeyValue("http://localhost:8080/MyService?wsdl");
categoryBag.getKeyedReference().add(kr1);
bindingTemplate.setCategoryBag(categoryBag);
bindingTemplates.getBindingTemplate().add(bindingTemplate);
myBusEntity.getName().add(name);
myBusEntity.getDescription().add(desc);
myBusEntity.setBindingTemplates(bindingTemplates);
BusinessList findBusiness = uddi.findBusiness("%");
BusinessInfos businessInfos = findBusiness.getBusinessInfos();
BusinessInfo businessInfo = businessInfos.getBusinessInfo().get(0);
BusinessDetail businessDetail = uddi.getBusinessDetail(businessInfo.getBusinessKey());
BusinessEntity businessEntity = businessDetail.getBusinessEntity().get(0);
SaveBusiness sb = new SaveBusiness();
sb.getBusinessEntity().add(myBusEntity);
Holder<AuthInfo> authInfo = new Holder<AuthInfo>(uddi.getAuthToken("username", "password"));
uddi.saveBusiness(sb.getBusinessEntity(), authInfo);
BusinessList findBU = uddi.findBusiness(businessName, null, null, null, null, null, null, null, null);
BusinessInfos businessInfoss = findBU.getBusinessInfos();
BusinessInfo businessInfossz = businessInfoss.getBusinessInfo().get(0);
// Now we have a business, let's make a service
BusinessService myService = new BusinessService();
myService.setBusinessKey(businessInfossz.getBusinessKey());
name.setValue(serviceName);
desc.setValue("My first service for the business");
bindingTemplates.getBindingTemplate().clear();
AccessPoint accessPoints = new AccessPoint();
accessPoints.setUseType(AccessPointType.WSDL_DEPLOYMENT.toString());
accessPoints.setValue(wsdlURL);
BindingTemplate bindingTemplates1 = new BindingTemplate();
bindingTemplates1.setAccessPoint(accessPoints);
bindingTemplates1.setBindingKey("MyBinding2");
bindingTemplates1.setDescription(desc);
bindingTemplates1.setCategoryBag(categoryBag);
bindingTemplates.getBindingTemplate().add(bindingTemplates1);
myService.getName().add(name);
myService.getDescription().add(desc);
myService.setBindingTemplates(bindingTemplates);
SaveService service = new SaveService(myService);
authInfo = new Holder<AuthInfo>(uddi.getAuthToken("username", "password"));
uddi.saveService(businessInfossz.getBusinessKey(), service, authInfo);
}
}
要查找 Web 服务,需要以下步骤:
import java.net.URISyntaxException;
import org.apache.juddi.v3.client.UDDIProxy;
import org.uddi.api_v3.BusinessList;
import org.uddi.api_v3.BusinessService;
import org.uddi.api_v3.ServiceList;
import org.uddi.api_v3.ServiceInfos;
import org.uddi.api_v3.BusinessInfos;
import org.uddi.api_v3.BindingTemplates;
public class FindService {
public static void main(String[] args) throws URISyntaxException {
UDDIProxy uddi = new UDDIProxy();
uddi.setInquiryURL("http://localhost:8080/juddiv3/services/inquiry");
uddi.setPublishURL("http://localhost:8080/juddiv3/services/publish");
uddi.setSecurityUrl("http://localhost:8080/juddiv3/services/security");
BusinessList findBusiness = uddi.findBusiness("%");
BusinessInfos businessInfos = findBusiness.getBusinessInfos();
BusinessService myService = null;
for (int i=0;i<businessInfos.getBusinessInfo().size();i++){
BusinessInfos theBusinessInfo = uddi.getBusinessDetail(businessInfos.getBusinessInfo().get(i).getBusinessKey());
ServiceList serviceList = theBusinessInfo.getServiceInfos();
ServiceInfos theList = serviceList.getServiceInfo();
for (int j=0;j<theList.size();j++){
myService = theList.get(j).getBusinessService();
BindingTemplates theBindingTemplates = myService.getBindingTemplates();
System.out.println("The service: "+ myService.getServiceKey() + " has "+ theBindingTemplates.getBindingTemplate().size() +" binding templates");
}
}
}
}
UDDI 是一种用于在 Web 上发布和查找 Web 服务的规范,它使 Web 服务可以通过互联网被普遍查找和访问,从而增强了 Web 服务的互操作性。通过以上示例,程序员可以掌握如何使用 UDDI 注册和查找服务。