Sofa服务接入
此篇文章是介绍 sofa 服务接入到 Apache ShenYu 网关,Apache ShenYu 网关使用 sofa 插件来接入sofa服务。
接入前,请正确启动 shenyu-admin,并开启sofa插件,在网关端和sofa服务端引入相关依赖。可以参考前面的 Sofa快速开始。
关于插件使用可请参考:Sofa插件
应用客户端接入的相关配置请参考:客户端接入配置。
数据同步的相关配置请参考:数据同步配置。
在网关中引入 sofa 插件
当前版本,默认已经引入此依赖
- 在网关的
pom.xml文件中增加如下依赖:
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-all</artifactId>
<version>5.7.6</version>
<exclusions>
<exclusion>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-plugin-sofa</artifactId>
<version>${project.version}</version>
</dependency>
- 重启网关服务。
sofa服务接入网关
可以参考示例:shenyu-examples-sofa
springboot构建,引入以下依赖:
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
<version>${rpc-sofa-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-client-sofa</artifactId>
<version>${shenyu.version}</version>
</dependency>
- 在 application.yml 中配置
com:
alipay:
sofa:
rpc:
registry-address: zookeeper://127.0.0.1:2181 # consul # nacos
bolt-port: 8888
shenyu:
register:
registerType: http #zookeeper #etcd #nacos #consul
serverLists: http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848
props:
username: admin
password: 123456
client:
sofa:
props:
contextPath: /sofa
ipAndPort: sofa
appName: sofa
port: 8888
- 在 resources 目录下xml 文件中配置 sofa 服务暴露的服务接口
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sofa="http://sofastack.io/schema/sofaboot"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://sofastack.io/schema/sofaboot https://sofastack.io/schema/sofaboot.xsd"
default-autowire="byName">
<!-- 示例 sofa 接口 -->
<sofa:service ref="sofaSingleParamService" interface="org.apache.shenyu.examples.sofa.api.service.SofaSingleParamService">
<sofa:binding.bolt/>
</sofa:service>
<!-- 示例 sofa 接口 -->
<sofa:service ref="sofaMultiParamService" interface="org.apache.shenyu.examples.sofa.api.service.SofaMultiParamService">
<sofa:binding.bolt/>
</sofa:service>
</beans>
- 在接口上增加
@ShenyuSofaClient注解
@ShenyuSofaClient("/demo")
@Service
public class SofaClientMultiParamServiceImpl implements SofaClientMultiParamService {
@Override
@ShenyuSofaClient("/findByIdsAndName")
public SofaSimpleTypeBean findByIdsAndName(final List<Integer> ids, final String name) {
return new SofaSimpleTypeBean(ids.toString(), "hello world shenyu sofa param findByIdsAndName :" + name);
}
}
- 启动
sofa服务,成功注册后
- 进入后台管理系统的
插件列表 -> Proxy -> Sofa,会看到自动注册的选择器、规则信息。 - 进入后台管理系统的
基础配置-> 元数据管理,搜索appName(通过)可以看到元数据,每一个sofa接口方法,都会对应一条元数据。