首页 > 其他分享 >02-SpringCloud 学前准备

02-SpringCloud 学前准备

时间:2023-03-05 18:45:45浏览次数:42  
标签:02 SpringCloud org springcloud public atguigu import com 学前

学习版本

image-20230304101908377

创建父工程

父工程pom文件

创建父工程后mvn install发布到本地仓库供子工程继承。

Maven 的dependencyManagement 和dependencies

微服务服务提供者

创建cloud-provider-payment8001微服务提供者支付Module模块。

创建新模块主要步骤:

  1. 建module
  2. 改POM
  3. 写YML
  4. 主启动
  5. 业务类

POM文件

<?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>mscloud03</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8001</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

application.yml

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包 com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456


mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包

主启动类

package com.atguigu.springcloud;

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

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

业务类

entities

Payment:

package com.atguigu.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @auther zzyy
 * @create 2020-01-27 20:01
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable
{
    private Long id;
    private String serial;
}

Json封装体CommonResult:

package com.atguigu.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @auther zzyy
 * @create 2020-01-27 20:17
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T>
{
    private Integer code;
    private String  message;
    private T data;

    public CommonResult(Integer code, String message)
    {
        this(code,message,null);
    }
}

dao

PaymentDao:

package com.atguigu.springcloud.dao;

import com.atguigu.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * @auther zzyy
 * @create 2020-01-27 20:21
 */
@Mapper  //import org.apache.ibatis.annotations.Mapper;
public interface PaymentDao
{
    public int create(Payment payment);

    public Payment getPaymentById(@Param("id") Long id);
}

service

PaymentService

package com.atguigu.springcloud.service;

import com.atguigu.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;

/**
 * @auther zzyy
 * @create 2020-01-27 21:11
 */
public interface PaymentService
{
    public int create(Payment payment);

    public Payment getPaymentById(@Param("id") Long id);

}

PaymentServiceImpl

package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.PaymentDao;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @auther zzyy
 * @create 2020-01-27 21:12
 */
@Service
public class PaymentServiceImpl implements PaymentService
{
    @Resource
    private PaymentDao paymentDao;


    @Override
    public int create(Payment payment)
    {
        return paymentDao.create(payment);
    }

    @Override
    public Payment getPaymentById(Long id)
    {
        return paymentDao.getPaymentById(id);
    }
}

contorller

PaymentController

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import com.sun.media.jfxmedia.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @auther zzyy
 * @create 2020-01-27 21:17
 */
@RestController
@Slf4j
public class PaymentController
{
    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment)
    {
        int result = paymentService.create(payment);
        log.info("*****插入操作返回结果:" + result);

        if(result > 0)
        {
            return new CommonResult(200,"插入数据库成功",result);
        }else{
            return new CommonResult(444,"插入数据库失败",null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
    {
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果:{}",payment);
        if (payment != null) {
            return new CommonResult(200,"查询成功",payment);
        }else{
            return new CommonResult(444,"没有对应记录,查询ID: "+id,null);
        }
    }
}

微服务服务消费者

新建cloud-comsumer-order80 服务消费者,通过RestTemplate访问服务提供者。服务消费者使用的不是视频上的80端口,而是8080端口。

工程重构

将服务提供者和服务消费者的公用entity抽取到一个通用cloud-api-commons工程。供所有工程通用。

标签:02,SpringCloud,org,springcloud,public,atguigu,import,com,学前
From: https://www.cnblogs.com/1963942081zzx/p/17181272.html

相关文章

  • Day02.MYSQL高级语法
    MYSQL基础语法回顾MySQL管理数据库创建数据库CREATEDATABASEIFNOTEXISTS数据库名;删除数据库DROPDATABASE数据库名;表的管理查看所有表use数据库名;选......
  • 02 Golang 基础简介
    一、标识符标识符是指定义的具有特殊意义的词,例如变量、常量、函数名等等,任何一门语言中都对自己的标识符有特殊定义的规则。在Go语言中,标识符由字母数字和下划线组成,并......
  • D. Hard Tasks【GDUT 2022 grade Qualifying】
    D.HardTasks原题链接题意给出一个数n,询问1-n中有多少对组合(三个数)相加不需要进位思路1-10有{0,1,2},{1,2,3},{2,3,4}共3对10-20有{10,11,12},{11,12,13},{12,13,......
  • Diary -「2023」春测
    2023/03/03考前停课了好久,从各种模拟赛中感觉还是学到很多东西。下午试机!好烂的机子。打了Tarjan,就开始尝试能不能登上qq。登上qq联系上了小学同学,结果通知要回去了......
  • C Make it in a Line【GDUT 2022 grade Qualifying #1】
    C MakeitinaLine原题链接题意求出三维n×n×n中的满足条件的总数题解只看底面一个面n个点在一个平面内,一共有n个面:\(n(2n+2)\)(行列+对角线)n个点在垂直底......
  • #yyds干货盘点#【愚公系列】2023年03月 ASP.NET Core下Worker Service构建系统服务实
    前言当你想到ASP.NETCore时,可能会想到Web应用程序后端代码,包括MVC和WebAPI。MVC视图和Razor页面还允许使用后端代码生成带有HTML元素的前端UI。全新的Blazor更进一步,允许......
  • A Calculus【GDUT 2022 grade Qualifying #1】
    A CalculusChipmunkisamathgenius,andheisextraordinarilygoodatcalculus.NowChipmunkgivesyouaformulaandasksyoutocalculateitinoneminute,......
  • 2023 春季测试游记
    Day0下午试机,很快打完了平衡树,强联通分量缩点,线段树,后缀自动机的模板,并测试了一下对拍,感觉不错,不过越坐越紧张。吃完饭买彩票,一共四张,前三张都没刮出来,第四张一下子刮出......
  • Spring2023训练
    3.4AA只能购买2的次方数的纸张,而他最大可以购买n大小的纸张也就是求小于n的最大二次方数#include<bits/stdc++.h>usingnamespacestd;intmain(){intx;......
  • 2023.9 ChatGPT
    ChatGPT为代表的AIGC成为春节后科技圈最火的方向之一,媒体各种报道、国内外大厂纷纷跟进,相关概念股有不少都翻倍了。尽管目前还有不少问题,但你还是要尽可能重视并参与其中,要......