springcloud入门及练习

springcloud入门及练习

  1. 什么是SpringCloud?
    SpringCloud是微服务一站式服务解决方案,微服务全家桶。是微服务开发的主流技术栈。
    Cloud简介 ,详见:官方网站:https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/
  2. SpringCloud小案例
    环境:
    idea,maven3.x,springboot,jdk1.8,springcloud,mysql,durid,lombok,postman
    搭建工程:
    2.1 搭建父级工程
    注意:不用使用骨架搭建
    步骤:
    ① 创建项目
    在这里插入图片描述
    在这里插入图片描述
    ② 修改项目环境

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
③ 配置pom.xml文件添加项目依赖

pom

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.mybatis.spring.boot mybatis-spring-boot-starter com.alibaba druid-spring-boot-starter 1.1.10 mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 ③ 编写全局配置文件 application.yml

server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: org.gjt.mm.mysql.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: classpath:mapper/*.xml # xml映射文件路径配置
type-aliases-package: com.usan.pojo # 配置实体类别名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
④ 编写启动类

@SpringBootApplication
@MapperScan(“com.usan.dao”)// 配置包扫描
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
1
2
3
4
5
6
7
⑤ 开始处理业务

创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
1
2
3
4
5
6
7
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {
private Integer code;
private String messgae;
private T data;

/**
 * 查询为空的时候使用的构造器
 * 
 * @param code
 * @param messgae
 */
public CommonResult(Integer code, String messgae){
    this(code, messgae, null);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
创建controller
@RestController // 此次后台使用的使restful风格请求,每个返回值为json格式数据
@Slf4j
public class PaymentController {
@Autowired
private PaymentService paymentService;

/**
 * 添加数据
 *
 * @param payment
 * @return
 */
@PostMapping("/payment/create")
public CommonResult<Payment> create(Payment payment) {
    int result = paymentService.create(payment);
    if (result > 0) {
        return new CommonResult(200, "添加数据成功!!", result);
    } else {
        return new CommonResult(500, "添加数据失败!!", null);
    }
}

// localhost:8001/payment/1
// localhost:8001/payment/create?xxx=xxx

/**
 * 根据id查找对应的信息
 *
 * @param id
 * @return
 */
@GetMapping("/payment/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
    Payment result = paymentService.getPaymentById(id);

    if (result != null) {
        return new CommonResult(200, "查询成功!!", result);
    } else {
        return new CommonResult(500, "没有对应的记录!!", null);
    }
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
创建service
public interface PaymentService {

int create(Payment payment);

Payment getPaymentById(Long id);

}
1
2
3
4
5
6
创建service实现类
@Service
@SuppressWarnings(“all”) // 压制警告
public class PaymentServiceImpl implements PaymentService {
@Autowired
private PaymentDao paymentDao;

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

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

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
创建dao
public interface PaymentDao {
int create(Payment payment);

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

}
1
2
3
4
5
创建xml映射文件


insert into payment values(null,#{serial})

<select id="getPaymentById" parameterType="long" resultType="payment">
    select * from payment where id = #{id}
</select>
1 2 3 4 5 6 7 8 9 项目结构 在这里插入图片描述 ⑥ 测试项目

在这里插入图片描述
在这里插入图片描述

2.3 搭建服务消费方
本次只模拟调用提供方的Controller方法,不用持久层配置,只需配置启动类和启动端口
① 创建方式与创建服务方一样
② 导入pom.xml配置

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ③ 编写配置文件

server:
port: 80
1
2
④ 编写启动类

@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
1
2
3
4
5
6
⑤ 定义实体类(与提供方中实体类一样,直接copy就哦了~)

⑥ 注入RestTemplate

由于要使用RestTemplate进行服务调用,所以需要在spring容器中注入RestTemplate
@Configuration
public class MyConfig{
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
//相当于
1
2
3
4
5
6
7
8
⑦ 编写controller

@RestController
@Slf4j
public class OrderController {

@Autowired
private RestTemplate restTemplate;

// 远程调用的地址
public static final String PAYMENT_URL = "http://localhost:8001";

/**
 * param1: 请求地址
 * param2: 请求参数
 * param3: 返回类型
 *
 * @param payment
 * @return
 */
@PostMapping("consumer/payment/create")
public CommonResult<Payment> create(Payment payment) {
    return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}

@GetMapping("consumer/payment/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
    return restTemplate.getForObject(PAYMENT_URL + "/payment/" + id, CommonResult.class);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
⑧ 项目结构

在这里插入图片描述
⑨测试

在这里插入图片描述
在这里插入图片描述
注意:在服务提供方,controller中的插入订单的方法,需要在形参中加入@RequestBody注解!!!否则插入数据为null。

在这里插入图片描述
在这里插入图片描述

小结:

3.1 关于父工程pom.xml文件中的
聚合版本依赖,只用来声明依赖,并不实现引入,所以子项目中还需要引入依赖。
如果不在子项目中声明依赖,依赖是不会从父项目中继承到的。
子项目中引入的依赖,是不需要标明版本号的,会从父工程中继承到。
同时,version和scope都读取子父项目中的pom.xml文件。
3.2 关于热部署问题
服务方中pom.xml文件添加热部署依赖
父工程中pom.xml文件添加热部署插件
idea中集成了热部署配置,因此,在idea中开启相应的配置项即可。

org.springframework.boot spring-boot-devtools runtime true

1
2
3
4
5
6
7
8

org.springframework.boot spring-boot-maven-plugin true true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
在这里插入图片描述
在这里插入图片描述

ctrl + shift + alt + / ,然后点击Registry进入
但是这样配置有时并没有效果,热部署无效。
解决:
在这里插入图片描述
在这里插入图片描述
这样一来,热部署就可以使用了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值