一.概述
之前章节开发的接口都是为了下订单接口的开发做了铺垫,下订单的接口内部将调用这些接口,是一个涉及到4-5个微服务的重量级接口.
二.下订单接口伪代码
可以看到该接口涉及的微服务有:server-user、server-goods、server-pay和server-order服务.
三.新建server-order微服务
1.创建maven工程.
2.server-order.pom中引入maven依赖.
<dependencies>
<dependency>
<groupId>com.ccm</groupId>
<artifactId>assembly-mysql</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
3.编写服务启动类.
package com.ccm.server.order;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Description server-order服务启动类
* @Author ccm
* @CreateTime 2020/8/14 9:47
*/
@EnableFeignClients
@EnableDiscoveryClient //注册中心客户端
@ComponentScan(basePackages = "com.ccm")
@EnableSwagger2
@SpringBootApplication //声明为一个启动类
@MapperScan(basePackages = "com.ccm.server.order.dao.mysql.mapper")
public class ServerOrderApplication {
public static void main(String[] args) {
SpringApplication.run(ServerOrderApplication.class,args);
}
}
4.编写bootstrap.yml配置文件.
server:
port: 7677 #服务端口
spring:
application:
name: server-order #服务名称
cloud:
nacos:
discovery:
server-addr: 47.96.131.185:8849
config:
server-addr: 47.96.131.185:8849 #nacos config配置中心ip和端口
file-extension: yaml #文件扩展名格式,针对于默认的{spring.application.name}-${profile}.${file-extension:properties}配置
enabled: true #开启或关闭配置中心
datasource:
username: root
password: Test2016@
url: jdbc:mysql://47.96.131.185:3306/order?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
typeAliasesPackage: com.ccm.server.order.dao.mysql.domain #数据库实体类包
mapper-locations: classpath:mappering/*.xml #xml文件扫描
#自定义配置
server-order:
serverNumber: 12
5.自定义配置的实体映射.
package com.ccm.server.order.constants;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @Description 自定义配置映射实体
* @Author ccm
* @CreateTime 2020/08/18 15:01
*/
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "server-order")
public class ServerOrderProperties {
private String serverNumber;
@PostConstruct
public void init() {
log.info("ServerOrderProperties初始化完成......{}", JSONObject.toJSONString(this));
}
}
6.swagger配置.
package com.ccm.server.order.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;
/**
* @Description swagger配置
* @Author ccm
* @CreateTime 2020/08/14 17:38
*/
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
List<Parameter> pars = new ArrayList<Parameter>();
ParameterBuilder ticketPar = new ParameterBuilder();
ticketPar.name("ccm-token").description("必要参数(白名单接口无需传递)")
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build(); //header中的ticket参数非必填,传空也可以
pars.add(ticketPar.build