dubbo-spring-boot-starter使用指南

https://github.com/apache/incubator-dubbo-spring-boot-project.git

dependencies

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>

千萬不要使用以下依賴(已廢棄)

<dependency>
    <groupId>com.alibaba.spring.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

另外,如果你的公共包如xx-common也引用了dubbo,請在xx-common里加上<optional>true</optional>

provider

application.yml

dubbo:
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
    id: my-registry
  protocol:
    port: 20882
    name: dubbo
    status: server
    id: dubbo
  application:
    name: demo-provider
    id: demo-provider
    qosEnable: true
    qosPort: 22223
  scan:
    basePackages: org.hongxi.whatsmars.dubbo.demo.provider.service

provider

@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {

    @Autowired
    private UserService userService;

    public String sayHello(String name) {
        boolean registerSuccess = userService.register(name);
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
    
}

consumer

application.yml

dubbo:
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
    id: my-registry
  protocol:
    port: 20882
    name: dubbo
    id: dubbo
  application:
    name: demo-consumer
    id: demo-consumer
    qosEnable: true
    qosPort: 22224

consumer

/**
 * Created by javahongxi on 2017/12/4.
 * 將服務調用包裝,可以方便進行調用監控
 */
@Component
public class DemoRpc {

    @Reference(version = "1.0.0")
    private DemoService demoService;

    public String sayHello(String name) {
        String result = null;
        try {
            result = demoService.sayHello(name);
        } catch (Exception e) {
            // log
            e.printStackTrace();
        }
        return result;
    }

}

調用多個註冊中心的服務

application.yml

dubbo:
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
    id: my-registry
  protocol:
    port: 20882
    name: dubbo
    id: dubbo
  application:
    name: demo-consumer
    id: demo-consumer
    qosEnable: true
    qosPort: 22224
registry:
  other:
    protocol: zookeeper
    address: 127.0.0.1:2181
    id: other
@Configuration
public class DubboConfig {

    /**
     * 配置文件裏配置默認的,這裏配置其他需要的
     */
    @Bean("otherRegistry")
    public RegistryConfig otherRegistry(@Value("${registry.other.protocol}") String protocol,
                                        @Value("${registry.other.address}") String address,
                                        @Value("${registry.other.id}") String id) {
        RegistryConfig registry = new RegistryConfig();
        registry.setProtocol(protocol);
        registry.setAddress(address);
        registry.setId(id);
        return registry;
    }
}
/**
 * Created by javahongxi on 2017/12/4.
 * 將服務調用包裝,可以方便進行調用監控
 */
@Component
public class DemoRpc {

    @Reference(version = "1.0.0")
    private DemoService demoService;

    @Reference(version = "1.0.0", registry = "otherRegistry")
    private OtherService otherService;

    public String sayHello(String name) {
        String result = null;
        try {
            result = demoService.sayHello(name);
        } catch (Exception e) {
            // log
            e.printStackTrace();
        }
        return result;
    }

    public String sayHello2(String name) {
        String result = null;
        try {
            result = otherService.sayHello(name);
        } catch (Exception e) {
            // log
            e.printStackTrace();
        }
        return result;
    }
}

發佈服務到多個註冊中心

@Service(
        version = "1.0.0",
        registry = {"my-registry", "otherRegistry"}
)
public class OtherServiceImpl implements OtherService {

    @Autowired
    private UserService userService;

    public String sayHello(String name) {
        boolean registerSuccess = userService.register(name);
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
    
}

PS: Dubbo優雅停機應該 kill -> sleep -> kill -9,切勿直接kill -9

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章