Learn more about how to deploy ice services (i)

Source: Internet
Author: User
Tags stub

This series of articles will teach you how to deploy an ice service, and if you are reading this blog, I think you already know what ice (Internet Communications Engine) is and how to implement the ice service, and understand what an ice object is, Ice object identifiers, ice object adapters, ice service implementations servant, Ice communicator, and so on, of course, if you don't even know what ice is, I don't recommend that you read on.

Let's talk about the basic components of ice:

(1) Slice tool: Compile the interface defined by the slice language into code that is implemented in a variety of specific languages, which is part of the development environment

(2) Ice container: Icebox, ice Node, ice Registry, ice Grid, ice admin and other components, these are used to deploy and manage the ice service, you can choose several of them to use in your service.

(3) Ice Runtime: The set of APIs provided by the ICE implementation platform, different languages have different implementations, such as Java using the jar package, C + + uses the. so file, which needs to be called by the Ice service client and server

To achieve the underlying communication.


The development environment I used when I wrote this series of blogs was as follows:

Eclipse

Ice-3.3.1

jdk-1.6

CentOS release 5.11 (Final)


Here we implement a service that queries employee information to demonstrate how the ice service is deployed.


(a) We first create a Java project in Eclipse and create a folder named Slice under the project, then define the ice service interface using ICE's slice language and name the file Query.ice, and put the file in the Slice folder in the project

