Dubbo+Nacos做注册中心和配置中心

项目结构

相关代码

EchoService

public interface EchoService {
    String echo(String msg);
}

DefaultEchoService

@Service(version = "${echo.service.version}")
public class DefaultEchoService implements EchoService {


    @Value("${echo.service.name}")
    private String serviceName;

    @Override
    public String echo(String msg) {
        RpcContext rpcContext = RpcContext.getContext();
        return String.format("Service [name :%s , port : %d] %s say : Hello,%s",
                serviceName,
                rpcContext.getLocalPort(),
                rpcContext.getMethodName(),
                msg);
    }
}

EchoServiceConsumerBootstrap

/**
 * TODO
 *
 * @auther xh
 * @date 3/14/19 10:25 AM
 */
@EnableDubbo
@EnableNacosConfig
@PropertySource(value = "classpath:configs/consumer-config.properties")
public class EchoServiceConsumerBootstrap {


    @Reference(version = "${echo.service.version}")
    private EchoService echoService;

    @PostConstruct
    public void init() {
        for (int i = 0; i < 10; i++) {
            System.err.println(echoService.echo("hello "));
        }
    }

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(EchoServiceConsumerBootstrap.class);
        context.refresh();
        context.close();
    }
}

EchoServiceProviderBootstrap

/**
 * TODO
 *
 * @auther xh
 * @date 3/14/19 10:22 AM
 */
@EnableDubbo(scanBasePackages = "provider")
@PropertySource(value = "classpath:configs/provider-config.properties")
public class EchoServiceProviderBootstrap {
    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(EchoServiceProviderBootstrap.class);
        context.refresh();
        System.out.println("EchoService 启动...");
        System.in.read();
    }
}

pom.xml

...
   <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.22.RELEASE</version>
        </dependency>

        <!-- Dubbo dependency -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.6</version>
        </dependency>

        <!-- Spring Context Extras -->
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>1.0.2</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.0.35.Final</version>
        </dependency>

        <!-- Dubbo Nacos registry dependency -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>0.0.1</version>
        </dependency>

        <!-- Nacos Spring dependency -->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-spring-context</artifactId>
            <version>0.2.3-RC1</version>
        </dependency>
    </dependencies>
...

注册中心(配置文件在本地,注册中心指向Nocas)

consumer-config.properties:

## Dubbo Application info
dubbo.application.name=dubbo-consumer-demo
## Nacos registry address
dubbo.registry.address=nacos://127.0.0.1:8848
# @Reference version
echo.service.version=1.0.0

provider-config.properties:

## application
dubbo.application.name = dubbo-provider-demo
## Nacos registry address
dubbo.registry.address = nacos://127.0.0.1:8848
## Dubbo Protocol
dubbo.protocol.name = dubbo
dubbo.protocol.port = -1
# Provider @Service version
echo.service.version = 1.0.0
echo.service.name = EchoService

配置中心(配置文件也在Nacos上,注解中填写Nacos地址和配置文件名)

EchoServiceConsumerConfigBootstrap:

/**
 * TODO
 *
 * @auther xh
 * @date 3/18/19 10:23 AM
 */

@EnableDubbo
@EnableNacosConfig // 激活 Nacos 配置
@NacosPropertySource(dataId = "nacos-consumer-2.properties")
public class EchoServiceConsumerConfigBootstrap {

    static {
        System.setProperty("nacos.server-addr", "127.0.0.1:8848");
    }

    @Reference(version = "${echo.service.version}")
    private EchoService echoService;

    @NacosConfigListener(dataId = "nacos-consumer-2.properties")
    public void onChange(String properties) {
        System.out.println("onChange(String) : " + properties);
    }

    @PostConstruct
    public void init() {
        for (int i = 0; i < 10; i++) {
            System.err.println(echoService.echo("hello "));
        }
    }

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(EchoServiceConsumerConfigBootstrap.class);
        context.refresh();
        System.out.println("服务消费者已启动...");
        System.in.read();
        context.close();
    }

}

EchoServiceProviderConfigBootstrap:

/**
 * TODO
 *
 * @auther xh
 * @date 3/18/19 10:23 AM
 */

@EnableDubbo(scanBasePackages = "provider")
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848")) // 激活 Nacos 配置
@NacosPropertySource(dataId = "nacos-provider-2.properties")
public class EchoServiceProviderConfigBootstrap {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(EchoServiceProviderConfigBootstrap.class);
        context.refresh();
        System.out.println("EchoService 启动...");
        System.in.read();
    }
}

Nacos 配置(内容和本地的一致)

猜你喜欢

转载自www.cnblogs.com/lanqie/p/10552500.html