第一个Dubbo程序

1.新建api接口

/**
 * API接口,provider和consumer都要去引入这个接口
 */
public interface DemoDubboService {
    public String demo(String param);
}

2.新建provider

  • pom依赖

    <parent>
    	<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>   
    
  • application.yml

    dubbo:
      application:
        name: dubbo-provider
      registry:
        address: zookeeper://127.0.0.1:2181
    
  • 实现接口

    import com.msb.dubbo.service.DemoDubboService;
    import org.apache.dubbo.config.annotation.Service;
    
    /**
     * 此类为接口的实现类,
     * Service注解为apache的@Service.此注解是区分项目是Provider还是Consumer最重要依据
     */
    @Service
    public class DemoDubboServiceImpl implements DemoDubboService {
        public String demo(String param) {
            System.out.println("执行demo");
            return param + "123";
        }
    }
    
  • 启动类

    import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * 必须要有@EnableDubbo注解,否则Dubbo不生效。
     */
    @SpringBootApplication
    @EnableDubbo
    public class ProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class, args);
        }
    }
    

3.新建consumer

  • pom依赖

    <parent>
    	<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>   
    
  • application.yml

    dubbo:
      application:
        name: dubbo-cunsumer
      registry:
        address: zookeeper://127.0.0.1:2181
    
  • Service类

    import com.msb.dubbo.service.DemoDubboService;
    import org.apache.dubbo.config.annotation.Reference;
    import org.springframework.stereotype.Service;
    
    /**
     * 调用服务使用@Reference注解org.apache.dubbo.config.annotation.Reference;
     */
    @Service
    public class DemoService{
    
        @Reference
        private DemoDubboService demoDubboService;
    
        public String demo() {
            return demoDubboService.demo("张三丰");
        }
    }
    
  • Controller类

    import com.msb.service.DemoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class DemoController {
    
        @Autowired
        private DemoService demoService;
    
        @RequestMapping("/demo")
        @ResponseBody
        public String demo() {
            return demoService.demo();
        }
    }
    
  • 启动类

    import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @EnableDubbo
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    }
    
上一篇:Spring Cloud认知学习(一)--Eureka使用、Ribbon使用


下一篇:Python数据可视化交互基本