美文网首页
服务间调用修改

服务间调用修改

作者: EddieZhang | 来源:发表于2017-05-19 16:49 被阅读0次

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);
}

相关文章

  • 服务间调用修改

    1:thrift 服务间调用 1.1 pom.xml添加如下的依赖## 1.2 添加spring-client.x...

  • 服务间调用-openFeign

    Feign 简介 Feign 是声明式的服务调用工具,我们只需创建一个接口并用注解的方式来配置它,就可以实现对某个...

  • 消息驱动的分布式事务实现

    微服务架构的事务问题 服务间调用操作的回滚 服务间调用失败的重试问题 微服务架构的事务问题解决 方法1:减少服务间...

  • 微服务架构设计模式

    微服务架构需要考虑的问题 API Gateway 服务间调用 服务发现 服务容错 服务部署 数据调用 聚合器微服务...

  • Hystrix 服务降级

    1.开启服务降级配置 2.微服务调用接口创建一个实现类处理错误 3.修改调用服务接口的注解

  • SpringCloud 服务间的调用

    总结: spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Fe...

  • 八、fegin服务间调用

    本项目会使用之前的Eureka注册中心、会员微服务(两个集群)、新建点名微服务,去调用会员服务中的会员信息。feg...

  • 深入讲解SpringCloud Alibaba 微服务调用,还不

    概述 在微服务架构中,最常见的场景就是微服务间的相互调用。微服务间的相互调用方式主要有RestTemplate、F...

  • 手撸一个RPC框架

    如何调用他人的远程服务? 由于各服务部署在不同机器,服务间的调用免不了网络通信过程,服务消费方每调用一个服务都要写...

  • 微服务调用实例

    微服务的调用示例 1 修改主机hosts windows修改主机host方法 win+r 输入:C:\WINDOW...

网友评论

      本文标题:服务间调用修改

      本文链接:https://www.haomeiwen.com/subject/dtwyxxtx.html