[["Java:package:com.yujie.ice"]]
Module info{

struct employeeinfo{
string name;
int age;
BOOL Isleave;
Double salary;
string remark;
};
	Interface queryemployee{
	employeeinfo query (EmployeeInfo msg);};

The Query.ice file defines the registration Com.yujie.ice and packet isolation with info and the last package name generated is com.yujie.ice.info.

The file defines a EMPLOYEEINFO structure as an employee's information structure, defining an interface Queryemployee, and we need to implement this interface in our own server-side code.

(ii) Using the Slice tool to generate specific code for the Java implementation of the interface defined by Query.ice, my eclipse has installed the ice compilation plug-in, and the Java Project can be compiled directly to generate the corresponding interface client and server stub code. If you do not have Slice2java compile plugin installed you can use similar Slice2java--output-dir. /src/main/java/query.ice Such commands generate code, provided that you have set up the ice corresponding environment variables.

The project directory after compilation is as follows:



(c) Implement the ice service-side code.

/**
  * Inherits the server stub code _queryemployeedisp class and implements the query interface
  * @author Yujie.wang * */Public
class Queryemployeeimpl extends _queryemployeedisp{

	@Override public
	employeeinfo query (EmployeeInfo msg, Current _ _current) {
		//TODO auto-generated method stub
		employeeinfo ei = new EmployeeInfo ();
		Ei.age =;
		Ei.name = Msg.name;
		Ei.isleave = false;
		Ei.salary = 2000.0;
		Ei.remark = "He is a good employee";
		return ei;
	}
}

We are here to implement a service that queries employee information and returns information about an employee.

Then we implement the code that launches the ice server, and the code looks like this:

/** * Implement start ice Service-side code * @author Yujie.wang * * */public class Queryemployeeserver {public static void main (String [] Arg
		s) {int state = 0;
		Ice.communicator Communicator = null;
			try {//Initialize the ice Communicator, you can use args to pass in the ice initialization parameters such as time-out, thread pool size, and so on communicator = Ice.Util.initialize (args); Create an adapter named Queryemployeeadapter and use the TCP Protocol service to deploy the service on the 10.4.30.81 machine by default 10006 listening Port Ice.objectadapter adapter =
			Communicator.createobjectadapterwithendpoints ("Queryemployeeadapter", "Default-p 10006");
			Create a server-side code implementation servant Queryemployeeimpl servant = new Queryemployeeimpl ();
			The servant is mapped to the Ice object identifier and added to the Ice object adapter Adapter.add (servant, Ice.Util.stringToIdentity ("Queryserver"));
			Activates the object adapter adapter.activate ();
			SYSTEM.OUT.PRINTLN ("Queryemployeeserver adapter activate");
		The service remains in the listening state before exiting Communicator.waitforshutdown ();
			} catch (Exception e) {//Todo:handle Exception state = 1;
		System.out.println (e); finally{if (communicator! = null) {Communicator.Destroy ();
	}} System.out.println ("state:" + state); }
}

(iv) Implementing client-side code

/** * Implement client Invoke Interface code * @author Yujie.wang * */public class Queryemployeeclient {public static void main (string[] args)
		{//TODO auto-generated method stub ice.communicator Communicator = null;
			try {//Initialize the ice Communicator, you can use args to pass in the ice initialization parameters such as time-out, thread pool size, and so on communicator = Ice.Util.initialize (args); The Ice object identifier of the incoming Remote Service unit Protocol default TCP host has served the listening port ice.objectprx op = Communicator.stringtoproxy ("Queryserver:default-h 10.4.3
			0.81-p 10006 ");
			Check that the Generic Client Agent OP is not the agent of the ice object associated with the Queryserver object identifier Queryemployeeprx QP = Queryemployeeprxhelper.checkedcast (OP);
			if (QP = = null) {throw new Exception ("QP = = null");
			}//construct incoming parameter employeeinfo ei = new EmployeeInfo ();
			Ei.name = "Yujie.wang";
			Call interface EmployeeInfo result = qp.query (EI);
			if (result = = null) {throw new Exception ("result = = null");
		}//Output server returns the result System.out.println (Result.remark);
		} catch (Exception e) {//Todo:handle Exception System.out.println (e); }
	}

}

(v) Service deployment

Of course you can run the main method of starting the ice service directly in eclipse, but in order to continue the following service deployment ideas, I'm going to make the code into a jar package and run the Main method with a simple shell script.

Next we compile the Java project, then the compiled class code through the JAR-CVF yujie-ice-test1.jar./* command to make a Yujie-ice-test1.jar package.

I wrote a very simple container on the server iceserver used to start the ice service, the directory structure of the container is as follows:


The bin directory has the following two files:

env.sh

#!/bin/sh
If [-z] $JAVA _home "], then
        java_home="/data/web/jdk "
fi
echo" java_home: $JAVA _home "
If [-Z "$SERVER _home"]; Then
        bin_dir= ' readlink-f '
        $ ' echo ' Bin_dir: $BIN _dir "
        base_dir= ' dirname" $BIN _dir "' Echo '
        base_ DIR: $BASE _dir "
        server_home=" ' CD $BASE _dir/. && pwd ' "
else 
        echo" Server_home is not EMPTY "
fi

echo "server_home: $SERVER _home"

starticeserver.sh

#!/bin/sh:
/env.sh

app_home= $SERVER _home/lib
app_mainclass= Com.yujie.ice.server.QueryEmployeeServer

classpath= $APP _home
echo $CLASSPATH for
i in "$APP _home"/*. Jar;do classpath= "$CLASSPATH": "$i";d one
echo $CLASSPATH
$JAVA _home/bin/java-xms1000m-xmx1000m-xmn500m-xx :P ERMSIZE=128M-XX:MAXPERMSIZE=256M-CP  $CLASSPATH  


The Lib directory below is the Ice Platform API jar package and our own service jar package.


Now we use the starticeserver.sh script to start the service-side code.

SH starticeserver.sh &



Finally, we run the client call code locally in the following output:





(vi) Summary of the deployment approach

Pros: Direct use of the service-side main function to start, did not introduce the ice other container components, the client and the server directly through TCP to establish a connection, is a very lightweight service.

Determine: The client directly writes dead endpoint information of the service, and its inflexible; server-side deployment method is simple and rough operation, unable to achieve load balancing, failure recovery


This type of deployment is a simple, crude demonstration that cannot be used as an industrial-grade deployment scenario at all.

In the next section we will implement another deployment scenario that will address some of the issues with this deployment approach.










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.