蚂蚁金服SOFA整合RPC(Hello World)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liman65727/article/details/89315036

前言

算了,懒得写了,假装这里有个前言,关于SOFA的介绍还是看这里吧:SOFA官网

生产者

1、建立一个spring boot的项目——sofa_provider,引入pom文件如下

这里需要说明一下:根据官网的使用规范,需要替换掉原有的替换 spring-boot-starter-parent引用,改为 sofaboot-dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--替换了parent-->
    <parent>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>sofaboot-dependencies</artifactId>
        <version>2.5.0</version>
    </parent>

    <groupId>com.learn</groupId>
    <artifactId>sofa-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sofa-provider</name>
    <description>SOFAProvider</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alipay.sofa</groupId>
            <artifactId>rpc-sofa-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
            <version>0.10</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 2、配置相关属性,application.name必须要指定

#can't be deleted
spring.application.name=sofaProvider
#this application log level
logging.level.com.alipay.test=INFO
#zookeeper
com.alipay.sofa.rpc.registry.address=zookeeper://localhost:2181

3、测试业务类编写

 接口

package com.learn.sofa_provider.service;

/**
 * author:liman
 * createtime:2019/4/15
 */
public interface IHelloworldService {

    public String helloSOFA(String name);

}

 实现类

package com.learn.sofa_provider.service;

import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import org.springframework.stereotype.Component;

/**
 * author:liman
 * createtime:2019/4/15
 */
@SofaService(interfaceType = IHelloworldService.class,bindings = {@SofaServiceBinding(bindingType = "dubbo")})
@Component
public class HelloworldServiceImpl implements IHelloworldService {
    @Override
    public String helloSOFA(String name) {
        return "Hello " + name + " this is world of SOFA";
    }
}

 消费者实例编写完成。

api中间件

建立一个springboot的项目——sofa-api。这个就相当于一个jar包的功能,pom文件中修改为如下即可:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>sofaboot-dependencies</artifactId>
        <version>3.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.learn</groupId>
    <artifactId>sofa-api</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <!--<module>../sofa-consumer</module>-->
    </modules>

    <dependencies>
        <dependency>
            <groupId>com.learn</groupId>
            <artifactId>sofa-provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

 消费者

1、建立一个spring boot项目——sofa-consumer。pom文件和生产者相差不大,如下所示(注意:依旧需要替换掉spring boot项目自己生成的parent,同时需要引用api中间件)这里就不贴出来了,

2、编写业务类

package com.learn.sofaconsumer.controller;

import com.learn.sofaconsumer.service.HelloworldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * author:liman
 * createtime:2019/4/15
 */
@RestController
public class controller {

    @Autowired
    private HelloworldService helloworldService;

    @RequestMapping("/testSOFA")
    public String helloSOFA(@RequestParam String name){
        return helloworldService.sayHelloToSOFA(name);
    }
}
package com.learn.sofaconsumer.service;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.learn.sofa_provider.service.IHelloworldService;
import org.springframework.stereotype.Component;

/**
 * author:liman
 * createtime:2019/4/15
 */
@Component
public class HelloworldService {

    @SofaReference(interfaceType = IHelloworldService.class,binding = @SofaReferenceBinding(bindingType = "dubbo"))
    private IHelloworldService helloworldService;

    public String sayHelloToSOFA(String name){
        return helloworldService.helloSOFA(name);
    }

}

3、properties文件如下

server.port=8889
spring.application.name=sofaConsumer
com.alipay.sofa.rpc.registry.address=zookeeper://localhost:2181

运行示例

启动zookeeper,先启动生产者,然后启动消费者即可(api中间件不用启动)

浏览器输入相关地址,可以看到如下结果:

顺便截一张SOFA-boot的启动图片

 

总结

一些使用在SOFA的官网中比较详细了,其实SOFA貌似和dubbo有很多类似的地方,但是该组件貌似在金融圈内使用较多。

展开阅读全文

没有更多推荐了,返回首页