nacos入门-使用nacos实现一个最简单的生产者消费者模型

一.创建项目

项目总体结构

项目结构如下:

nacos入门-使用nacos实现一个最简单的生产者消费者模型

其中, order-service为订单服务, pay-service为支付服务.

pay-service做生产者, order-service做消费者, order-service将会调用pay-service服务.

nacos-demo的pom.xml文件内容如下:
<?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.forest.xs.sfg</groupId>
    <artifactId>nacos-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>

    <modules>
        <module>order-service</module>
        <module>pay-service</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>

</project>

pay-service支付服务结构和代码(生产者)

结构

nacos入门-使用nacos实现一个最简单的生产者消费者模型

pay-service的pom.xml文件内容:
<?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>nacos-demo</artifactId>
        <groupId>com.forest.xs.sfg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>pay-service</artifactId>

    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>

</project>
pay-service的application.yml文件内容:
server:
  port: 8080
spring:
  application:
    name: pay-service
  cloud:
    nacos:
      discovery:
        server-addr: 148.70.65.31:8848 # 此处写自己的nacos服务ip与端口
        namespace: forest-nacos-demo-zhoujl
        group: DEMO
pay-service的启动类(PayServiceApplication.java)代码:
package com.forest.xs.sfg;

/**
 * @Author:周建林
 * @Time:2021/3/7 0:39
 * @Description
 */

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

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

pay-service的业务类代码(PayController.java):
package com.forest.xs.sfg.payservice;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:周建林
 * @Time:2021/3/7 0:39
 * @Description
 */
@RestController
@RequestMapping("/payService")
public class PayController {

    @GetMapping("/pay/{money}")
    public String pay(@PathVariable Double money) {
        return String.format("<span style='color: #00F; font-size: 100px;'>I am payService.</br>I am paying...</br>Pay succefully! %s$.</span>", money);
    }
}

order-service订单服务结构和代码(消费者)

结构

nacos入门-使用nacos实现一个最简单的生产者消费者模型

order-service的pom.xml文件内容:
<?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>nacos-demo</artifactId>
        <groupId>com.forest.xs.sfg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-service</artifactId>

    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>

</project>
order-service的application.yml文件内容:
server:
  port: 9090
spring:
  application:
    name: order-service
  cloud:
    nacos:
      discovery:
        server-addr: 148.70.65.31:8848
        namespace: forest-nacos-demo-zhoujl
        group: DEMO
order-service的启动类(OrderServiceApplication.java)代码:
package com.forest.xs.sfg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @Author:周建林
 * @Time:2021/3/7 0:49
 * @Description
 */
@SpringBootApplication
public class OrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

order-service的业务类代码(PayController.java):
package com.forest.xs.sfg.orderservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author:周建林
 * @Time:2021/3/7 0:48
 * @Description
 */
@RestController
@RequestMapping("/orderService")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    private static final String PAY_SERVICE_NAME = "pay-service";
    private static final String PAY_URL = "/payService/pay";

    @GetMapping("/payOrder/{money}")
    public Object payOrder(@PathVariable Double money) {
        String url = new StringBuilder("http://").append(PAY_SERVICE_NAME).append(PAY_URL).append("/").append(money).toString();
        return restTemplate.getForObject(url, String.class);
    }
}


二.启动生产者和消费者

如图所示, 启动成功后, 两个服务都注册到了nacos:

nacos入门-使用nacos实现一个最简单的生产者消费者模型

三.浏览器请求订单服务查看效果

nacos入门-使用nacos实现一个最简单的生产者消费者模型

上一篇:浅析微信支付:(余额提现)企业付款到微信用户零钱或银行卡账户


下一篇:微信支付-基础支付学习资料整理