【Ice】【04】新建maven項目user-service 方式一

參考文檔

https://blog.csdn.net/u011784767/article/details/74339123

1.新建maven項目user-service

2.pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.yinzhen.demo.ice</groupId>
	<artifactId>user-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>user-service</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.7</java.version>
		<ice.version>3.6.4</ice.version>
		<org.springframework-version>4.2.6.RELEASE</org.springframework-version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>com.yinzhen.demo.ice</groupId>
			<artifactId>user-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<dependency>
			<groupId>com.zeroc</groupId>
			<artifactId>ice</artifactId>
			<version>${ice.version}</version>
		</dependency>
		<dependency>
			<groupId>com.zeroc</groupId>
			<artifactId>icebox</artifactId>
			<version>${ice.version}</version>
		</dependency>



		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.25</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core logback-core:提供了LogBack的核心功能,是另外兩個組件的基礎 -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.2.3</version>
		</dependency>

		<!-- 實現了Slf4j的API,所以當想配合Slf4j使用時,需要引入logback-classic -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.3</version>
		</dependency>

		<!-- 爲了集成Servlet環境而準備的,可提供HTTP-access的日誌接口 -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-access</artifactId>
			<version>1.2.3</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>


	<build>
		<plugins>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<testExcludes>true</testExcludes>
					<source>${java.version}</source>
					<target>${java.version}</target>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>


			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>package</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<encoding>UTF-8</encoding>
							<outputDirectory>
								${project.build.directory}
							</outputDirectory>
							<resources>
								<resource>
									<!--項目中的路徑 -->
									<directory>src/main/resources/</directory>
								</resource>
							</resources>
						</configuration>
					</execution>
				</executions>
			</plugin>


			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>2.4.2</version>
				<configuration>
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<filters>
								<filter>
									<artifact>*:*</artifact>
									<excludes>
										<exclude>META-INF/*.SF</exclude>
										<exclude>META-INF/*.DSA</exclude>
										<exclude>META-INF/*.RSA</exclude>
										<exclude>*.xml</exclude>
										<exclude>*.jpg</exclude>
										<exclude>*.jks</exclude>
										<exclude>*.properties</exclude>
										<exclude>*.pem</exclude>
										<exclude>*.keystore</exclude>
										<exclude>*.pfx</exclude>
										<exclude>*.cer</exclude>
									</excludes>
								</filter>
							</filters>
							<transformers>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.handlers</resource>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.schemas</resource>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>*.*</resource>
								</transformer>
							</transformers>
							<shadedArtifactAttached>true</shadedArtifactAttached>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>



</project>

3.新建實現類

package com.yinzhen.demo.ice.userservice;

import com.yinzhen.demo.ice.user.UserInfo;
import com.yinzhen.demo.ice.user._UserServiceDisp;

import Ice.Current;

public class UserServiceImpl extends _UserServiceDisp {

	private static final long serialVersionUID = -7494659905336272682L;

	@Override
	public UserInfo getUserInfoById(String id, Current __current) {
		UserInfo userInfo = new UserInfo("id","銀真",18,false,200000,"test");
		return userInfo;
	}

}

4.新建UserServiceServer

package com.yinzhen.demo.ice.userservice;

public class UserServiceServer {
	
	public static void main(String[] args) {
		int state = 0;
		Ice.Communicator communicator = null;
		try {
			//初始化ice通信器communicator,可以使用args傳入一下ice初始化的參數如超時時間,線程池大小等
			communicator = Ice.Util.initialize(args);
			//創建一個名爲UserServiceAdapter的適配器並且默認使用tcp協議  服務部署在192.168.1.25機器上 服務開啓10006監聽端口
			Ice.ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints("UserServiceAdapter","default -p 10006");
			// 創建服務端代碼實現servant
			UserServiceImpl servant = new UserServiceImpl();
			// 將servant與ice對象標識符建立映射關係,並添加到ice對象適配器中
			adapter.add(servant, Ice.Util.stringToIdentity("UserService"));
			// 激活對象適配器
			adapter.activate();
			System.out.println("UserService adapter activate");
			// 服務在退出之前一直保持監聽狀態
			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);
	}

}

5.創建/opt/ice_project/app/userservice/lib /opt/ice_project/app/userservice/bin

mkdir -p /opt/ice_project/app/userservice/lib  /opt/ice_project/app/userservice/bin

6.打包把jar包user-service-1.0-SNAPSHOT-shaded.jar放到/opt/ice_project/app/userservice/lib下

7.在/opt/ice_project/app/userservice/bin新建腳本

[root@zhen bin]# cat env.sh 

#!/bin/sh
if [ -z "$JAVA_HOME" ]; then
        JAVA_HOME="/usr/local/java/jdk1.8.0_191"
fi
echo "JAVA_HOME:$JAVA_HOME"
if [ -z "$SERVER_HOME" ]; then
        BIN_DIR=`readlink -f "$0"`
        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"


[root@zhen bin]# cat startIceServer.sh 

#!/bin/sh
. ./env.sh
 
APP_HOME=$SERVER_HOME/lib
APP_MAINCLASS=com.br.ice.service.applyfeature.ApplyFeatureServiceServer
 
CLASSPATH=$APP_HOME
echo $CLASSPATH
for i in "$APP_HOME"/*.jar;do CLASSPATH="$CLASSPATH":"$i";done


echo $CLASSPATH

echo "$JAVA_HOME/bin/java -Xms512M -Xmx512M -XX:PermSize=128m -XX:MaxPermSize=256m -cp  $CLASSPATH  $APP_MAINCLASS"
$JAVA_HOME/bin/java -Xms512M -Xmx512M -XX:PermSize=128m -XX:MaxPermSize=256m -cp  $CLASSPATH  $APP_MAINCLASS

賦予執行權限

chmod +x env.sh startIceServer.sh 

8.遠程192.168.1.25端口10006開通開防火牆

sudo firewall-cmd --zone=public --add-port=10006/tcp --permanent
systemctl restart firewalld

9.本地驗證遠程192.168.1.25端口10006開通

telnet 192.168.1.25 10006
退出
Ctrl +C Ctrl +C 
或者
exit

10.新建客戶端調用

package com.yinzhen.demo.ice.userservice;

import com.yinzhen.demo.ice.user.UserInfo;
import com.yinzhen.demo.ice.user.UserServicePrx;
import com.yinzhen.demo.ice.user.UserServicePrxHelper;

public class UserServiceClient {

public static void main(String[] args) {
	Ice.Communicator communicator = null;
	try {
		//初始化ice通信器communicator,可以使用args傳入一下ice初始化的參數如超時時間,線程池大小等
		communicator = Ice.Util.initialize(args);
		// 傳入遠程服務單元的 ice對象標識符  協議默認tcp 主機 已經服務監聽端口 -h 172.16.20.220
		Ice.ObjectPrx op = communicator.stringToProxy("UserService:default -h 192.168.1.25  -p 10006");
		// 檢查通用客戶端代理op 是不是queryServer對象標識符所關聯的ice對象的代理
		UserServicePrx userServicePrx = UserServicePrxHelper.checkedCast(op);
		if(userServicePrx == null){
			throw new Exception("qp == null");
		}
		UserInfo userInfo = userServicePrx.getUserInfoById("id");
		if(userInfo == null){
			throw new Exception("userInfo == null");
		}
		// 輸出服務端返回結果
		System.out.println(userInfo.remark);
	} catch (Exception e) {
		// TODO: handle exception
		System.out.println(e);
	}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章