Search in sources :

Example 1 with BindingParam

use of com.alipay.sofa.runtime.api.client.param.BindingParam in project sofa-boot by sofastack.

the class SofaBootRpcAllTest method testRestSwaggerAddService.

@Test
public void testRestSwaggerAddService() throws IOException {
    List<BindingParam> bindingParams = new ArrayList<>();
    bindingParams.add(new RestBindingParam());
    ServiceParam serviceParam = new ServiceParam();
    serviceParam.setInterfaceType(AddService.class);
    serviceParam.setInstance((AddService) () -> "Hello");
    serviceParam.setBindingParams(bindingParams);
    ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class);
    serviceClient.service(serviceParam);
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpUriRequest request = new HttpGet("http://localhost:8341/swagger/openapi");
    HttpResponse response = httpClient.execute(request);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    Assert.assertTrue(EntityUtils.toString(response.getEntity()).contains("/webapi/add_service"));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RestBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam) ServiceClient(com.alipay.sofa.runtime.api.client.ServiceClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) RestBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam) ServiceParam(com.alipay.sofa.runtime.api.client.param.ServiceParam) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 2 with BindingParam

use of com.alipay.sofa.runtime.api.client.param.BindingParam in project sofa-boot by sofastack.

the class ServiceClientImpl method service.

@SuppressWarnings("unchecked")
public void service(ServiceParam serviceParam) {
    Implementation implementation = new DefaultImplementation();
    implementation.setTarget(serviceParam.getInstance());
    if (serviceParam.getInterfaceType() == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00201"));
    }
    Service service = new ServiceImpl(serviceParam.getUniqueId(), serviceParam.getInterfaceType(), InterfaceMode.api, serviceParam.getInstance(), null);
    for (BindingParam bindingParam : serviceParam.getBindingParams()) {
        BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
        }
        BindingConverterContext bindingConverterContext = new BindingConverterContext();
        bindingConverterContext.setInBinding(false);
        bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
        bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
        Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
        service.addBinding(binding);
    }
    boolean hasJvmBinding = false;
    for (Binding binding : service.getBindings()) {
        if (binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE)) {
            hasJvmBinding = true;
            break;
        }
    }
    if (!hasJvmBinding) {
        service.addBinding(new JvmBinding());
    }
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, bindingAdapterFactory, sofaRuntimeContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) Service(com.alipay.sofa.runtime.service.component.Service) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext)

Example 3 with BindingParam

use of com.alipay.sofa.runtime.api.client.param.BindingParam in project sofa-boot by alipay.

the class ServiceClientImpl method service.

@SuppressWarnings("unchecked")
public void service(ServiceParam serviceParam) {
    Implementation implementation = new DefaultImplementation();
    implementation.setTarget(serviceParam.getInstance());
    if (serviceParam.getInterfaceType() == null) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-00201"));
    }
    Service service = new ServiceImpl(serviceParam.getUniqueId(), serviceParam.getInterfaceType(), InterfaceMode.api, serviceParam.getInstance(), null);
    for (BindingParam bindingParam : serviceParam.getBindingParams()) {
        BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
        }
        BindingConverterContext bindingConverterContext = new BindingConverterContext();
        bindingConverterContext.setInBinding(false);
        bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
        bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
        Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
        service.addBinding(binding);
    }
    boolean hasJvmBinding = false;
    for (Binding binding : service.getBindings()) {
        if (binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE)) {
            hasJvmBinding = true;
            break;
        }
    }
    if (!hasJvmBinding) {
        service.addBinding(new JvmBinding());
    }
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, bindingAdapterFactory, sofaRuntimeContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) Service(com.alipay.sofa.runtime.service.component.Service) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext)

Example 4 with BindingParam

use of com.alipay.sofa.runtime.api.client.param.BindingParam in project sofa-boot by alipay.

the class BoltSwaggerServiceApplicationListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
    List<BindingParam> bindingParams = new ArrayList<>();
    bindingParams.add(new RestBindingParam());
    ServiceParam serviceParam = new ServiceParam();
    serviceParam.setInterfaceType(SwaggerRestService.class);
    serviceParam.setInstance(new SwaggerRestServiceImpl());
    serviceParam.setBindingParams(bindingParams);
    ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class);
    serviceClient.service(serviceParam);
}
Also used : SwaggerRestServiceImpl(com.alipay.sofa.rpc.doc.swagger.rest.SwaggerRestServiceImpl) RestBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam) ServiceClient(com.alipay.sofa.runtime.api.client.ServiceClient) ArrayList(java.util.ArrayList) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) RestBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam) ServiceParam(com.alipay.sofa.runtime.api.client.param.ServiceParam)

Example 5 with BindingParam

use of com.alipay.sofa.runtime.api.client.param.BindingParam in project sofa-boot by alipay.

the class SwaggerServiceApplicationListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
    List<BindingParam> bindingParams = new ArrayList<>();
    bindingParams.add(new RestBindingParam());
    ServiceParam serviceParam = new ServiceParam();
    serviceParam.setInterfaceType(SwaggerService.class);
    serviceParam.setInstance(new SwaggerServiceImpl());
    serviceParam.setBindingParams(bindingParams);
    ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class);
    serviceClient.service(serviceParam);
}
Also used : RestBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam) ServiceClient(com.alipay.sofa.runtime.api.client.ServiceClient) ArrayList(java.util.ArrayList) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) RestBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam) ServiceParam(com.alipay.sofa.runtime.api.client.param.ServiceParam)

Aggregations

BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)10 RestBindingParam (com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam)6 ServiceClient (com.alipay.sofa.runtime.api.client.ServiceClient)6 ServiceParam (com.alipay.sofa.runtime.api.client.param.ServiceParam)6 ArrayList (java.util.ArrayList)6 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)4 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)4 Binding (com.alipay.sofa.runtime.spi.binding.Binding)4 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)4 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)4 SwaggerRestServiceImpl (com.alipay.sofa.rpc.doc.swagger.rest.SwaggerRestServiceImpl)2 JvmBindingParam (com.alipay.sofa.runtime.service.binding.JvmBindingParam)2 Reference (com.alipay.sofa.runtime.service.component.Reference)2 Service (com.alipay.sofa.runtime.service.component.Service)2 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)2 ReferenceImpl (com.alipay.sofa.runtime.service.component.impl.ReferenceImpl)2 ServiceImpl (com.alipay.sofa.runtime.service.component.impl.ServiceImpl)2 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)2 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)2 Implementation (com.alipay.sofa.runtime.spi.component.Implementation)2