当前位置: 首页 > 工具软件 > SOFABoot > 使用案例 >

SOFA Boot 整合SOFA RPC 、SOFA Registry

充小云
2023-12-01

参考资料

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注解

 类似资料: