其中
aigou_basic_parent:公共的抽取
aigou_basic_util:公共工具的抽取
aigou_eureka_7001:注册中心
aigou_plat_parent:平台管理
aigou_plat_interface:平台接口
aigou_plat_service:平台服务
aigou_zuul_9527:网关服务
项目流程:
1.需求收集
2.需求分析
3.需求设计
4.项目设计
5.迭代开发
6.测试
7.上线
只配置pom.xml代码如下
UTF-8
UTF-8
1.8
Finchley.SR1
2.0.5.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-dependencies
${springboot.version}
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
server:
port: 7001
eureka:
instance:
hostname: eureka-7001.com
client:
fetch-registry: false #是否注册到eureka
register-with-eureka: false #是否从服务获取注册信息
service-url:
defaultZone: http://eureka-7001.com:7001/eureka/ #这里是另一个中心的地址
@SpringBootApplication
@EnableEurekaServer
public class Enreka7001Application {
public static void main(String[] args) {
SpringApplication.run(Enreka7001Application.class);
}
}
定义一个AjaxResult方便测试数据
public class AJaxResult {
//是否成功
private boolean success=true;
//返回提示信息
private String msg="登录成功";
//对象值,在我们返回前台数据时方便返回一个对象
private Object object;
//向外返回AJaxResult对象
public static AJaxResult me(){
return new AJaxResult();
}
public boolean isSuccess() {
return success;
}
//在每个set方法返回当前对象,方便链式编程
public AJaxResult setSuccess(boolean success) {
this.success = success;
return this;
}
public String getMsg() {
return msg;
}
public AJaxResult setMsg(String msg) {
this.msg = msg;
return this;
}
public Object getObject() {
return object;
}
public AJaxResult setObject(Object object) {
this.object = object;
return this;
}
@Override
public String toString() {
return "AJaxResult{" +
"success=" + success +
", msg='" + msg + '\'' +
", object=" + object +
'}';
}
}
cn.itsource.aigou
aigou_basic_util
1.0-SNAPSHOT
public class User {
private String name;
private String password;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
aigou_plat_interface
cn.itsource.aigou
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
server:
port: 8001
spring:
application:
name: USER-PROVIDER
eureka:
client:
service-url:
defaultZone: http://eureka-7001.com:7001/eureka #告诉服务提供者要把服务注册到哪儿 #单机环境
instance:
prefer-ip-address: true #显示客户端真实ip
@SpringBootApplication
@EnableEurekaClient
public class Service8001Application {
public static void main(String[] args) {
SpringApplication.run(Service8001Application.class);
}
}
@RestController
@RequestMapping("/user")
public class UserController {
//post跳转
@RequestMapping(value = "/login",method = RequestMethod.POST)
public AJaxResult login(@RequestBody User user){
if(user!=null && !StringUtils.isEmpty(user.getName()) && !StringUtils.isEmpty(user.getPassword())){
String name = user.getName();
String password = user.getPassword();
if("admin".equals(name)&&"123".equals(password)){
return AJaxResult.me().setMsg("登录成功").setSuccess(true);
}
}
return AJaxResult.me().setSuccess(false).setMsg("登录失败");
}
//get跳转
@RequestMapping(value = "/login2",method = RequestMethod.GET)
public AJaxResult login2(){
return AJaxResult.me().setMsg("登录失败是get请求").setSuccess(false);
}
}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-zuul
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
server:
port: 9527
spring:
application:
name: AIGOU-ZUUL-GATEWAY
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: gateway-9527.com
prefer-ip-address: true
zuul:
routes:
aigouUser.serviceId: user-provider
aigouUser.path: /user/**
ignored-services: "*"
prefix: /aigou
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class Zuul9527Application {
public static void main(String[] args) {
SpringApplication.run(Zuul9527Application.class);
}
}
7.4.1 SwaggerConfig 类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("分布式购物系统")
.description("购物系统接口文档说明")
.termsOfServiceUrl("http://localhost:8081")
.contact(new Contact("wbtest", "", "[email protected]"))
.version("1.0")
.build();
}
}
7.4.2DocumentationConfig 类
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List get() {
List resources = new ArrayList<>();
//aigou网关前缀,user网关路径 ;swagger的路径 ,以后增加了接口就在这配置就ok
resources.add(swaggerResource("用户系统", "/aigou/user/v2/api-docs", "2.0"));
resources.add(swaggerResource("商品系统", "/aigou/product/v2/api-docs", "2.0"));
resources.add(swaggerResource("基础系统", "/aigou/common/v2/api-docs", "2.0"));
return resources;
}
private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
最后使用路径测试:http://127.0.0.1:9527/swagger-ui.html即可