Search in sources :

Example 6 with ServiceParam

use of com.alipay.sofa.runtime.api.client.param.ServiceParam 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)

Example 7 with ServiceParam

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

the class ClientFactoryTest method testRemoveServiceOrRemoveReference.

/**
 * test removeService and removeReference methods in ReferenceClientImpl and ServiceClientImpl
 */
@Test
public void testRemoveServiceOrRemoveReference() {
    // without unique id
    ServiceParam serviceParam = new ServiceParam();
    serviceParam.setInstance(new PrivateServiceImpl());
    serviceParam.setInterfaceType(PrivateService.class);
    serviceClient.service(serviceParam);
    // with unique id
    serviceParam.setUniqueId("uniqueId");
    serviceClient.service(serviceParam);
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    // ignore
    }
    // reference without unique id
    ReferenceParam<PrivateService> referenceParam = new ReferenceParam<>();
    referenceParam.setInterfaceType(PrivateService.class);
    Assert.assertEquals(PrivateServiceImpl.class.getName(), referenceClient.reference(referenceParam).service());
    // reference with unique id
    referenceParam.setUniqueId("uniqueId");
    Assert.assertEquals(PrivateServiceImpl.class.getName(), referenceClient.reference(referenceParam).service());
    // remove Reference
    referenceClient.removeReference(PrivateService.class);
    referenceClient.removeReference(referenceParam);
    // remove Service
    try {
        serviceClient.removeService(PrivateService.class, -1);
    } catch (IllegalArgumentException ex) {
        Assert.assertTrue(ex.getMessage().contains("Argument delay must be a positive integer or zero"));
    }
    serviceClient.removeService(PrivateService.class, "uniqueId", 0);
}
Also used : ReferenceParam(com.alipay.sofa.runtime.api.client.param.ReferenceParam) ServiceParam(com.alipay.sofa.runtime.api.client.param.ServiceParam) IOException(java.io.IOException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with ServiceParam

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

the class ClientFactoryAwareBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() {
    ServiceParam serviceParam = new ServiceParam();
    serviceParam.setInstance(new DefaultSampleService(ClientFactory.class.getName()));
    serviceParam.setInterfaceType(SampleService.class);
    serviceParam.setUniqueId("clientFactory");
    clientFactory.getClient(ServiceClient.class).service(serviceParam);
}
Also used : DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) ServiceClient(com.alipay.sofa.runtime.api.client.ServiceClient) ServiceParam(com.alipay.sofa.runtime.api.client.param.ServiceParam)

Example 9 with ServiceParam

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

the class ClientFactoryTest method testWithBindingParam.

/**
 * test WithBindingParam in ReferenceClientImpl and ServiceClientImpl
 */
@Test
public void testWithBindingParam() {
    JvmBindingParam jvmBindingParam = new JvmBindingParam();
    jvmBindingParam.setSerialize(true);
    ServiceParam serviceParam = new ServiceParam();
    serviceParam.setInstance(new DefaultSampleService("WithBindingParam"));
    serviceParam.setInterfaceType(SampleService.class);
    serviceParam.setUniqueId("withBindingParam");
    serviceParam.addBindingParam(jvmBindingParam);
    serviceClient.service(serviceParam);
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    // ignore
    }
    ReferenceParam<SampleService> referenceParam = new ReferenceParam<>();
    referenceParam.setInterfaceType(SampleService.class);
    referenceParam.setUniqueId("withBindingParam");
    referenceParam.setBindingParam(jvmBindingParam);
    SampleService service = referenceClient.reference(referenceParam);
    Assert.assertNotNull(service);
    Assert.assertEquals("WithBindingParam", service.service());
}
Also used : DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) DefaultSampleService(com.alipay.sofa.runtime.test.beans.service.DefaultSampleService) SampleService(com.alipay.sofa.runtime.test.beans.facade.SampleService) ReferenceParam(com.alipay.sofa.runtime.api.client.param.ReferenceParam) ServiceParam(com.alipay.sofa.runtime.api.client.param.ServiceParam) IOException(java.io.IOException) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 10 with ServiceParam

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

the class ClientFactoryTest method testBindingInstanceIsNotAssignableFromInterfaceType.

@Test
public void testBindingInstanceIsNotAssignableFromInterfaceType() throws IOException {
    String logRootPath = StringUtils.hasText(System.getProperty("logging.path")) ? System.getProperty("logging.path") : "./logs";
    File sofaLog = new File(logRootPath + File.separator + "sofa-runtime" + File.separator + "sofa-default.log");
    FileUtils.write(sofaLog, "", System.getProperty("file.encoding"));
    SofaRuntimeProperties.setServiceInterfaceTypeCheck(true);
    JvmBindingParam jvmBindingParam = new JvmBindingParam();
    jvmBindingParam.setSerialize(true);
    ServiceParam serviceParam = new ServiceParam();
    serviceParam.setInstance(new Object());
    serviceParam.setInterfaceType(SampleService.class);
    serviceParam.addBindingParam(jvmBindingParam);
    serviceClient.service(serviceParam);
    String content = FileUtils.readFileToString(sofaLog, System.getProperty("file.encoding"));
    Assert.assertTrue(content.contains("SOFA-BOOT-01-00104: Bean " + "[com.alipay.sofa.runtime.test.beans.facade.SampleService] " + "type is [class java.lang.Object] not isAssignableFrom " + "[interface com.alipay.sofa.runtime.test.beans.facade.SampleService] , please check it"));
}
Also used : File(java.io.File) ServiceParam(com.alipay.sofa.runtime.api.client.param.ServiceParam) JvmBindingParam(com.alipay.sofa.runtime.service.binding.JvmBindingParam) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ServiceParam (com.alipay.sofa.runtime.api.client.param.ServiceParam)14 ServiceClient (com.alipay.sofa.runtime.api.client.ServiceClient)8 Test (org.junit.Test)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 RestBindingParam (com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam)6 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)6 ArrayList (java.util.ArrayList)6 ReferenceParam (com.alipay.sofa.runtime.api.client.param.ReferenceParam)4 JvmBindingParam (com.alipay.sofa.runtime.service.binding.JvmBindingParam)4 DefaultSampleService (com.alipay.sofa.runtime.test.beans.service.DefaultSampleService)4 IOException (java.io.IOException)4 SwaggerRestServiceImpl (com.alipay.sofa.rpc.doc.swagger.rest.SwaggerRestServiceImpl)2 SampleService (com.alipay.sofa.runtime.test.beans.facade.SampleService)2 File (java.io.File)2 HttpResponse (org.apache.http.HttpResponse)2 HttpClient (org.apache.http.client.HttpClient)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2