dubbo整合nacos

一,dubbo-nacos-common

描述:公共模块

1.依赖

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
			<version>1.18.18</version>
		</dependency>

2.接口

public interface OrderService {

    void  createOrder(Integer uid,Integer skuid,Integer skunum);
}

二,dubbo-nacos-provider

描述:服务提供者

1.依赖

<dependencies>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>com.yhd</groupId>
			<artifactId>dubbo-nacos-common</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba.nacos</groupId>
			<artifactId>nacos-client</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-registry-nacos</artifactId>
			<version>2.7.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.7.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.7.8</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2.配置文件

server:
    port: 8001
spring:
    application:
        name: service-prod
dubbo:
    # 配置服务信息
    application:
        name: service-prod
        # 禁用QOS同一台机器可能会有端口冲突现象
        qos-enable: false
        qos-accept-foreign-ip: false
    # 配置注册中心
    registry:
        address: nacos://localhost:8848
    # 设置协议-协议由提供方指定消费方被动接受
    protocol:
        name: dubbo
        port: 20880

3.主启动类

@EnableDubbo
@SpringBootApplication
public class DubboNacosProviderApplication {

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

}

4.实现类

import com.yhd.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Override
    public void createOrder(Integer uid, Integer skuid, Integer skunum) {
        log.info("createOrder()执行:参数为:"+uid+"..."+skuid+"..."+skunum);
    }
}

三,dubbo-nacos-consumer

描述:服务消费者

1.依赖

<dependencies>
		<dependency>
			<groupId>com.yhd</groupId>
			<artifactId>dubbo-nacos-common</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba.nacos</groupId>
			<artifactId>nacos-client</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-registry-nacos</artifactId>
			<version>2.7.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.7.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.7.8</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2.配置文件

server:
  port: 8002
dubbo:
  # 配置服务信息
  application:
    name: service-cons
    # 禁用QOS同一台机器可能会有端口冲突现象
    qos-enable: false
    qos-accept-foreign-ip: false
  # 配置注册中心
  registry:
    address: nacos://localhost:8848
  # 设置超时时间
  consumer:
    timeout: 4000
spring:
  application:
    name: service-cons


3.主启动类

@EnableDubbo
@SpringBootApplication
public class DubboNacosConsumerApplication {

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

}

4.controller

import com.yhd.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @DubboReference
    private OrderService orderService;

    @GetMapping("demo")
    public String createOrder(){
        orderService.createOrder(1,1,5);
        return "success";
    }
}

四,测试

1.访问接口

访问``localhost:8002/order/demo`
显示成功,控制台打印。

2.访问nacos控制台

访问localhost:8848/nacos
服务注册成功