目前Spring boot可以说是Spring web最为流行的开发框架了,目前很多框架都支持Spring boot注册扫描的模式,这篇博客我们用一个示例来展示一下sofa-boot简单的开发工程。

1、创建API中相关接口及实现类

接口:

public interface IHelloService {

public String sayHello(String name);
}

实现类:

public class HelloServiceImpl implements IHelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}

2、sofa boot相关pom引用

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>2.4.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- 引入 SOFARPC 依赖 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
<version>5.4.4</version>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>healthcheck-sofa-boot-starter</artifactId>
<version>2.4.5</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

3、sofa boot服务提供者相关配置

(1)服务发布provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
http://sofastack.io/schema/sofaboot.xsd"
default-autowire="byName">

<bean id="helloService" class="com.tianjunwei.HelloServiceImpl"/>
<!--对外暴露服务-->
<sofa:service interface="com.tianjunwei.IHelloService" ref="helloService">
<sofa:binding.bolt/>
</sofa:service>
</beans>

(2)application.properties相关配置

# 指定应用名
spring.application.name=sofa-provider

server.port=8091
# 指定日志路径
logging.path=./logs
# 注册中心地址
com.alipay.sofa.rpc.registry.address=zookeeper://127.0.0.1:2181

(3)服务启动类

@SpringBootApplication
@ImportResource("provider.xml")
public class ProviderBootStrap {

public static void main(String args[]){
ApplicationContext context = SpringApplication.run(ProviderBootStrap.class,args);
}
}

4、服务消费者相关配置(与服务提供者类似)

(1)服务引用consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
http://sofastack.io/schema/sofaboot.xsd"
default-autowire="byName">
<!--引用服务-->
<sofa:reference id="helloService" interface="com.tianjunwei.IHelloService">
<sofa:binding.bolt/>
</sofa:reference>

</beans>

(2)application.properties中相关配置

# 指定应用名
spring.application.name=sofa-client

server.port=8090
# 指定日志路径
logging.path=./logs
# 注册中心地址
com.alipay.sofa.rpc.registry.address=zookeeper://127.0.0.1:2181

(3)服务启动类:

@SpringBootApplication
@ImportResource("consumer.xml")
public class ConsumerBootStrap {

public static void main(String args[]){
ApplicationContext context = SpringApplication.run(ConsumerBootStrap.class,args);
IHelloService helloService =(IHelloService) context.getBean("helloService");
while (true) {
System.out.println(helloService.sayHello("sofa"));
try{
Thread.sleep(1000);
}catch (Exception e){

}

}

}
}

5、代码地址:

​sofa-rpc-demo​