spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建...

教学视频传送:

https://www.bilibili.com/video/BV19T4y1g7ue


springBoot和springCloud的版本选型

https://start.spring.io/actuator/info

查看json串返回结果

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第1张图片

这就是我们的选型依据

本次开发选用版本如下:

cloud         : Hoxton.SR1boot          : 2.2.2.RELEASEcloud alibaba : 2.1.0.RELEASEjava          : java8Maven         : 3.5及以上Mysql         : 5.7及以上

关于Cloud各种组件的停更、升级、替换

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第2张图片

约定>配置>编码

IDEA新建project工作空间

微服务cloud整体聚合工程

父工程步骤

1.New Project

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第3张图片

2.聚合总父工程名字

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第4张图片

3.Maven选版本

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第5张图片

4.工程名字

5.字符编码

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第6张图片

6.注解生效激活

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第7张图片

7.java编译版本选8

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第8张图片

8.File Type过滤

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第9张图片

9.父工程POM

4.0.0org.springframework.boot        spring-boot-starter-parent        2.2.6.RELEASEcom.top    cloud2020-learn    1.0cloud2020-learnpomDemo project for cloud2020-learncloud-provider-payment8001UTF-81.81.84.121.2.171.16.185.1.471.1.162.2.2.RELEASEHoxton.SR12.1.0.RELEASE1.3.0org.springframework.boot                spring-boot-dependencies                ${spring.boot.version}pomimportorg.springframework.cloud                spring-cloud-dependencies                ${spring.cloud.version}pomimportcom.alibaba.cloud                spring-cloud-alibaba-dependencies                ${spring.cloud.alibaba.version}pomimportmysql                mysql-connector-java                ${mysql.version}com.alibaba                druid                ${druid.version}org.mybatis.spring.boot                mybatis-spring-boot-starter                ${mybatis.spring.boot.version}org.projectlombok                lombok                ${lombok.version}trueorg.springframework.boot                spring-boot-maven-plugin                true                    true                nexus-aliyunNexus aliyunhttp://maven.aliyun.com/nexus/content/groups/publictruefalse

Maven工程落地细节复习

Maven中的DependencyManagement和Dependencies
spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第10张图片
maven中跳过单元测试
spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第11张图片
父工程创建完成执行mvn:insall将父工程发布到仓库方便子工程继承
spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第12张图片

Rest微服务工程搭建

构建步骤
1. Cloud-provider-payment8001 微服务提供者Module模块
- 建module
spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第13张图片
- 改POM
org.springframework.boot            spring-boot-starter-web        org.mybatis.spring.boot            mybatis-spring-boot-starter        com.alibaba            druid-spring-boot-starter            1.1.10mysql            mysql-connector-java        org.springframework.boot            spring-boot-starter-jdbc        org.projectlombok            lombok            trueorg.springframework.boot            spring-boot-starter-test            testorg.junit.vintage                    junit-vintage-engine                
- 写YML
server:  port: 8001spring:  application:    name: cloud-payment-service  datasource:    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包    url: jdbc:mysql://localhost:3306/the_course?useUnicode=true&characterEncoding=utf-8&useSSL=false    username: root    password: aaaaaamybatis:  mapperLocations: classpath:mapper/*.xml  type-aliases-package: com.top.cloud.provider.payment8001.entities    # 所有Entity别名类所在包
- 主启动
@SpringBootApplicationpublic class CloudProviderPayment8001Application {    public static void main(String[] args) {        SpringApplication.run(CloudProviderPayment8001Application.class, args);    }}
- 业务类
  1. CommonResult
@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult{    private Integer code;    private String  message;    private T       data;    public CommonResult(Integer code, String message)    {        this(code,message,null);    }}
  1. Payment
@Data@AllArgsConstructor@NoArgsConstructorpublic class Payment implements Serializable {    private Long id;    private String name;}
  1. PaymentDao
@Mapperpublic interface PaymentDao {    public int create(Payment payment);    public Payment getPaymentById(@Param("id") Long id);}
  1. PaymentService
public interface PaymentService{    int create(Payment payment);    Payment getPaymentById(@Param("id") Long id);}
  1. PaymentServiceImpl
