SpringCloud微服务任务笔记
任务一:重构账单项目,开发基于 resutful 的 webservice 接口,创建 EurekaServer,把账单服务注册到注册中
1.构建EurekaServer,选择如下模块
2.pom.xml配置如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kkb.qun</groupId>
<artifactId>03_cloud_eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>03_cloud_eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.修改账单项目controller类(查询账单列表为例)
/*** 查询 * @param b * @param model * @return */
@GetMapping(value = "/list-page", produces = "application/json;charset=UTF-8")
public ResponseEntity listPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, Bill b, BindingResult bindingResult, Model model) {
List<BillType> types = typeService.list();
//model.addAttribute("types", types);
PageInfo<Bill> pageInfo = billService.listPage(b, pageNum, pageSize);
//model.addAttribute("page", pageInfo);
//model.addAttribute("bill", b);
HashMap map = new HashMap();
map.put("types", types);
map.put("pageInfo", pageInfo);
ResultVo resultVo = new ResultVo();
resultVo.setCode(200);
resultVo.setMessage("ok");
resultVo.setData(map);
return new ResponseEntity(resultVo, HttpStatus.OK);
}
4.application.yml 配置文件修改
5.使用postman调用接口
任务二:重构账单项目,搭建网关微服务,前端访问地址约定 http://locahost:8086/api/bill/list 等
1.创建微服务网关工程,选择如下依赖
2.修改配置文件
server:
port: 9005
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: url-proxy-1
uri: http://127.0.0.1:9009/
predicates:
- Path=/api/bill/**
filters:
- StripPrefix=1
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9010/eureka
3.调用接口
任务三:创建账单项目配置微服务,使用码云远程库,统一管理账单微服务配置文件 application.yml
1.gitee创建新仓库,存储配置文件
2.配置文件
3.接口调用查看配置文件