天天看點

SOFA原理學習--sofa boot入門學習

        目前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){

            }

        }

    }
}