springboot+dubbo+nacos整合

1、创建maven项目

dubbo-nacos-demo

2、父工程引入依赖


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-nacos-demoartifactId>
        <groupId>com.zmqgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>com.zmqgroupId>
    <artifactId>dubbo-provideartifactId>
    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <exclusions>
                
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubboartifactId>
        dependency>

        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-registry-nacosartifactId>
            <version>2.7.3version>
        dependency>
        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-clientartifactId>
        dependency>
    dependencies>
    <repositories>
        <repository>
            <id>apache.snapshots.httpsid>
            <name>Apache Development Snapshot Repositoryname>
            <url>https://repository.apache.org/content/repositories/snapshotsurl>
            <releases>
                <enabled>falseenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
    repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

3、创建子工程dubbo-provide

引入依赖


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-nacos-demoartifactId>
        <groupId>com.zmqgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>com.zmqgroupId>
    <artifactId>dubbo-provideartifactId>
    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <exclusions>
                
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubboartifactId>
        dependency>

        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-registry-nacosartifactId>
            <version>2.7.3version>
        dependency>
        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-clientartifactId>
        dependency>
    dependencies>
    <repositories>
        <repository>
            <id>apache.snapshots.httpsid>
            <name>Apache Development Snapshot Repositoryname>
            <url>https://repository.apache.org/content/repositories/snapshotsurl>
            <releases>
                <enabled>falseenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
    repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

新建配置文件application.yml

nacos:
  config:
    server-addr: localhost:8848
spring:
  application:
    name: dubbo-provide
dubbo:
  application:
    name: ${spring.application.name}
  registry:
    address: nacos://${nacos.config.server-addr}
  scan:
    base-packages: com.zmq.service
  protocol:
    name: dubbo
    port: 20881

创建启动类

package com.zmq;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class DubboProvideApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProvideApplication.class);
    }
}

创建service接口

package com.zmq.service;

public interface ProvideService {
    String sayHello(String word);
}

创建service接口实现类,注意这里的service注解是dubbo的注解

package com.zmq.service.impl;

import com.zmq.service.ProvideService;
import org.apache.dubbo.config.annotation.Service;

@Service
public class ProvideImpl implements ProvideService {
    public String sayHello(String word) {
        return word;
    }
}

3、创建dubbo-consumer模块

引入依赖


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-nacos-demoartifactId>
        <groupId>com.zmqgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>com.zmqgroupId>
    <artifactId>dubbo-consumerartifactId>
    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubboartifactId>
        dependency>

        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-registry-nacosartifactId>
            <version>2.7.3version>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-spring-contextartifactId>
            <version>0.3.1version>
        dependency>
        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-clientartifactId>
        dependency>
        <dependency>
            <groupId>com.zmqgroupId>
            <artifactId>dubbo-provideartifactId>
            <version>1.0-SNAPSHOTversion>
            <scope>compilescope>
        dependency>
    dependencies>
    <repositories>
        <repository>
            <id>apache.snapshots.httpsid>
            <name>Apache Development Snapshot Repositoryname>
            <url>https://repository.apache.org/content/repositories/snapshotsurl>
            <releases>
                <enabled>falseenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
    repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

创建application.yml配置文件

server:
  address:
  port: 8081
spring:
  application:
    name: dubbo-consumer
nacos:
  config:
    server-addr: localhost:8848
dubbo:
  registry:
    address: nacos://${nacos.config.server-addr}
  application:
    name: ${spring.application.name}

创建启动类

package com.zmq;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class dubboConsumerApplication {

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

创建controller调用服务

package com.zmq.controller;

import com.zmq.service.ProvideService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {
    @Reference
    ProvideService provideService;

    @RequestMapping("sayHello")
    public String sayHello(){
        return provideService.sayHello("我爱你");
    }
}

4、浏览器查看结果

在浏览器输入localhost:8081/sayHello,即可看到输出的话
在这里插入图片描述

5、启动nacos后可在服务列表看到注册的服务

springboot+dubbo+nacos整合_第1张图片
项目源码:https://github.com/xiaohuoban00/springboot-dubbo-nacos

你可能感兴趣的:(springboot+dubbo+nacos整合)