@Servicepublic 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);    }}
  1. PaymentMapper.xml
        insert into payment(name)  values(#{name});            select * from payment where id=#{id};    
  1. PaymentController
@RestController@Slf4jpublic class PaymentController {    @Resource    private PaymentService paymentService;    @Value("${server.port}")    private String serverPort;    @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,"插入数据库成功,serverPort: "+serverPort,result);        }else{            return new CommonResult(444,"插入数据库失败",null);        }    }    @GetMapping(value = "/payment/get/{id}")    public CommonResult getPaymentById(@PathVariable("id") Long id)    {        Payment payment = paymentService.getPaymentById(id);        if(payment != null)        {            return new CommonResult(200,"查询成功,serverPort:  "+serverPort,payment);        }else{            return new CommonResult(444,"没有对应记录,查询ID: "+id,null);        }    }}
- 测试

用postman或者idea自带的HTTPClient测试接口

2. cloud-consumer-order80 微服务消费者订单Module模块
- 建cloud-consumer-order80
- 改POM
 org.springframework.boot            spring-boot-starter-web        org.springframework.boot            spring-boot-starter-actuator        org.springframework.boot            spring-boot-devtools            runtimetrueorg.projectlombok            lombok            trueorg.springframework.boot            spring-boot-starter-test            testorg.junit.vintage                    junit-vintage-engine                
- 写YML
server:  port: 80spring:    application:        name: cloud-order-service
- 主启动
@SpringBootApplicationpublic class CloudConsumerOrder80Application {    public static void main(String[] args) {        SpringApplication.run(CloudConsumerOrder80Application.class, args);    }}
- 业务类
  1. CommonResult
@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult{    private Integer code;    private String  message;    private T       data;    public CommonResult(Integer code, String message)    {        this(code,message,null);    }}
  1. Payment
@Data@AllArgsConstructor@NoArgsConstructorpublic class Payment implements Serializable {    private Long id;    private String name;}
  1. ApplicationContextConfig
@Configurationpublic class ApplicationContextConfig{    @Bean    //@LoadBalanced    public RestTemplate getRestTemplate()    {        return new RestTemplate();    }}
  1. OrderController
@RestController@Slf4jpublic class OrderController {    public static final String PAYMENT_URL = "http://localhost:8001";    @Resource    private RestTemplate restTemplate;    @GetMapping("/consumer/payment/create")    public CommonResult create(Payment payment)    {        return restTemplate.postForObject(PAYMENT_URL +"/payment/create",payment,CommonResult.class);    }    @GetMapping("/consumer/payment/get/{id}")    public CommonResult getPayment(@PathVariable("id") Long id)    {        System.out.println("哈哈");        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);    }    @GetMapping("/consumer/payment/getForEntity/{id}")    public CommonResult getPayment2(@PathVariable("id") Long id)    {        ResponseEntity entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);        if(entity.getStatusCode().is2xxSuccessful()){            return entity.getBody();        }else{            return new CommonResult<>(444,"操作失败");        }    }}
- 测试

http://localhost/consumer/payment/get/1

3. 工程重构

观察问题: 系统中有重复部分,重构

  • 新建cloud-api-common

就是一个最简单的工程

  • 改pom
4.0.0com.top        cloud2020-learn        1.0com.top    cloud-api-common    1.0cloud-api-commonDemo project for cloud-api-common1.8org.springframework.boot            spring-boot-devtools            runtimetrueorg.projectlombok            lombok            true
  • 提取公共部分
spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第14张图片
  • maven命令clean install
spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第15张图片
  • 其他模块依赖 删除各自的原先的entities文件夹

pom中添加:

com.top    cloud-api-common    1.0

修改代码中的依赖路径

解决SpringBoot项目repackage failed: Unable to find main class Maven打包 install的问题

发现父项目有一个打包的插件org.springframework.boot            spring-boot-maven-plugin            repackage这时,问题就出现了,报打包失败错误!!! 解决:如果你的项目是一个放置通用工具类的工程,那么该项目中,就不能包括上面这个打包插件,如果你这个工具类工程依赖有父工程,那么父工程中也不能包括该打包插件,只有你的项目是一个web项目时,含有Main方法的程序入口类,要加该打包插件,我放在了父工程的pom文件中,那就是代表了所有子模块都有这个打包插件,所以报错,解决就是去掉这个插件 ,只在web工程中加入这个打包插件!

目前工程样图

spring cloud alibaba微服务原理与实战_《SpringCloud H版+SpringCloud alibaba》01.微服务架构编码构建..._第16张图片

你可能感兴趣的:(spring,cloud,alibaba微服务原理与实战)