dubbo整合springboot例子

新建一个父项目,里面包含Api,Provide和Consume三个子项目。
Api:存放接口
Provide:实现接口
Consume:调用接口,返回的是Provide具体实现的内容

1.父项目maven配置:

    MyDemoProvide
    MyDemoServiceApi
    MyDemoServiceConsume


    org.springframework.boot
    spring-boot-starter-parent
    2.1.6.RELEASE
     

com.MyDemo
MyDemoService
1.0.0
pom

    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        javax.servlet
        javax.servlet-api
        3.1.0
        Provided
    
    
        org.apache.dubbo
        dubbo-spring-boot-starter
        2.7.3
    
    
        org.apache.dubbo
        dubbo
        2.7.3
        
            
                org.springframework
                spring-context
            
        
    
    
        org.apache.dubbo
        dubbo-dependencies-zookeeper
        2.7.3
        pom
        
            
                slf4j-log4j12
                org.slf4j
            
            
                log4j
                log4j
            
        
    




2.1 api项目maven配置:

    MyDemoService
    com.MyDemo
    1.0.0

4.0.0
MyDemoServiceApi
1.0.0

2.2 api项目写一个接口
public interface SayHello {
    public String sayHello();
}



3.1 Provide项目maven配置:

    MyDemoService
    com.MyDemo
    1.0.0

4.0.0
MyDemoProvide
jar

    
        com.MyDemo
        MyDemoServiceApi
        1.0.0
    


3.2 Provide项目实现SayHello接口:
@Service
@Component
public class SayHelloImp implements SayHello {
    @Override
    public String sayHello() {
        return "I am Provide";
    }
}

3.3 Provide项目启动类
@EnableDubbo
@EnableDubboConfig
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

3.4 Provide项目application.yml文件:
server:
  port: 8100
dubbo:
  application:
    name: MyDemoProvide
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181



4.1 Consume项目maven配置:

    MyDemoService
    com.MyDemo
    1.0.0

4.0.0
MyDemoServiceTest
jar

    
        com.MyDemo
        MyDemoServiceApi
        1.0.0
    


4.2 Consume项目调用SayHello接口,返回的是Provide项目具体实现的内容
@RestController
public class TestController {
    @Reference
    private SayHello sayHello;
    @RequestMapping(value = "/sayHello.do", method = RequestMethod.GET)
    public String sayHello(){
        return sayHello.sayHello();//实际运行会返回"I am Provide"
    }
}

4.3 Consume项目启动类
@EnableDubbo
@EnableDubboConfig
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

4.4 Consume项目application.yml文件:
server:
  port: 8200
dubbo:
  application:
    name: MyDemoConsume
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181

 

你可能感兴趣的:(dubbo)