Spring Boot配置Dubbo

2019-09-26  本文已影响0人  _52Hertz

搭建dubbo基础环境

目录结构如下图所示

父module:cateye_center_base
子module:cateye_center_order cateye_center_user
提供者:cateye_center_user
消费者:cateye_center_order

image.png

提供者服务

官网

<dependency>
    <groupId>com.alibaba.spring.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>
spring.application.name=dubbo-spring-boot-starter
spring.dubbo.server=true
spring.dubbo.registry=N/A
package com.evan;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration
public class CateyeCenterUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(CateyeCenterUserApplication.class, args);
    }

}
package com.evan.dubbo.api;

public interface ApiService {
    String sendMessage(String message);
}

ProviderServiceImpl

package com.evan.dubbo.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.evan.dubbo.api.ApiService;
import org.springframework.stereotype.Component;

@Component
@Service(interfaceClass = ApiService.class)
public class ProviderServiceImpl implements ApiService {

    @Override
    public String sendMessage(String message) {
        return "provider:"+message;
    }
}

消费者服务

spring.application.name=dubbo-spring-boot-starter
package com.evan;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import com.evan.dubbo.api.impl.ConsumerServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
@EnableDubboConfiguration
public class CateyeCenterOrderApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(CateyeCenterOrderApplication.class, args);

        ConsumerServiceImpl consumerServiceImpl = (ConsumerServiceImpl) context.getBean("consumerServiceImpl");
        consumerServiceImpl.sendMessage("hello");
    }
}
package com.evan.dubbo.api;

public interface ApiService {
    String sendMessage(String message);
}

ConsumerServiceImpl

package com.evan.dubbo.api.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.evan.dubbo.api.ApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class ConsumerServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(ConsumerServiceImpl.class);

    @Reference(url = "dubbo://localhost:20880")
    ApiService apiService;

    public void sendMessage(String message){
        logger.info(apiService.sendMessage(message));
    }
}

集成注册中心

前提是已经安装zookeeper并且启动

<dependency>
      <groupId>com.101tec</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.9</version>
</dependency>
<dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.9</version>
      <type>pom</type>
</dependency>
@Reference(interfaceClass = ApiService.class)
上一篇下一篇

猜你喜欢

热点阅读