1:thrift 服务间调用
1.1 pom.xml添加如下的依赖##
<libthrift.version>0.9.3</libthrift.version>
<thrift.server.version>2.0-RELEASE</thrift.server.version>
<thrift.client.version>1.0-RELEASE</thrift.client.version>
<thrift.util.version>2.0-RELEASE</thrift.util.version>
<commons-pool.version>1.6</commons-pool.version>
<!-- thrift 公共类 -->
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>${libthrift.version}</version>
</dependency>
<dependency>
<groupId>com.wyun.utils</groupId>
<artifactId>utils</artifactId>
<version>${thrift.util.version}</version>
</dependency>
<dependency>
<groupId>com.wyun.thrift</groupId>
<artifactId>server</artifactId>
<version>${thrift.server.version}</version>
</dependency>
<dependency>
<groupId>com.wyun.thrift</groupId>
<artifactId>client</artifactId>
<version>${thrift.client.version}</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>${commons-pool.version}</version>
</dependency>
1.2 添加spring-client.xml 放到resources下面##
Paste_Image.png
配置你所需调用其他模块的bean,bean的id自己定义,port的value自己线下调试的时候保持与被调用的服务启动端口一致就好,(线上默认80端口)###
Paste_Image.png
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<bean id="protocolClient" class="com.wyun.thrift.client.Factory.ServiceClientProxyFactory" destroy-method="close">
<property name="maxActive" value="20" />
<property name="idleTime" value="1800000" />
<property name="serverName" value="MyService" />
<property name="serverPackage" value="com.wyun.thrift.server." />
<property name="service" value="guest-protocol" />
<property name="port" value="80" />
</bean>
</beans>
那么我的institutionClient 模块需要以8989的端口启动,如下图所示(线上默认8080端口)###
Paste_Image.png
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.zhiweicloud.guest.conf" />
<bean id="SpringBeanUtil" class="com.wyun.utils.SpringBeanUtil"/>
<bean id="airPortServiceImpl" class="com.wyun.thrift.server.processor.WyunServiceImpl"></bean>
<bean id="server" class="com.wyun.thrift.server.server.Server">
<!-- setServer_port -->
<property name="serverPort" value="8080"></property>
<property name="processorName" value="airPortServiceImpl"></property>
</bean>
</beans>
1.3 Main 方法注入 第二步添加的spring-client.xml文件 如下所示##
image.png
package com.zhiweicloud.guest;
import com.zhiweicloud.guest.server.Server;
import org.springframework.context.support.GenericXmlApplicationContext;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by luojing@wyunbank.com on 02/05/2017.
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.getEnvironment().setActiveProfiles("production");
context.setValidating(false);
context.load( "classpath:spring.xml", "classpath:mybatis.xml","classpath:spring-client.xml");
context.refresh();
while (true) {
System.out.println("start");
Thread.sleep(1000000);
}
}
}
1.4##
服务间调用修改###
bean的依赖注入MyService实例,在你需要做服务间调用的类添加如下图,这里需要特别主意,作为hander的类名必须为BusinessService,因为go里面把这个名字写死了####
Paste_Image.png
image.png
private static MyService.Iface protocolClient = SpringBeanUtil.getBean("protocolClient");
服务间调用####
Paste_Image.png
JSONObject jsonObject = new JSONObject();
jsonObject.put("user_id", userId);
jsonObject.put("client_id", airportCode);
jsonObject.put("operation", "protocolList");
for (Long id : ids) {
jsonObject.put("institutionClientId", id);
JSONObject protocolList = new JSONObject();
Response response = ClientUtil.clientSendData(protocolClient, "businessService", jsonObject);
if (response != null && response.getResponeCode().getValue() == 200) {
protocolList = ByteBufferUtil.convertByteBufferToJSON(response.getResponseJSON());
}
1.5 hander返回值修改,从返回string改为JSONObject###
@Override
public JSONObject handle(JSONObject request) {
String success = null;
String operation = null; //operation表示从参数中获取的操作类型"operation"
if (request.get("operation") != null) {
operation = request.getString("operation");
}
switch (operation) {
case "list":
success = list(request);
break;
case "saveOrUpdate":
success = save(request);
break;
case "view":
success = view(request);
break;
case "delete":
success = delete(request);
break;
case "queryInstitutionClientDropdownList":
success = queryInstitutionClientDropdownList(request);
break;
case "getInstitutionType":
success = getInstitutionType(request);
break;
default:
break;
}
return JSON.parseObject(success);
}
网友评论