nacos集成dubbo实现远程服务调用多服务端2

文章目录

一、版本选取、需求和项目简述
1. 版本选取
框架 版本 说明
spring-cloud-alibaba 2.2.6.RELEASE 版本要对应
spring-boot 2.3.2.RELEASE 版本要对应
nacos 1.4.2 版本要对应
org.apache.dubbo 2.7.8 版本要对应
2. 项目模块说明
模块 说明
EShopParent 父工程
DubboApi 接口子模块
Order-serv 订单模块
Stock-serv 扣库存模块
product-serv 产品模块
2. 需求说明

订单模块调用扣库存模块完成库库存的业务

二、需求实战-依赖初始化
2.1. 创建maven父工程EShopParent
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gblfy</groupId>
    <artifactId>EShopParent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Order-serv</module>
        <module>Stock-serv</module>
        <module>DubboApi</module>
        <module>product-serv</module>
    </modules>
    <description>父工程 所有子工程需要依赖此工程</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>

    <dependencies>
        <!--springMVC启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--nacos服务发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--dubbo组件-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <!--spring-cloud-alibaba依赖版本控制-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
2.2. 创建子模块DubboApi
<?xml version="1.0" encoding="UTF-8"?>
<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>EShopParent</artifactId>
        <groupId>com.gblfy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>DubboApi</artifactId>

</project>
2.3. 创建服务端Stock-serv
<?xml version="1.0" encoding="UTF-8"?>
<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>EShopParent</artifactId>
        <groupId>com.gblfy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Stock-serv</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.gblfy</groupId>
            <artifactId>DubboApi</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>
2.4. 创建服务端product-serv
<?xml version="1.0" encoding="UTF-8"?>
<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>EShopParent</artifactId>
        <groupId>com.gblfy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>product-serv</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.gblfy</groupId>
            <artifactId>DubboApi</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
2.5. 创建消费端端Order-serv
<?xml version="1.0" encoding="UTF-8"?>
<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>EShopParent</artifactId>
        <groupId>com.gblfy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.gblfy</groupId>
    <artifactId>Order-serv</artifactId>
    <modelVersion>4.0.0</modelVersion>

    <dependencies>
        <dependency>
            <groupId>com.gblfy</groupId>
            <artifactId>DubboApi</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project>
三、需求实战-代码编写
3.1. 创建公共接口

在DubboApi子模块中创建调用扣库存模块(Stock-serv)的接口IStockService

package com.gblfy.stock.api;

public interface IStockService {
    public String reduce(Integer productId, Integer userId);
}

在DubboApi子模块中创建调用产品(product-serv)模块的接口

package com.gblfy.product.api;

public interface IProductService {
    public String buyProduct(Integer productId, Integer userId);
}
3.2. 扣库存服务端编写

在Stock-serv子模块中实现接口实现类StockServiceimpl

package com.gblfy.service.impl;

import com.gblfy.stock.api.IStockService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class StockServiceimpl implements IStockService {

    @Override
    public String reduce(Integer productId, Integer userId) {
        return "用户编号:   " + userId + "产品编码:   " + productId + "减库存1个";
    }
}

启动类上添加@EnableDiscoveryClient注解

package com.gblfy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class StockApplication {
    public static void main(String[] args) {
        SpringApplication.run(StockApplication.class);
    }
}
3.3. 产品服务端编写

在product-serv子模块中实现接口实现类ProductServiceImpl

package com.gblfy.service.impl;

import com.gblfy.product.api.IProductService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class ProductServiceImpl implements IProductService {

    @Override
    public String buyProduct(Integer productId, Integer userId) {
        return "用户编号:   " + userId + "产品编码:   " + productId + "购买PHONE SUCCESS";
    }
}

启动类添加@EnableDiscoveryClient注解

package com.gblfy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ProductApplication {

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

3.5. 消费端编写

在Order-serv子模块的启动类上添加@EnableDiscoveryClient开启服务发现扫描

package com.gblfy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@SpringBootApplication
@EnableDiscoveryClient
public class OrderAppliaction {

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


创建一个客户端类发起请求OrderController分别向扣库存模块和产品模块发起请求

package com.gblfy.controller;

import com.gblfy.product.api.IProductService;
import com.gblfy.stock.api.IStockService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class OrderController {

    @DubboReference
    private IStockService stockService;
    @DubboReference
    private IProductService productService;

    @GetMapping("/order/create")
    public String createOrder(Integer productId, Integer userId) {
        //调用扣库存模块服务
        return stockService.reduce(productId, userId);
    }
    //http://127.0.0.1:8080/order/create?productId=2&userId=8

    @GetMapping("/buyProduct")
    public String buyProduct(Integer productId, Integer userId) {
        //调用产品模块服务
        return productService.buyProduct(productId, userId);
    }
    // http://127.0.0.1:8080/buyProduct?productId=2&userId=8
}
四、需求实战-配置编写
4.1. 扣库存服务端配置
# 应用端口
server:
  port: 8082

# nacos服务发现配置
spring:
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        service: stock-serv
  application:
    name: stock-serv

# Dubbo服务配置
dubbo:
  scan:
    base-packages: com.gblfy.service.impl
  protocol:
    name: dubbo
    port: -1
  registry:
    address: spring-cloud://127.0.0.1
4.2. 产品服务端配置
server:
  port: 8081
spring:
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        service: product-serv
  application:
    name: product-serv

# Dubbo服务配置
dubbo:
  scan:
    base-packages: com.gblfy.service.impl
  protocol:
    name: dubbo
    port: -1
  registry:
    address: spring-cloud://127.0.0.1
4.3.消费端配置
server:
  port: 8080
spring:
  application:
    name: order-serv
  cloud:
    nacos:
      discovery:
        server-addr: http://127.0.0.1:8848
      config:
        server-addr: http://127.0.0.1:8848
        
# Dubbo服务配置
dubbo:
  protocol:
    name: dubbo
    port: -1
  registry:
    address: spring-cloud://127.0.0.1
  cloud:
    subscribed-services:
      - stock-serv
      - product-serv

五、需求测试实战
5.1. 启动nacos

nacos集成dubbo实现远程服务调用多服务端2
nacos集成dubbo实现远程服务调用多服务端2

5.2. 启动服务端

分别依次启动扣库存服务端和产品服务端
nacos集成dubbo实现远程服务调用多服务端2
nacos集成dubbo实现远程服务调用多服务端2

5.3. 启消费端

nacos集成dubbo实现远程服务调用多服务端2

5.4. 查看nacos

nacos集成dubbo实现远程服务调用多服务端2

5.5. 项目模块分布结构

nacos集成dubbo实现远程服务调用多服务端2

六、测试实战
6.1. 请求扣库存链路

http://127.0.0.1:8080/order/create?productId=2&userId=8
nacos集成dubbo实现远程服务调用多服务端2
nacos集成dubbo实现远程服务调用多服务端2
nacos集成dubbo实现远程服务调用多服务端2

6.2. 请求产品链路

http://127.0.0.1:8080/buyProduct?productId=2&userId=8

nacos集成dubbo实现远程服务调用多服务端2
nacos集成dubbo实现远程服务调用多服务端2
nacos集成dubbo实现远程服务调用多服务端2

6.3. 常见的异常

没有服务的提供者,启动客户端
nacos集成dubbo实现远程服务调用多服务端2

上一篇:网络网络:西西弗斯 路由代码解读


下一篇:11g修改listner ADR