SOFA Boot 整合SOFA RPC 、SOFA Registry

本文档介绍了如何使用SOFAStack进行RPC服务的集成与配置,包括添加依赖、配置application.properties文件、服务发布与调用。重点在于理解SOFABoot的依赖管理和SOFA服务的注册与发现。在实践中应注意版本匹配,以及@Service注解在发布服务时的重要性。
摘要由CSDN通过智能技术生成

参考资料

https://www.wenjiangs.com/doc/dc7xvpxh

https://www.sofastack.tech/projects/sofa-rpc/getting-started-with-rpc/

SOFA Stack 在gitee上的源码仓库

https://gitee.com/sofastack

源码

stulab-sofarpc.zip

整合的关键步骤

添加依赖项

注意版本号

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

<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofa-rpc-all</artifactId>
    <version>5.5.2</version>
</dependency>
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>registry-client-all</artifactId>
    <version>5.2.0</version>
</dependency>

配制application.properties

spring.application.name=service-provider

logging.path=./logs

com.alipay.sofa.rpc.registry.address=sofa://127.0.0.1:9603
com.alipay.sofa.rpc.bolt.port=12201

server.port=8801

发布服务(服务提供方)

import org.springframework.stereotype.Service;

import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.stulab.sofarpc.api.HelloService;

@SofaService(interfaceType = HelloService.class, 
bindings = { @SofaServiceBinding(bindingType = "bolt") })
@Service
public class HelloServiceImpl implements HelloService{

	@Override
	public String hello() {
		return "Hello World";
	}

}

调用服务(服务消息方)

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.stulab.sofarpc.api.HelloService;

@RestController
public class HelloResource {

	@SofaReference(interfaceType = HelloService.class, binding =          @SofaReferenceBinding(bindingType = "bolt"))
	HelloService helloService;

	@RequestMapping("/hello")
	public String hello() {
		return helloService.hello();
	}
	
}

注意点

1、注意SOFA相关依赖的版本,这里面坑挺多

2、发布服务是注意加上@Service注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值