前面我们说过shop-manager-web需要shop-manager-service提供服务。这里我们使用spring cloud的Feign模式,在这里我们需要一个注册中心,用于服务的注册与发现。
如果不使用spring cloud而是直接系统间使用http请求,那么当系统比较多的时候就会比较混乱,同时如果新加了子系统,那么如果以前的系统需要调用新系统的服务就都需要修改代码,不灵活。
四、spring cloud实现系统间通信
1、首先我们来搭建注册中心eureka
eureka就是一个spring boot项目,与前面的过程一样
四、spring cloud实现系统间通信
修改pom文件


<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>eurekaserver</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eurekaserver</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- spring boot test--> <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>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
在这里我将spring boot的版本从2.0.2.RELEASE改为了1.5.2.RELEASE,因为2.0版本的spring boot和spring cloud结合的时候会出现错误
四、spring cloud实现系统间通信
修改application.properties


server.port=8761 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
启动类加上@EnableEurekaServer 注解


@EnableEurekaServer @SpringBootApplication public class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); } }
到此为止注册中心就搭建完成了,注册中心有web界面,访问http://localhost:8761/
四、spring cloud实现系统间通信
2、将shop-manager-service变为生产者
首先添加依赖


<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
修改application.properties,添加下面配置


eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ spring.application.name=shop-manager-service
启动类加上注解@EnableEurekaClient


@EnableEurekaClient @SpringBootApplication @MapperScan("com.shop.mapper") public class ShopManagerServiceApplication { public static void main(String[] args) { SpringApplication.run(ShopManagerServiceApplication.class, args); } }
启动后会在注册中新的web界面中看到该模块
四、spring cloud实现系统间通信
3、将shop-manager-web变为消费者
首先添加依赖


<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
修改application.properties,添加下面配置


eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ spring.application.name=shop-manager-web
启动类加注解


@EnableDiscoveryClient @EnableFeignClients @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) public class ShopManagerWebApplication { public static void main(String[] args) { SpringApplication.run(ShopManagerWebApplication.class, args); } }
启动后访问注册中心
四、spring cloud实现系统间通信
到此我们的基本环境就搭建完成了,下面我们就实现功能,首先是新增商品中选择类目按钮。这个按钮会向后台发送ajax请求,返回List<EasyUITreeNode>。该请求被shop-manager-web接收。
首先是Controller


@RestController public class ItemCatController { @Autowired private ItemCatService itemCatService; @RequestMapping("/item/cat/list") public List<EasyUITreeNode> getItemCatList(@RequestParam(name="id", defaultValue="0")Long parentId) { List<EasyUITreeNode> list = itemCatService.getItemCatList(parentId); return list; } }
service


@FeignClient(value = "shop-manager-service") public interface ItemCatService { @RequestMapping(value = "/item/cat/list") List<EasyUITreeNode> getItemCatList(@RequestParam(name="id", defaultValue="0")Long parentId); }
其中@FeignClient(value = "shop-manager-service")调用shop-manager-service接口
四、spring cloud实现系统间通信
ItemCatController


@Controller public class ItemCatController { @Autowired private ItemCatService itemCatService; @RequestMapping("/item/cat/list") @ResponseBody public List<EasyUITreeNode> getItemCatList(@RequestParam(name="id", defaultValue="0")Long parentId) { List<EasyUITreeNode> list = itemCatService.getItemCatList(parentId); return list; } }
ItemCatService


@Service public class ItemCatService { @Autowired private ItemCatMapper itemCatMapper; public List<EasyUITreeNode> getItemCatList(Long parentId) { List<TbItemCat> list = itemCatMapper.getItemCatList(parentId); //将List<TbItemCat>转为List<EasyUITreeNode> List<EasyUITreeNode> resultList = new ArrayList<>(); for (TbItemCat tbItemCat : list) { EasyUITreeNode node = new EasyUITreeNode(); node.setId(tbItemCat.getId()); node.setText(tbItemCat.getName()); //如果节点下有子节点“closed”,如果没有子节点“open” node.setState(tbItemCat.getIsParent()?"closed":"open"); //添加到节点列表 resultList.add(node); } return resultList; } }
ItemCatMapper


public interface ItemCatMapper { List<TbItemCat> getItemCatList(@Param("parentId") Long parentId); }
ItemCatMapper.xml


<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.shop.mapper.ItemCatMapper" > <resultMap id="TbItemCat" type="com.shop.pojo.TbItemCat" > <id column="id" property="id" jdbcType="BIGINT" /> <result column="parent_id" property="parentId" jdbcType="BIGINT" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="status" property="status" jdbcType="INTEGER" /> <result column="sort_order" property="sortOrder" jdbcType="INTEGER" /> <result column="is_parent" property="isParent" jdbcType="BIT" /> <result column="created" property="created" jdbcType="TIMESTAMP" /> <result column="updated" property="updated" jdbcType="TIMESTAMP" /> </resultMap> <sql id="Base_Column_List" > id, parent_id, name, status, sort_order, is_parent, created, updated </sql> <select id="getItemCatList" resultMap="TbItemCat" parameterType="java.lang.Long" > select <include refid="Base_Column_List" /> from tb_item_cat where parent_id = #{parentId} </select> </mapper>
其中用到了shop-manager-pojo中的TbItemCat以及shop-common中的EasyUITreeNode
四、spring cloud实现系统间通信


public class TbItemCat implements Serializable{ private Long id; private Long parentId; private String name; private Integer status; private Integer sortOrder; private Boolean isParent; private Date created; private Date updated; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getParentId() { return parentId; } public void setParentId(Long parentId) { this.parentId = parentId; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public Integer getSortOrder() { return sortOrder; } public void setSortOrder(Integer sortOrder) { this.sortOrder = sortOrder; } public Boolean getIsParent() { return isParent; } public void setIsParent(Boolean isParent) { this.isParent = isParent; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getUpdated() { return updated; } public void setUpdated(Date updated) { this.updated = updated; } }



public class EasyUITreeNode implements Serializable{ private long id; private String text; private String state; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
首先启动注册中心eurekaserver,然后启动生产者shop-manager-service最后启动消费方shop-manager-web
四、spring cloud实现系统间通信
功能实现
四、spring cloud实现系统间通信
后台日志
接下来我们将实现其他功能

相关文章: