一、前言
在之前的系统架构的演变中,已经简要介绍了系统架构的的发展已及各个系统架构的优缺点,下面从零开始详细介绍一下以微服务spring cloud架构的项目搭建。
本节主要阐述,商品微服务搭建,后续还会介绍订单微服务搭建,以及商品微服务和订单微服务的数据交互及服务的调用、此外还会介绍spring cloud中的产品erueka(类似于阿里的中间件dubbo)
二、项目搭建
1.项目的搭建环境
- 编辑器:spring tool suit(STS)
- JDK:1.8
- 项目构建工具:maven
2.开始搭建
1.新建maven项目 new maven project
2.打包方式jar
3.指定jdk版本及添加Spring Cloud依赖
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
</parent>
<!-- 导入Spring Cloud的依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- spring boot web dependency -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 导入Eureka依赖服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.23</version>
</dependency>
</dependencies>
3.创建pojo,controller,service对应的包结构等,一共分为三步
3.1创建pojo,controller,service及文件,添加set、get方法
3.1.1.pojo
private Long id;
private String title;
private String pic;
private String desc;
private Long price;
3.1.2.service,构造假数据
package cn.itcast.microservice.service;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import cn.itcast.microservice.pojo.Item;
@Service
public class ItemService {
private static final Map<Long, Item> MAP = new HashMap<Long,Item>();
static {
MAP.put(1L,new Item(1L, "商品标题1", "http://图片1","商品描述1", 1000L));
MAP.put(1L,new Item(2L, "商品标题2", "http://图片2","商品描述2", 2000L));
MAP.put(1L,new Item(3L, "商品标题3", "http://图片3","商品描述3", 3000L));
MAP.put(1L,new Item(4L, "商品标题4", "http://图片4","商品描述4", 4000L));
MAP.put(1L,new Item(5L, "商品标题5", "http://图片5","商品描述5", 5000L));
MAP.put(1L,new Item(6L, "商品标题6", "http://图片6","商品描述6", 6000L));
MAP.put(1L,new Item(7L, "商品标题7", "http://图片7","商品描述7", 7000L));
MAP.put(1L,new Item(8L, "商品标题8", "http://图片8","商品描述8", 1000L));
}
/**
* 根据商品的id查询
* @param id
* @return
*/
public Item queryItemById(Long id) {
return MAP.get(id);
}
}
3.1.3.service,构造假数据
package cn.itcast.microservice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import cn.itcast.microservice.pojo.Item;
import cn.itcast.microservice.service.ItemService;
@RestController
public class ItemController {
@Autowired
private ItemService itemService;
// @RequestMapping(value="/item/{id}",method=RequestMethod.GET)
@GetMapping(value="/item/{id}")
public Item queryItemById(@PathVariable(name="id") Long id) {
return itemService.queryItemById(id);
}
}
3.2.创建ItemApplication入口文件,注意文件的位置一定要放在pojo,service,controller的上层
package cn.itcast.microservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ItemApplication {
public static void main(String[] args) {
SpringApplication.run(ItemApplication.class, args);
}
}
3.3.application.properties配置文件
server.port=8082
spring.application.name=itcast-microservice-itcast
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://127.0.0.1:6868/eureka/
eureka.instance.prefer-ip-address=true
3.运行测试
运行ItemApplication.java文件
在本地的浏览器访问application.properties对应的端口号,即可访问,即商品微服务发布完成