This document is intended to help the Sofa
service access the Apache ShenYu
gateway. The Apache ShenYu
gateway uses the Sofa
plugin to handle sofa
service.
Before the connection, start shenyu-admin
correctly, start Sofa
plugin, and add related dependencies on the gateway and Sofa
application client. Refer to the previous Quick start with Sofa .
For the use of the plugin, see:Sofa Plugin
For details about client access configuration, see Application Client Access Config .
For details about data synchronization configurations, see Data Synchronization Config .
In the current version, this dependency has been introduced by default.
pom.xml
file:<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>
Please refer to:shenyu-examples-sofa
springboot
project,Introduce the following dependencies:<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>
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
<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:service ref="sofaSingleParamService" interface="org.apache.shenyu.examples.sofa.api.service.SofaSingleParamService"> <sofa:binding.bolt/> </sofa:service> <sofa:service ref="sofaMultiParamService" interface="org.apache.shenyu.examples.sofa.api.service.SofaMultiParamService"> <sofa:binding.bolt/> </sofa:service> </beans>
@ShenyuSofaClient
annotation to the interface@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
service, and after successful registration:PluginList -> Proxy -> Sofa
in the backend management system, you will see the information of auto-registered selectors and rules.BasicConfig -> Metadata
and search by app name . You will see the metadata of sofa, each sofa
interface method, will correspond to a metadata.http
to request your sofa
service.contextPath
.For example, if you have an
order
service, it has an interface and its registration path/order/test/save
Now it's to request the gateway via post:
http://localhost:9195/order/test/save
Where
localhost:9195
is the IP port of the gateway, default port is9195
,/order
is thecontextPath
of your sofa access gateway configuration.
Parameter passing:
Single java bean parameter type (default)
Customize multi-parameter support:
In the gateway project you built, add a new class MySofaParamResolveService
, implements org.apache.shenyu.plugin.api.sofa.SofaParamResolveService
.
public interface SofaParamResolveService { /** * Build parameter pair. * this is Resolve http body to get sofa param. * * @param body the body * @param parameterTypes the parameter types * @return the pair */ Pair<String[], Object[]> buildParameter(String body, String parameterTypes); }
body
is the json string passed by body in http.parameterTypes
: list of matched method parameter types, If there are multiple, use ,
to separate.@Bean public SofaParamResolveService mySofaParamResolveService() { return new MySofaParamResolveService(); }