SpringCloud学习笔记
2020/7/11
微服务架构
![image-20200711183705962](https://gitee.com/liu_xiao_shuai/picture/raw/master/img/image-20200711183705962.png)
![image-20200711184050488](https://gitee.com/liu_xiao_shuai/picture/raw/master/img/image-20200711184050488.png)
技术路线
(红色代表已淘汰技术)
搭建项目环境
(思路:先创建父工程,修改pom文件,再添加一个个的moudle)
-
使用Maven创建一个父工程(site模板)
-
设置编码
-
注解生效激活
-
设置java编译版本
-
删除src文件
-
修改pom文件
-
设置<packing>标签
<packaging>pom</packaging>
-
统一管理jar包版本
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <lombok.version>1.16.18</lombok.version> <log4j.version>1.2.17</log4j.version> <mysql.version>8.0.18</mysql.version> <druid.version>1.1.16</druid.version> <mybatis.spring.boot.version>2.1.1</mybatis.spring.boot.version> </properties>
maven.compiler.source:设置编译的jdk版本
-
子模块继承之后,提供作用:锁定版本+子module不用写groupId和version
<dependencyManagement> <dependencies> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </dependency> <!--spring boot 2.2.2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud Hoxton.SR1--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope> </dependency> <!-- druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> </dependencies> </dependencyManagement>
-
创建SpringBoot服务提供者子模块
思路:建Module、改Pom、写Yml、主启动、业务类
创建Moudle
修改Pom文件
继承了父工程之后,子模块只需写GAV中的A
-
添加依赖
<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> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
编写application.yml文件
内容如下
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 23456789
url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.sise.entities
注:
- mapper-location:扫描resources/mapper中的xml文件
- type-aliases-package:所有entities别名类所在的包
创建启动类
代码如下:
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包
CommenResult类:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class CommentResult<T> { private Integer code; private String message; private T data; public CommentResult(Integer code,String message){ this(code,message,null); } }
Payment类:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @NoArgsConstructor @AllArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
-
dao包
PaymentDao接口:
@Mapper public interface PaymentDao { public int create(Payment payment); public Payment getPaymentById(@Param("id") long id); }
PaymentDao类:
import com.sise.entities.Payment; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface PaymentDao { public int create(Payment payment); public Payment getPaymentById(@Param("id") long id); }
-
resources/mapper/PaymentMapper.xml:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sise.dao.PaymentDao"> <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values (#{serial}) </insert> <resultMap id="BaseResultMap" type="Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" javaType="VARCHAR"/> </resultMap> <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap"> select * from payment where id=#{id} </select> </mapper>
-
service包
PaymentService接口:
import com.sise.entities.Payment; import org.apache.ibatis.annotations.Param; public interface PaymentService { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id); }
PaymentServiceImp类:
import com.sise.dao.PaymentDao; import com.sise.entities.Payment; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class PaymentServiceImp 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); } }
-
controller包
PaymentController类:
import com.sise.entities.CommentResult; import com.sise.entities.Payment; import com.sise.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @PostMapping(value = "/payment/create") public CommentResult create(Payment payment) { int result = paymentService.create(payment); log.info("*****插入结果:"+result); if (result>0){ return new CommentResult(200,"插入数据成功",result); }else { return new CommentResult(444,"插入数据失败"); } } @GetMapping(value = "/payment/get/{id}") public CommentResult getPaymentById(@PathVariable("id") Long id){ Payment payment=paymentService.getPaymentById(id); log.info("*****查找结果:"+payment); if(payment!=null){ return new CommentResult(200,"查询成功",payment); }else { return new CommentResult(444,"没有记录"); } } }
测试
-
get请求
-
post请求
创建SpringBoot服务消费者子模块
创建Moudle
修改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>my-springcloud</artifactId>
<groupId>com.sise</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-order80</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.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
编写application.yml文件
设置端口为80
创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
编写业务类
-
entities包
Payment类:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @NoArgsConstructor @AllArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
CommentResult类:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class CommentResult<T> { private Integer code; private String message; private T data; public CommentResult(Integer code,String message){ this(code,message,null); } }
-
config包
config配置类:
将RestTemplate注入到springboot容器中
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ApplicationContextConfig { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } //注解@bean:相当于在applicationContext.xml中的<bean id="" class=""> }
-
controller包
import com.sise.entities.CommentResult; import com.sise.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.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @Slf4j public class OderController { public static final String PAYMENT_URL="http://localhost:8001"; @Resource RestTemplate restTemplate; @GetMapping("/consumer/payment/create") public CommentResult<Payment> create(Payment payment){ return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommentResult.class); } @GetMapping("/consumer/payment/get/{id}") public CommentResult<Payment> getPayment(@PathVariable("id") Long id){ return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommentResult.class); } }
注:需要在PaymentController中添加@RequestBody
测试
nal String PAYMENT_URL=“http://localhost:8001”;
@Resource
RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommentResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommentResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommentResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommentResult.class);
}
}
注:需要在PaymentController中添加@RequestBody
[外链图片转存中...(img-80NGFltt-1604759997286)]
#### 测试
![image-20200712222547017](https://img-blog.csdnimg.cn/img_convert/04ff2f8e87121c9a2c6f0058c1bb1211.png)