当前位置: 首页 > article >正文

微服务架构编码构建

在这里插入图片描述

一、搭建 Provider 和 Consumer 服务

1、父工程 spring-cloud-parent

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.liming</groupId>
    <artifactId>spring-cloud-parent</artifactId>
    <version>1.0.0</version>

    <!--spring boot 环境 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.11.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
</project>

2、提供者 eureka-provider

在这里插入图片描述

搭建springboot工程:

  1. pom 导包
  2. 配置文件
  3. 主启动类

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>spring-cloud-parent</artifactId>
        <groupId>com.liming</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-provider</artifactId>

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

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

</project>

application.yml

server:
  port: 8000

主启动类 ProviderApp

package com.liming.provider;

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

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

Goods

package com.liming.provider.domain;

import java.io.Serializable;

public class Goods implements Serializable {
    private int id;//商品id
    private String title;//商品名
    private double price;//价格
    private int count;//库存

    public Goods(int id, String title, double price, int count) {
        this.id = id;
        this.title = title;
        this.price = price;
        this.count = count;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", price=" + price +
                ", count=" + count +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

GoodsController

package com.liming.provider.controller;

import com.liming.provider.domain.Goods;
import com.liming.provider.service.GoodsService;
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;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @GetMapping("findById/{id}")
    public Goods findById(@PathVariable("id") int id){
        Goods goods = goodsService.findById(id);
        return goods;
    }
}

GoodsService

package com.liming.provider.service;

import com.liming.provider.dao.GoodsDao;
import com.liming.provider.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsService {
    @Autowired
    GoodsDao goodsDao;

    public Goods findById(int id){
        Goods goods = goodsDao.findById(id);
        return goods;
    }
}

GoodsDao

package com.liming.provider.dao;

import com.liming.provider.domain.Goods;
import org.springframework.stereotype.Repository;

@Repository
public class GoodsDao {
    public Goods findById(int id){
        //查数据库
        return new Goods(id,"手机",2000,100);
    }
}

测试:
访问:http://localhost:8000/goods/findById/1

在这里插入图片描述

2、消费者 eureka-consumer

在这里插入图片描述

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>spring-cloud-parent</artifactId>
        <groupId>com.liming</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-consumer</artifactId>


    <dependencies>

        <!--spring boot web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
</project>

Goods

package com.liming.consumer.domain;

/**
 * 商品实体类
 */
public class Goods {

    private int id;
    private String title;//商品标题
    private double price;//商品价格
    private int count;//商品库存

    public Goods() {
    }

    public Goods(int id, String title, double price, int count) {
        this.id = id;
        this.title = title;
        this.price = price;
        this.count = count;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

OrderController

package com.liming.consumer.controller;

import com.liming.consumer.domain.Goods;
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;

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

    @GetMapping("/add/{id}")
    public Goods add(@PathVariable("id") Integer id){
        //业务逻辑
        //1查询商品
        //2减库存
        //3支付
        //4物流

        return new Goods();
    }
}

application.yml

server:
  port: 9000

测试:http://localhost:9000/order/add/2

在这里插入图片描述

二、使用 RestTemplate 完成远程调用

  • Spring提供的一种简单便捷的模板类,用于在 java 代码里访问 restful 服务
  • 其功能与 HttpClient 类似,但是 RestTemplate 实现更优雅,使用更方便

consumer工程中

RestTemplateConfig

package com.liming.consumer.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {


    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

OrderController

package com.liming.consumer.controller;


import com.liming.consumer.domain.Goods;
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;

/**
 * 服务的调用方
 */

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

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("add/{id}")
    public Goods add(@PathVariable("id") int id){
        /*
            //远程调用Goods服务中的findById接口
            使用RestTemplate
            1. 定义Bean  restTemplate
            2. 注入Bean
            3. 调用方法
         */

        String url = "http://localhost:8000/goods/findById/"+id;
        // 3. 调用方法
        Goods goods = restTemplate.getForObject(url, Goods.class);
        return goods;
    }
}

http://www.kler.cn/a/16030.html

相关文章:

  • 【高危】Apache Superset <2.1.0 认证绕过漏洞(POC)(CVE-2023-27524)
  • linux常用命令大全(保姆及入门)
  • main.m文件解析--@autoreleasepool和UIApplicationMain
  • C++构造函数和析构函数(附示例代码)
  • 封装建立-SMD封装
  • springboot网上商城项目(一)
  • 停车场管理系统的设计与实现_kaic
  • 大学本科毕业选择继续考研,还是去做程序员?我来聊聊看法
  • Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】(二)
  • VirboxLM-免服务版授权码,快速实现一机一码
  • 《使用深度神经网络对光电容积脉搏图进行归一化,以进行个体和群体比较》阅读笔记
  • 每天一道算法练习题--Day14 第一章 --算法专题 --- -----------大话搜索
  • 6.hashcode与equals区别与联系
  • 产业数字化爆发,松山湖开发者村打通数实融合“最后一公里”
  • 【地铁上的设计模式】--结构型模式:组合模式
  • 2023五一数学建模B题完整思路
  • 深入探究C++中的仿函数和迭代器——提升你的STL技能
  • SDKJ_JD 服务器部署
  • 解决Windows下QtCreator编译代码时错误:cc1plus.exe: out of memory allocating 65536 bytes
  • MCAL知识点(二十四):WDG MCAL驱动配置详解