当前位置:   article > 正文

Dubbo整合springboot无注册中心_springboot没有注册中心

springboot没有注册中心

Springboot 2.3.9

Dubbo :3.0.0

一、服务端:

Maven引入

    <dependency>

        <groupId>org.apache.dubbo</groupId>

        <artifactId>dubbo-spring-boot-starter</artifactId>

        <version>3.0.0</version>

    </dependency>

    <!--不引人下面包时会抛异常 -->

    <dependency>

        <groupId>org.apache.curator</groupId>

        <artifactId>curator-recipes</artifactId>

        <version>2.13.0</version>

    </dependency>

Application.properties配置:

#dubbo服务名称

dubbo.application.name=boot-user-service-provider

#注册中心位置(N/A表示无注册中心

dubbo.registry.address=N/A

#通信规则协议

dubbo.protocol.name=dubbo

#通信端口

dubbo.protocol.port=20880

#扫描位置

#dubbo.scan.base-packages=com.xuanfeng.code.service

定义接口:

public interface DubboDemoService {

    public String sayHello(String param1);

}

接口实现类:

import org.apache.dubbo.config.annotation.DubboService;

//dubbo服务注解

@DubboService

public class DubboDemoServiceImpl implements DubboDemoService{

    @Override

    public String sayHello(String param1) {

        System.out.println("aaaaaa----------------");

        System.out.println(param1);

        return "haha";

    }

}

启动类代码:

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication

@EnableScheduling

//扫描dubbo服务

@EnableDubbo

@EnableConfigurationProperties(Configs.class)

public class Application {

    public static void main(String[] args) {

        try {

            SpringApplication.run(Application.class, args);        

        } catch (Exception e) {

            e.printStackTrace();

            System.exit(1);

        }

    }

}

二、客户端

Maven引入

    <dependency>

        <groupId>org.apache.dubbo</groupId>

        <artifactId>dubbo-spring-boot-starter</artifactId>

        <version>3.0.0</version>

    </dependency>

Application.properties配置:

dubbo.application.name=dubbo-consumer

#不验证服务是否存在

dubbo.consumer.check=false

定义接口:

public interface DubboDemoService {

    public String sayHello(String param1);

}

注:接口路径必须与服务端的保持一致

接口调用(以下写个自动作业测试):

import org.apache.dubbo.config.annotation.DubboReference;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Service;

@Service

public class AutoJob{

   //配置url可直连服务,绕过注册中心(旧版用@Reference

   @DubboReference(url = "dubbo://127.0.0.1:20880")

   private DubboDemoService dubboDemoService;

   @Scheduled(cron = "0/10 * * * * ?")

   public void test() {

      dubboDemoService.sayHello("哈啊哈ssssssss");

   }

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/284126
推荐阅读
相关标签
  

闽ICP备14008679号