GrpcConfig.java 15.8 KB
package com.infoloop.tianting.config;

import com.infoloop.rpc.meizhongyiheservice.MeiZhongYiHeServiceRpcGrpc;
import com.infoloop.tianting.COperatorServiceProtoRpcGrpc;
import com.infoloop.tianting.CategoriesServiceProtoRpcGrpc;
import com.infoloop.tianting.SkuServiceProtoRpcGrpc;
import com.infoloop.tianting.WOperatorServiceProtoRpcGrpc;
import com.infoloop.tianting.clientcustomerorderservice.ClientCustomerOrderServiceRpcGrpc;
import com.infoloop.tianting.clientcustomerservice.ClientCustomerServiceRpcGrpc;
import com.infoloop.tianting.clientinventoryservice.TiantingClientInventoryServiceRpcGrpc;
import com.infoloop.tianting.clientresourcetagservice.ClientResourceTagServiceRpcGrpc;
import com.infoloop.tianting.deliveryruleservice.TiantingDeliveryRuleServiceRpcGrpc;
import com.infoloop.tianting.enterpriseresourcetagservice.EnterpriseResourceTagServiceRpcGrpc;
import com.infoloop.tianting.mealorderservice.MealOrderServiceRpcGrpc;
import com.infoloop.tianting.menuservice.MenuServiceRpcGrpc;
import com.infoloop.tianting.setmealscheduleservice.SetMealScheduleServiceRpcGrpc;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannel;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

import static com.infoloop.tianting.constant.ConfigConstants.CATEGORY_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.CATEGORY_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.CATEGORY_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_CUSTOMER_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_CUSTOMER_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_CUSTOMER_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_INVENTORY_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_INVENTORY_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_INVENTORY_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_RESOURCE_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_RESOURCE_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.CLIENT_RESOURCE_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.DELIVERY_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.DELIVERY_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.DELIVERY_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.ENTERPRISE_RESOURCE_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.ENTERPRISE_RESOURCE_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.ENTERPRISE_RESOURCE_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.MEAL_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.MEAL_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.MEAL_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.MEIZHONGYIHE_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.MEIZHONGYIHE_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.MEIZHONGYIHE_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.MENU_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.MENU_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.MENU_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.OPERATOR_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.OPERATOR_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.OPERATOR_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.ORDER_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.ORDER_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.ORDER_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.SKU_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.SKU_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.SKU_SERVICE_RPC_URL;
import static com.infoloop.tianting.constant.ConfigConstants.WOPERATOR_SERVICE_CHANNEL;
import static com.infoloop.tianting.constant.ConfigConstants.WOPERATOR_SERVICE_RPC_PORT;
import static com.infoloop.tianting.constant.ConfigConstants.WOPERATOR_SERVICE_RPC_URL;

@Configuration
public class GrpcConfig {

    private static final int DEFAULT_TIMEOUT_DAYS = 7;

