服务间调用修改

1:thrift 服务间调用

1.1 pom.xml添加如下的依赖##

0.9.3
2.0-RELEASE
1.0-RELEASE
2.0-RELEASE
1.6

    
        org.apache.thrift
        libthrift
        ${libthrift.version}
    
    
        com.wyun.utils
        utils
        ${thrift.util.version}
    
    
        com.wyun.thrift
        server
        ${thrift.server.version}
    
    
        com.wyun.thrift
        client
        ${thrift.client.version}
    
    
        commons-pool
        commons-pool
        ${commons-pool.version}
    

1.2 添加spring-client.xml 放到resources下面##

服务间调用修改_第1张图片
Paste_Image.png

配置你所需调用其他模块的bean,bean的id自己定义,port的value自己线下调试的时候保持与被调用的服务启动端口一致就好,(线上默认80端口)###

服务间调用修改_第2张图片
Paste_Image.png

    
    
        
            
            
            
            
            
            
        
    

那么我的institutionClient 模块需要以8989的端口启动,如下图所示(线上默认8080端口)###

服务间调用修改_第3张图片
Paste_Image.png



    
    
    
        
        
        
    

1.3 Main 方法注入 第二步添加的spring-client.xml文件 如下所示##

服务间调用修改_第4张图片
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 [email protected] 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里面把这个名字写死了####

服务间调用修改_第5张图片
Paste_Image.png
服务间调用修改_第6张图片
image.png
private static MyService.Iface protocolClient = SpringBeanUtil.getBean("protocolClient");

服务间调用####

服务间调用修改_第7张图片
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);
}

你可能感兴趣的:(服务间调用修改)