Ant RPC framework SOFA-RPC first experience

image.png

foreword

Recently, Ant Financial has open sourced the distributed framework SOFA. The landlord wrote a demo to experience the functions of SOFA. SOFA is fully compatible with SpringBoot (of course, Dubbo is also compatible).

Project address: Alipay , this homepage has 5 projects, all of which are open sourced by Ali.
sofa-boot ,
sofa-rpc ,
sofa-bolt ,
sofa-ark ,
sofa-rpc-boot-projects .

quick start

In fact, the official documentation of SOFA-RPC has detailed how to use this RPC framework, based on Netty's long connection. Similar to Dubbo. The landlord looks at this framework mainly to learn the design of the distributed RPC framework.

Since the test example requires two projects, we create a directory and create two maven modules in the directory (the SpringBoot project is sufficient):

One producer, one consumer.

Replace the parent tag of springBoot in the pom.xml of these two projects with the following:

  <parent>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofaboot-dependencies</artifactId>
    <version>2.3.1</version>
  </parent>

Add one more dependency:

<dependency>
  <groupId>com.alipay.sofa</groupId>
  <artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>

At this point, the dependency and construction of the RPC framework is good, isn't it simple?

interface creation

Since it is an RPC service, it needs an interface and an implementation class. We create here at the provider.

public interface HelloSyncService {
  String saySync(String string);
}

// 实现类
public class HelloSyncServiceImpl implements HelloSyncService {

  @Override
  public String saySync(String string) {
    return "provider tell you : this is your say: " +  string;
  }
}

Then add a dependency on this interface in the consumer's pom.xml.

<dependency>
  <groupId>cn.think.in.java</groupId>
  <artifactId>provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <scope>compile</scope>
</dependency>

Once you have an interface, you need to configure it.

Interface configuration

First publish the interface here in the provider. Create an xml file named: rpc-sofa-boot-starter-samples.xml.

document content:

<?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:sofa="http://sofastack.io/schema/sofaboot"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://sofastack.io/schema/sofaboot   http://sofastack.io/schema/sofaboot.xsd"
  default-autowire="byName">

  <bean id="helloSyncServiceImpl" class="cn.think.in.java.provider.HelloSyncServiceImpl"/>
  <sofa:service ref="helloSyncServiceImpl" interface="cn.think.in.java.provider.HelloSyncService">
    <sofa:binding.bolt/>
  </sofa:service>
</beans>

Very simple, publish an interface, a bean similar to Spring.

At the same time, the protocol of this interface is bolt, which is Ali's RPC network communication framework solt (based on the best practices of Netty).

Similarly, under the resource file of the consumer, a file with the same name is also created. The content is slightly different.

<?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:sofa="http://sofastack.io/schema/sofaboot"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://sofastack.io/schema/sofaboot   http://sofastack.io/schema/sofaboot.xsd"
  default-autowire="byName">

  <sofa:reference id="helloSyncServiceReference" interface="cn.think.in.java.provider.HelloSyncService">
    <sofa:binding.bolt/>
  </sofa:reference>
</beans>

Get a bean through an interface.

OK, the interface is configured, then you can start the test.

Prepare for the test

There is still a little work to do before testing.

In the provider configuration file application.perproties, configure the port and program name.

# server.port=8080 # 默认
spring.application.name=provider

The default port is 8080, so there is no need to configure it.

Then, configure this file in the consumer as well. The content is as follows:

spring.application.name=consumer
server.port=8081

Consumer and provider ports cannot conflict.

One last step left.

Import the file into the Spring container.

Add the following on the provider startup class (introduce the configuration file):

@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" })
@SpringBootApplication
public class ProviderApplication {

  public static void main(String[] args) {
    SpringApplication.run(ProviderApplication.class, args);
  }
}

Introduce the following in the consumer startup class:

@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" })
@SpringBootApplication
public class ConsumerApplication {

  public static void main(String[] args) {
    SpringApplication springApplication = new SpringApplication(ConsumerApplication.class);
    ApplicationContext applicationContext = springApplication.run(args);

    HelloSyncService helloSyncServiceReference = (HelloSyncService) applicationContext
        .getBean("helloSyncServiceReference");

    System.out.println(helloSyncServiceReference.saySync("sync"));
  }
}

A little more stuff, but still pretty simple.

First create a Spring boot class. Then run, get the bean (remote call encapsulated by dynamic proxy) from the Spirng container. Then call the delegate method.

run

Run the provider first:

2018-04-23 23:18:24.776  INFO 26654 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2018-04-23 23:18:24.776  INFO 26654 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env || /env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-04-23 23:18:24.886  INFO 26654 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-23 23:18:24.893  INFO 26654 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
Sofa-Middleware-Log SLF4J : Actual binding is of type [ com.alipay.remoting Logback ]
2018-04-23 23:18:24.966  INFO 26654 --- [           main] com.alipay.sofa.common.log               : Sofa-Middleware-Log SLF4J : Actual binding is of type [ com.alipay.remoting Logback ]
2018-04-23 23:18:25.174  INFO 26654 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-04-23 23:18:25.179  INFO 26654 --- [           main] c.t.i.java.provider.ProviderApplication  : Started ProviderApplication in 3.352 seconds (JVM running for 3.978)

Run the consumer again:

2018-04-23 23:19:21.940  INFO 26673 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env || /env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-04-23 23:19:22.055  INFO 26673 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-23 23:19:22.063  INFO 26673 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2018-04-23 23:19:22.319  INFO 26673 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2018-04-23 23:19:22.324  INFO 26673 --- [           main] c.t.i.java.consumer.ConsumerApplication  : Started ConsumerApplication in 3.898 seconds (JVM running for 4.524)
provider tell you : this is your say: sync

The result is printed successfully.

Summarize

First of all, the first feeling is that this framework is still very easy to use, quite simple, based on the current SpringBoot. Quick Start. And it is not the Http call of SpringCloud, using Netty as the network communication framework, of course, there is no problem in performance.

Of course, the registry used by our demo here does not use ZK. After all, the first experience is the use of local files.

However, the landlord has a lot of interest in this framework. In the next free time, the landlord will study SOFA-related codes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325137464&siteId=291194637
RPC