soul-admin
correctly and Setup Environment Ok。pom.xml
file:<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>sofa-rpc-all</artifactId> <version>5.7.6</version> </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.dromara</groupId> <artifactId>soul-spring-boot-starter-plugin-sofa</artifactId> <version>${last.version}</version> </dependency>
Springboot
<dependency> <groupId>org.dromara</groupId> <artifactId>soul-spring-boot-starter-client-sofa</artifactId> <version>${soul.version}</version> </dependency>
backend server register center config, please look:register center access.
Spring
<dependency> <groupId>org.dromara</groupId> <artifactId>soul-client-sofa</artifactId> <version>${project.version}</version> </dependency>
Add the following in the xml file of your bean definition:
<bean id ="sofaServiceBeanPostProcessor" class ="org.dromara.soul.client.sofa.SofaServiceBeanPostProcessor"> <constructor-arg ref="soulRegisterCenterConfig"/> </bean> <bean id="soulRegisterCenterConfig" class="org.dromara.soul.register.common.config.SoulRegisterCenterConfig"> <property name="registerType" value="http"/> <property name="serverList" value="http://localhost:9095"/> <property name="props"> <map> <entry key="contextPath" value="/your contextPath"/> <entry key="appName" value="your name"/> <entry key="isFull" value="false"/> </map> </property> </bean>
First in the soul-admin
plugin management, set the sofa
plugin to open.
Secondly, configure your registered address in the sofa
plugin, or the address of other registry.
{"protocol":"zookeeper","register":"127.0.0.1:2181"}
For your sofa service implementation class, add @SoulSofaClient annotation to the method,Indicates that the interface method is registered to the gateway.
Start your provider and output the log sofa client register success
. You’re done. Your sofa interface has been published to the soul gateway. If you still don’t understand, you can refer to the soul-test-sofa
project.
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 is 9195 ,/order is the contextPath 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 A,implements org.dromara.soul.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.
In Pair,left is the parameter type,and right is the parameter value. This is the standard for sofa generalization calls.
Register your class as a String bean and override the default implementation.
@Bean public SofaParamResolveService A() { return new A(); }