微服务架构编码构建

在这里插入图片描述

一、搭建 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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
全套微服务架构,视频学习java微服务架构,包括如下 第1章 微服务简介 001构建单体应用 002微服务解决复杂问题 003微服务的优点 004微服务的缺点 第2章 Linux使用 005Linux 简介 006Linux 与 Windows 比较 007安装 Linux 008Linux 远程控制管理 009Linux 目录结构 010操作文件目录 011系统管理命令 012开关机命令 013压缩命令 014Linux 编辑器 015修改数据源 016常用 APT 命令 017Linux 用户和组管理 018查看目录和文件的权限 019更改操作权限 020安装 Java 021安装 Tomcat 022安装 MySQL 023部署项目 第3章 Docker实战开发 042设置镜像标签 024Docker 简介 025Docker 功能特点 026Docker 系统架构 027Docker 安装 028第一个 Docker 应用程序 029运行交互式的容器 030后台运行与停止容器 031Docker 客户端帮助命令 032运行 Web 容器 033指定 Web 容器映射端口 034查看容器进程 035查看容器状态 036批量移除容器 037Docker Hub 镜像仓库 038获取镜像 039查找镜像 040更新镜像 041创建和移除镜像 043安装 Tomcat 044安装 MySQL 045部署项目 046数据卷简介 047创建数据卷 048备份数据卷 049恢复数据卷 050Docker Compose-安装 051Docker Compose-使用 第4章 使用 GitLab 托管代码 055GitLab简介 056GitLab 安装 057GitLab 设置 058GitLab 账户管理 059GitLab 使用-HTTP 060GitLab 使用-SSH 第5章 Spring Boot 061Spring简史 062 Spring Boot 简介 063 第一个 Spring Boot 应用程序 064 Spring Boot 自定义 Banner 065 Spring Boot 配置 066 Spring Boot Starter POM 067 Spring Boot 日志配置 第6章 集成 MyBatis 068Thymeleaf简介 069 第一个 Thymeleaf 模板页 070 集成 Druid 数据源 071 集成 TkMyBatis 简化 MyBatis 操作 072 集成 PageHelper 分页插件 073 使用 MyBatis Maven Plugin 自动生成代码 074 集成 MyBatis-测试查询 第7章 项目实战 075项目简介 076 创建依赖管理项目 077 创建通用工具项目 078 创建数据库管理项目 079 创建领域模型项目 080 创建管理后台接口项目 081 创建管理后台实现项目 082 为什么要使用 Dubbo 083 再谈微服务-背景介绍 084 再谈微服务-面向服务架构微服务架构 085 再谈微服务-服务框架对比 1 085 再谈微服务-服务框架对比 2 086 再谈微服务-RPC 对比 REST 087 Dubbo 简介 088 Dubbo 服务治理 089 Dubbo 组件角色 090 Zookeeper 简介 091 Zookeeper 应用举例 092 Zookeeper 集群模式 093 Dubbo Admin 管理控制台 094 系统后台管理-修改所需依赖 095 服务提供者 096 服务消费者 097 测试 JRebel 热部署 098 登录页 099 首页1 099 首页2 100 使用 thymeleaf 模板 101 使用 iframe 展示功能页 102 频道管理功能-列表页布局 103 新增频道 104 频道列表 105 选择父级频道1 105 选择父级频道2 106 表单页的树控件 107 列表页的树表格 108 FastDFS 分布式文件系统简介 109 FastDFS 分布式文件系统安装 110 文章管理功能-改造树控件1 110文章管理功能-改造树控件2 111 文章管理功能-新增文章 112 文章管理功能-使用 PageHelper 进行分页查询 113 文章管理功能-前端 Datatable 控件分页处理1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小钟不想敲代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值