算了,懒得写了,假装这里有个前言,关于SOFA的介绍还是看这里吧:SOFA官网
1、建立一个spring boot的项目——sofa_provider,引入pom文件如下
这里需要说明一下:根据官网的使用规范,需要替换掉原有的替换 spring-boot-starter-parent引用,改为 sofaboot-dependencies
4.0.0
com.alipay.sofa
sofaboot-dependencies
2.5.0
com.learn
sofa-provider
0.0.1-SNAPSHOT
sofa-provider
SOFAProvider
1.8
com.alipay.sofa
rpc-sofa-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.101tec
zkclient
org.slf4j
slf4j-api
org.slf4j
slf4j-log4j12
org.apache.zookeeper
zookeeper
0.10
org.springframework.boot
spring-boot-maven-plugin
2、配置相关属性,application.name必须要指定
#can't be deleted
spring.application.name=sofaProvider
#this application log level
logging.level.com.alipay.test=INFO
#zookeeper
com.alipay.sofa.rpc.registry.address=zookeeper://localhost:2181
3、测试业务类编写
接口
package com.learn.sofa_provider.service;
/**
* author:liman
* createtime:2019/4/15
*/
public interface IHelloworldService {
public String helloSOFA(String name);
}
实现类
package com.learn.sofa_provider.service;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import org.springframework.stereotype.Component;
/**
* author:liman
* createtime:2019/4/15
*/
@SofaService(interfaceType = IHelloworldService.class,bindings = {@SofaServiceBinding(bindingType = "dubbo")})
@Component
public class HelloworldServiceImpl implements IHelloworldService {
@Override
public String helloSOFA(String name) {
return "Hello " + name + " this is world of SOFA";
}
}
消费者实例编写完成。
建立一个springboot的项目——sofa-api。这个就相当于一个jar包的功能,pom文件中修改为如下即可:
com.alipay.sofa
sofaboot-dependencies
3.0.0
4.0.0
com.learn
sofa-api
pom
0.0.1-SNAPSHOT
com.learn
sofa-provider
0.0.1-SNAPSHOT
1、建立一个spring boot项目——sofa-consumer。pom文件和生产者相差不大,如下所示(注意:依旧需要替换掉spring boot项目自己生成的parent,同时需要引用api中间件)这里就不贴出来了,
2、编写业务类
package com.learn.sofaconsumer.controller;
import com.learn.sofaconsumer.service.HelloworldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* author:liman
* createtime:2019/4/15
*/
@RestController
public class controller {
@Autowired
private HelloworldService helloworldService;
@RequestMapping("/testSOFA")
public String helloSOFA(@RequestParam String name){
return helloworldService.sayHelloToSOFA(name);
}
}
package com.learn.sofaconsumer.service;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.learn.sofa_provider.service.IHelloworldService;
import org.springframework.stereotype.Component;
/**
* author:liman
* createtime:2019/4/15
*/
@Component
public class HelloworldService {
@SofaReference(interfaceType = IHelloworldService.class,binding = @SofaReferenceBinding(bindingType = "dubbo"))
private IHelloworldService helloworldService;
public String sayHelloToSOFA(String name){
return helloworldService.helloSOFA(name);
}
}
3、properties文件如下
server.port=8889
spring.application.name=sofaConsumer
com.alipay.sofa.rpc.registry.address=zookeeper://localhost:2181
启动zookeeper,先启动生产者,然后启动消费者即可(api中间件不用启动)
浏览器输入相关地址,可以看到如下结果:
顺便截一张SOFA-boot的启动图片
一些使用在SOFA的官网中比较详细了,其实SOFA貌似和dubbo有很多类似的地方,但是该组件貌似在金融圈内使用较多。