栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring cloud 学习 day(1)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring cloud 学习 day(1)

spring cloud 《1》
  • 一、基础环境
    • 1.1 版本对应
    • 1.2 搭建项目
    • 1.3 项目配置
    • 1.4 dependencemanager
    • 1.5 跳过单元测试
    • 1.6 mvn:install
    • 1.7 项目当前架构
    • 1.8 pom配置repository
    • 1.9 热部署插件devtools
  • 二、微服务之间的简单调用
    • 2.1 RestTemplate
      • 2.1.1 restTemplate实例注入
      • 2.1.2 通过restemPlate调用远程接口
    • 2.2 远程接口,注意@RequestBody注解
      • 2.2.1 远程接口项目结构
      • 2.2.2 数据层接口
    • 2.3 Spring Dashboard
    • 2.4 数据插入数据库失败,记得加requestBody注解
    • 2.5 消费者模块结构目录
    • 2.6 系统重构,相似的代码提出来
      • 2.6.1 创建公共模块
      • 2.6.2 公共模块的pom
      • 2.6.3 在comm模块使用mvn clean,mvn install点击闪电标志
      • 2.6.4 删除冗余的代码,引入公共模块的
  • 三、出错
    • 3.1 dashboard出不来
    • 3.2 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project CommModule: Fatal error compiling
    • 3.3 There was an unexpected error (type=Internal Server Error, status=500).400 : [{"timestamp":"2022-05-20T07:47:42.395+0000","status":400,"error":"Bad Request","message":"Required String parameter 'serial' is not present","trace":"org.springframework.web.bind.MissingServletRequest... (6011 bytes)]

一、基础环境 1.1 版本对应

1.2 搭建项目

1.3 项目配置
  • 字符编码
  • 注解激活生效
1.4 dependencemanager

1.5 跳过单元测试

1.6 mvn:install

将父工程发布到仓库中

1.7 项目当前架构

1.8 pom配置repository

        
            central
            Central Repository
            https://repo1.maven.org/maven2/
        
    
1.9 热部署插件devtools
  • 子module添加pom


    org.springframework.boot
    spring-boot-devtools
    2.6.7


  • 父工程设置plugin

        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                    true
                
            
        
    
  • 开启自动编译选项

  • ctrl+alt+shift+/ -》registry

  • 重启idea

二、微服务之间的简单调用 2.1 RestTemplate


2.1.1 restTemplate实例注入
package com.order.skill.Config;

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

@Configuration
public class ApplicationContext {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

2.1.2 通过restemPlate调用远程接口
package com.order.skill.Controller;

import com.order.skill.entities.CommenResult;
import com.order.skill.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {
    public static final String PAYMENT_URL = "http://localhost:8081";
    @Resource
    private RestTemplate restTemplate;
    @PostMapping("/consumer/addPayment")
    public CommenResult create(@RequestParam("serial")String serial){
        Payment payment = new Payment(serial);
        return restTemplate.postForObject(PAYMENT_URL + "/addPayment", payment, CommenResult.class);
    }
    @GetMapping("/consumer/getPayment/{id}")
    public CommenResult getPayment(@PathVariable("id") int id){
        return restTemplate.getForObject(PAYMENT_URL + "/getPayment?id=" + id, CommenResult.class);
    }
}

2.2 远程接口,注意@RequestBody注解
package com.mypack.payment.Controller;

import com.mypack.payment.Service.PaymentService;
import com.mypack.payment.entities.CommenResult;
import com.mypack.payment.entities.Payment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class PaymentController {
    @Resource
    PaymentService paymentService;
    //postmapping好多浏览器不支持
    @PostMapping("/addPayment")
    public CommenResult addPayment(@RequestBody("serial")String serial){
        Payment payment = new Payment(serial);
        int i = paymentService.create(payment);
        if(i > 0){
            return new CommenResult(200,"successcrycry",payment);
        }else {
            return new CommenResult(500,"error");
        }

    }
    @GetMapping("/getPayment")
    public CommenResult getPayment(@RequestParam("id")int id){
        Payment payment = paymentService.getPayment(id);
        if(payment != null){
            return new CommenResult(200,"succ", payment);
        }else {
            return new CommenResult(500,"error");
        }
    }
}

2.2.1 远程接口项目结构

2.2.2 数据层接口
  • Dao层
@Mapper
public interface PaymentDao {
    @Insert("insert into payment (serial) values (#{serial})")
    public int create(Payment payment);
    @Select("select * from payment where id = #{id}")
    public Payment getPayment(@Param("id")int id);
}

2.3 Spring Dashboard

2.4 数据插入数据库失败,记得加requestBody注解
  • 加到方法中的字段中去
2.5 消费者模块结构目录

2.6 系统重构,相似的代码提出来 2.6.1 创建公共模块

2.6.2 公共模块的pom

        
        
            org.projectlombok
            lombok
            provided
        

        
        
            cn.hutool
            hutool-all
            5.8.1
        

        
            org.springframework.boot
            spring-boot-dependencies
            2.2.2.RELEASE
            pom
            import
        
        
        
            org.springframework.cloud
            spring-cloud-dependencies
            Hoxton.SR1
            pom
            import
        
    
2.6.3 在comm模块使用mvn clean,mvn install点击闪电标志

  • mvn clean结果

2.6.4 删除冗余的代码,引入公共模块的 三、出错 3.1 dashboard出不来
  • idea 项目名右键->show in explorer
  • 点进自己的项目->.idea->workspace.xml
  • 添加相关配置
3.2 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project CommModule: Fatal error compiling
  • 主项目的pom中mvn的版本与jdk要对应起来
1.8
1.8
3.3 There was an unexpected error (type=Internal Server Error, status=500).400 : [{“timestamp”:“2022-05-20T07:47:42.395+0000”,“status”:400,“error”:“Bad Request”,“message”:“Required String parameter ‘serial’ is not present”,“trace”:"org.springframework.web.bind.MissingServletRequest… (6011 bytes)]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号