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引用


        
            
                com.alipay.sofa
                sofaboot-dependencies
                2.4.5
                pom
                import
            
        
    

    
        
        
            com.alipay.sofa
            rpc-sofa-boot-starter
            5.4.4
        

        
            com.alipay.sofa
            healthcheck-sofa-boot-starter
            2.4.5
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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

(1)服务发布provider.xml




    
    
    
        
    

(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



    
    
        
    

(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

你可能感兴趣的:(RPC及Dubbo入门源码学习,RPC及Dubbo原理学习)