    @Bean(ORDER_SERVICE_CHANNEL)
    public ManagedChannel orderServiceChannel(@Value(ORDER_SERVICE_RPC_URL) final String url,
                                              @Value(ORDER_SERVICE_RPC_PORT) final int port,
                                              @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public ClientCustomerOrderServiceRpcGrpc.ClientCustomerOrderServiceRpcBlockingStub orderServiceRpcBlockingStub(@Autowired @Qualifier(ORDER_SERVICE_CHANNEL) final ManagedChannel channel) {
        return ClientCustomerOrderServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean
    public MealOrderServiceRpcGrpc.MealOrderServiceRpcBlockingStub mealOrderServiceRpcBlockingStub(@Autowired @Qualifier(ORDER_SERVICE_CHANNEL) final ManagedChannel channel) {
        return MealOrderServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean(MENU_SERVICE_CHANNEL)
    public ManagedChannel menuServiceChannel(@Value(MENU_SERVICE_RPC_URL) final String url,
        @Value(MENU_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public MenuServiceRpcGrpc.MenuServiceRpcBlockingStub menuServiceRpcBlockingStub(@Autowired @Qualifier(MENU_SERVICE_CHANNEL) final ManagedChannel channel) {
        return MenuServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean(SKU_SERVICE_CHANNEL)
    public ManagedChannel skuServiceChannel(@Value(SKU_SERVICE_RPC_URL) final String url,
        @Value(SKU_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public SkuServiceProtoRpcGrpc.SkuServiceProtoRpcBlockingStub skuServiceRpcBlockingStub(@Autowired @Qualifier(SKU_SERVICE_CHANNEL) final ManagedChannel channel) {
        return SkuServiceProtoRpcGrpc.newBlockingStub(channel);
    }

    @Bean(CATEGORY_SERVICE_CHANNEL)
    public ManagedChannel categoryServiceChannel(@Value(CATEGORY_SERVICE_RPC_URL) final String url,
        @Value(CATEGORY_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public CategoriesServiceProtoRpcGrpc.CategoriesServiceProtoRpcBlockingStub categoryServiceRpcBlockingStub(@Autowired @Qualifier(CATEGORY_SERVICE_CHANNEL) final ManagedChannel channel) {
        return CategoriesServiceProtoRpcGrpc.newBlockingStub(channel);
    }

    @Bean(CLIENT_CUSTOMER_SERVICE_CHANNEL)
    public ManagedChannel clientCustomerServiceChannel(@Value(CLIENT_CUSTOMER_SERVICE_RPC_URL) final String url,
        @Value(CLIENT_CUSTOMER_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public ClientCustomerServiceRpcGrpc.ClientCustomerServiceRpcBlockingStub customerServiceRpcBlockingStub(@Autowired @Qualifier(CLIENT_CUSTOMER_SERVICE_CHANNEL) final ManagedChannel channel) {
        return ClientCustomerServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean(CLIENT_INVENTORY_SERVICE_CHANNEL)
    public ManagedChannel clientInventoryServiceChannel(@Value(CLIENT_INVENTORY_SERVICE_RPC_URL) final String url,
        @Value(CLIENT_INVENTORY_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public TiantingClientInventoryServiceRpcGrpc.TiantingClientInventoryServiceRpcBlockingStub inventoryServiceRpcBlockingStub(@Autowired @Qualifier(CLIENT_INVENTORY_SERVICE_CHANNEL) final ManagedChannel channel) {
        return TiantingClientInventoryServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean(MEAL_SERVICE_CHANNEL)
    public ManagedChannel mealServiceChannel(@Value(MEAL_SERVICE_RPC_URL) final String url,
        @Value(MEAL_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public SetMealScheduleServiceRpcGrpc.SetMealScheduleServiceRpcBlockingStub mealServiceRpcBlockingStub(@Autowired @Qualifier(MEAL_SERVICE_CHANNEL) final ManagedChannel channel) {
        return SetMealScheduleServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean(DELIVERY_SERVICE_CHANNEL)
    public ManagedChannel deliveryServiceChannel(@Value(DELIVERY_SERVICE_RPC_URL) final String url,
        @Value(DELIVERY_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public TiantingDeliveryRuleServiceRpcGrpc.TiantingDeliveryRuleServiceRpcBlockingStub deliveryServiceRpcBlockingStub(@Autowired @Qualifier(DELIVERY_SERVICE_CHANNEL) final ManagedChannel channel) {
        return TiantingDeliveryRuleServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean(OPERATOR_SERVICE_CHANNEL)
    public ManagedChannel operatorServiceChannel(@Value(OPERATOR_SERVICE_RPC_URL) final String url,
        @Value(OPERATOR_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public COperatorServiceProtoRpcGrpc.COperatorServiceProtoRpcBlockingStub operatorServiceRpcBlockingStub(@Autowired @Qualifier(OPERATOR_SERVICE_CHANNEL) final ManagedChannel channel) {
        return COperatorServiceProtoRpcGrpc.newBlockingStub(channel);
    }

    @Bean(WOPERATOR_SERVICE_CHANNEL)
    public ManagedChannel woperatorServiceChannel(@Value(WOPERATOR_SERVICE_RPC_URL) final String url,
        @Value(WOPERATOR_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public WOperatorServiceProtoRpcGrpc.WOperatorServiceProtoRpcBlockingStub woperatorServiceRpcBlockingStub(@Autowired @Qualifier(WOPERATOR_SERVICE_CHANNEL) final ManagedChannel channel) {
        return WOperatorServiceProtoRpcGrpc.newBlockingStub(channel);
    }

    @Bean(ENTERPRISE_RESOURCE_SERVICE_CHANNEL)
    public ManagedChannel enterpriseResourceServiceChannel(@Value(ENTERPRISE_RESOURCE_SERVICE_RPC_URL) final String url,
        @Value(ENTERPRISE_RESOURCE_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public EnterpriseResourceTagServiceRpcGrpc.EnterpriseResourceTagServiceRpcBlockingStub enterpriseResourceRpcBlockingStub(@Autowired @Qualifier(ENTERPRISE_RESOURCE_SERVICE_CHANNEL) final ManagedChannel channel) {
        return EnterpriseResourceTagServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean(MEIZHONGYIHE_SERVICE_CHANNEL)
    public ManagedChannel meiZhongYiHeServiceChannel(@Value(MEIZHONGYIHE_SERVICE_RPC_URL) final String url,
        @Value(MEIZHONGYIHE_SERVICE_RPC_PORT) final int port,
        @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
            .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
            .intercept(clientInterceptor)
            .usePlaintext()
            .maxInboundMessageSize(Integer.MAX_VALUE)
            .build();
    }

    @Bean
    public MeiZhongYiHeServiceRpcGrpc.MeiZhongYiHeServiceRpcBlockingStub meiZhongYiHeServiceRpcBlockingStub(@Autowired @Qualifier(MEIZHONGYIHE_SERVICE_CHANNEL) final ManagedChannel channel) {
        return MeiZhongYiHeServiceRpcGrpc.newBlockingStub(channel);
    }

    @Bean(CLIENT_RESOURCE_SERVICE_CHANNEL)
    public ManagedChannel clientResourceServiceChannel(@Value(CLIENT_RESOURCE_SERVICE_RPC_URL) final String url,
                                                     @Value(CLIENT_RESOURCE_SERVICE_RPC_PORT) final int port,
                                                     @Autowired final ClientInterceptor clientInterceptor) {
        return NettyChannelBuilder.forAddress(url, port)
                .idleTimeout(DEFAULT_TIMEOUT_DAYS, TimeUnit.DAYS)
                .intercept(clientInterceptor)
                .usePlaintext()
                .maxInboundMessageSize(Integer.MAX_VALUE)
                .build();
    }

    @Bean
    public ClientResourceTagServiceRpcGrpc.ClientResourceTagServiceRpcBlockingStub clientResourceTagServiceRpcBlockingStub(@Autowired @Qualifier(CLIENT_RESOURCE_SERVICE_CHANNEL) final ManagedChannel channel) {
        return ClientResourceTagServiceRpcGrpc.newBlockingStub(channel);
    }
}