This article shows you how to quickly get started with SOFARPC by using demo projects to publish and reference services, deploy applications, and govern services.
Demo Projects
- Click to download SOFARPC Demo Projects.
- Open
myserver-app
andmyclient-app
projects in the demo respectively by IDEA or Eclipse.
Service Validation
- First, start the
SOFABootWebSpringApplication
of the myserver-app web module to publish a service. - Then, start the
SOFABootWebSpringApplication
reference service of the myclient-app web module to reference a service.
If the reference succeeds, the myclient-app console will output as follows:
Response from myserver-app: Hello SOFARpc! times = xx
You can also view the service reference result in the log. You can set the log path in src/main/resources/config/application.properties
under web module. The service reference result is provided in logs/myweb-app/common-default.log
by default.
For more information about other local compilation methods, see Compilation and Execution.
Demo Explaination
Key information
- groupId: the unique ID of the project organization. It is
com.alipay.mytestsofa
in the demo project. - artifactId: the artifact ID of the project. It is
myserver-app
in the demo project. - version: the version number. The default version is
1.0-SNAPSHOT
. - package: the app package name. It is equivalent to the groupId by default.
Configuration
The annotation-based configurations for service publishing and reference are as follows:
- Service Publishing:
@SofaService(interfaceType = SampleService.class,bindings = @SofaServiceBinding(bindingType = "bolt"))
- Service Reference:
@SofaReference(interfaceType = SampleService.class,binding = @SofaReferenceBinding(bindingType = "bolt",directUrl = "127.0.0.1:12201"))
private SampleService sampleService;
Note:
RPC Principle of Demo Project
- You should provide the same service interface and implementation at the same location in the endpoint module of two projects, and discover the service by using the annotation. The two projects are linked with each other through the same interface. One of the two projects serves as the client and the other as the server. In the local test, you can discover a service by using the testurl configuration in annotation. If tests are executed in the cloud server, you can use the DSR(Direct Server Return) to discover a service.
- Start SOFABootWebSpringApplication in the myserver-app web module to publish a service.
- Start SOFABootWebSpringApplication in the myclient-app web module to reference a service.
Service Publishing
The steps required to publish a service are as follows:
- Design Interface: Design the service interface class
SampleService.java
. The code is as follows.
The interface path iscom.alipay.samples.rpc.SampleService
./**
* Service interface class
*/
public interface SampleService {
public String hello();
}
Implement Interface: Create the service implementation class
SampleServiceImpl.java
.- The interface implementation path is
com.alipay.samples.rpc.impl.SampleServiceImpl
. - The code is as follows:
@Service
@SofaService(interfaceType = SampleService.class,bindings = @SofaServiceBinding(bindingType = "bolt"))
public class SampleServiceImpl implements SampleService {
private int count = 0;
@Override
public String hello() {
return "Hello SOFARpc! times = " + count++;
}
}
- The interface implementation path is
Package Scan:
@SpringBootApplication(scanBasePackages = {"com.alipay.mytestsofa","com.alipay.samples.rpc"})
public class SOFABootWebSpringApplication {
...
}
- Port Modification: The following configurations are for local test only. please remove them or comment them out before cloud deployment.
Properties Configuration: Please guarantee the following properties are properly configured in
application.properties
before cloud deployment. For more information, please refer to Import SOFA Middleware。The configuration steps are as follows:Please visit SOFAStack Console > R&D Performance > Scaffold > Step 2 for following information:
com.alipay.instanceid
: the unique ID of the app instance in the workspace. You can obtain this ID from Framework > Step 2 > Instance ID.com.antcloud.antvip.endpoint
: the AntVIP used by the app to obtain the server address of each component. The AntVIP varies with the region. You can obtain the value from Framework > Step 2 > AntVIP.com.antcloud.mw.access
andcom.antcloud.mw.secret
: the authentication keys for accessing the middleware. You can obtain it from the RAM console. For more information, see Create an AccessKey pair.
Add the following information:
run.mode=NORMAL
com.alipay.env=shared
Service Reference
The object injection and service reference are achieved through annotation. In demo myclient-app
, the steps required to reference a service are as follows:
- Object Injection: ReferenceHolder was created to manage all referenced instances in the demo.
@Component
public class ReferenceHolder {
@SofaReference(interfaceType = SampleService.class,binding = @SofaReferenceBinding(bindingType = “bolt”,directUrl = “127.0.0.1:12201”))
private SampleService sampleService;
public SampleService getSampleService() {
return sampleService;
}
public void setSampleService(SampleService sampleService) {
this.sampleService = sampleService;
}
}
note: 12201 is configured in
application.properties
in web module in myserver-app project. Object Reference:
@SpringBootApplication
public class SOFABootWebSpringApplication {
private static final Logger logger = LoggerFactory.getLogger(SOFABootWebSpringApplication.class);
public static void main(String[] args) {
//*************** Attention ******************//
// This configuration is to avoid port conflict only. please remove or comment out before cloud deployment.
System.setProperty("server.port", "8084");
//This configuration is for local test only. please remove or comment out before cloud deployment.
System.setProperty("run.mode", "TEST");
//********************************//
SpringApplication springApplication = new SpringApplication(SOFABootWebSpringApplication.class);
ApplicationContext applicationContext = springApplication.run(args);
if (logger.isInfoEnabled()) {
printMsg("SofaRpc Application (myclient-app) started on 8084 port.");
}
//reference rpc service
ReferenceHolder referenceHolder = applicationContext.getBean(ReferenceHolder.class);
final SampleService sampleService = referenceHolder.getSampleService();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
String response = sampleService.hello();
printMsg("Response from myserver-app: " + response);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
//ignore
}
}
}
}
}).start();
}
private static void printMsg(String msg) {
System.out.println(msg);
if (logger.isInfoEnabled()) {
logger.info(msg);
}
}
}
Port Modification: Add
rpc.tr.port=12202
toapplication.properties
.rpc.tr.port
is a port number with a default value 0f 12200 in TR protocol.Note: This configuration is for local test only. please remove it or comment it out before cloud deployment.
Properties Configuration: Please guarantee the following properties are properly configured in
application.properties
before cloud deployment. For detailed configuration,please refer to Properties Configuration.
Application Deployment
Please refer to the following documents for local packaging and cloud deployment
- Packaging: please see Compilation and Execution.
- Cloud Deployment
- For general procedures, please see Buildpacks and Application Deployment Procedure in Buildpacks User Guide.
- For detailed procedures, please see Quick Start under Classic Application Service.
Cloud Governance
You are able to query and govern the deployed RPC service through SOFAStack Console > Middleware > Microservice Platform > Microservice. For more information, please refer to Service Management > View and Manage RPC Service。