维度,构成体系
SpringCloud = 分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体。俗称微服务全家桶
主流技术
cloud 与 boot版本关系
cloud 升级
环境搭建
总父工程
- 可创建maven site
- 修改IDEA
- 字符编码
- 注解生效激活 Annotation Processors
- java编译选8 Java Processors
maven部分知识点
- dependencyManagement
通常在一个父POM中
- 作用:让所有在子项目中引用一个依赖而不用显示的列出
版本号
- 子模块还是要引入,只是不用写版本,
若写了则优先用子类的
搭建总结
- 新建module
- 写pom
- 写yml
- 主启动类
- 写业务
helloWorld
RestTemplate
RestTemplate提供了多种便捷访问Http服务的方法
是一种简单便捷的访问restful服务模板
- Configuration
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
开启RunDashboard
- .idea下workspace.xml,找对应位置添加
<component name="RunDashboard">
<!-- 添加 -->
<option name="configurationTypes">
<set>
<option value="SpringBootApplicationConfigurationType"/>
</set>
</option>
配置
- pom.xml
<!--web-->
<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><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.xfdd.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
- 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 # mysql驱动包
url: jdbc:mysql://localhost:3306/cloud01?serverTimezone=GMT&characterEncoding=utf-8&useSSL=false
username: root
password: 932567
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.xfdd.springcloud.entity # 所有Entity别名类所在包
服务端Conteoller
- 服务端
@ResponseBody
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment){
int i = paymentService.create(payment);
log.info("****插入结果:"+i);
if(i>0){
return new CommonResult(200,"插入数据成功",i);
}else {
return new CommonResult(444,"插入数据失败",null);
}
}
- 客户端
public class OrderController {
private 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);
}