首先添加依赖
<?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">
<parent>
<artifactId>bz_parent</artifactId>
<groupId>com.bjsxt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common_config</artifactId>
<dependencies>
<!--Spring Boot Web Starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Cloud Eureka Client Starter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--Spring Cloud Config Server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--Logback-->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改common_config模块的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">
<parent>
<artifactId>bz_parent</artifactId>
<groupId>com.bjsxt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common_config</artifactId>
<dependencies>
<!--Spring Boot Web Starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Cloud Eureka Client Starter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--Spring Cloud Config Server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改每一个客户端的服务的pom文件
backend_item,common_item等服务
在每个客户端的配置文件添加配置
创建一个文件,用来存放配置文件,将每个服务的配置文件放在其中,命名backend-content-dev.yml,其他依次这样命名,然后上传到gitee
目标分支:master
spring:
cloud:
config:
discovery:
enabled:
server: true
serviceId: common-config #指定配置中心服务端的服务名称
name: backend-content #对应的{application}部分
profile: dev #对应的{profile}部分
uri: http://192.168.1.101:9000 #配置中心的具体体质,Greenwich版中需要添加
label: master
第一,创建返回托底数据
package com.bjsxt.backend.item.fallback;
import com.bjsxt.backend.item.feign.CommonItemFeignClient;
import com.bjsxt.pojo.*;
import com.bjsxt.utils.PageResult;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* Common-item服务返回托底数据
*/
@Component
public class CommonItemFeignClientFallbackFactory implements FallbackFactory<CommonItemFeignClient>{
@Override
public CommonItemFeignClient create(Throwable throwable) {
return new CommonItemFeignClient() {
@Override
public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
return null;
}
@Override
public Integer insertTbItem(TbItem tbItem) {
return null;
}
@Override
public Integer deleteItemById(TbItem tbItem) {
return null;
}
@Override
public Map<String, Object> preUpdateItem(Long itemId) {
return null;
}
@Override
public Integer updateTbitem(TbItem tbItem) {
return null;
}
@Override
public List<TbItemCat> selectItemCategoryByParentId(Long id) {
return null;
}
@Override
public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
return null;
}
@Override
public PageResult selectItemParamAll(Integer page, Integer rows) {
return null;
}
@Override
public Integer insertItemParam(TbItemParam tbItemParam) {
return null;
}
@Override
public Integer deleteItemParamById(Long id) {
return null;
}
@Override
public Integer insertItemDesc(TbItemDesc tbItemDesc) {
return null;
}
@Override
public Integer updateItemDesc(TbItemDesc tbItemDesc) {
return null;
}
@Override
public Integer insertTbItemParamItem(TbItemParamItem tbItemParamItem) {
return null;
}
@Override
public Integer upateItemParamItem(TbItemParamItem tbItemParamItem) {
return null;
}
};
}
}
第二,修改FeignClient
package com.bjsxt.backend.item.feign;
import com.bjsxt.backend.item.fallback.CommonItemFeignClientFallbackFactory;
import com.bjsxt.pojo.*;
import com.bjsxt.utils.PageResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@FeignClient(value = "common-item",fallbackFactory = CommonItemFeignClientFallbackFactory.class)
public interface CommonItemFeignClient {
//--------------------/Service/Item
@GetMapping("/service/item/selectTbItemAllByPage")
PageResult selectTbItemAllByPage(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows);
@PostMapping("/service/item/insertTbItem")
Integer insertTbItem(@RequestBody TbItem tbItem);
@PostMapping("/service/item/deleteItemById")
Integer deleteItemById(@RequestBody TbItem tbItem);
@PostMapping("/service/item/preUpdateItem")
Map<String,Object> preUpdateItem(@RequestParam("itemId") Long itemId);
@PostMapping("/service/item/upateTbitem")
Integer updateTbitem(@RequestBody TbItem tbItem);
//--------------------/Service/itemCategory
@PostMapping("/service/itemCategory/selectItemCategoryByParentId")
List<TbItemCat> selectItemCategoryByParentId(@RequestParam("id") Long id);
//---------------------/Service/itemParam----------------------
@PostMapping("/service/itemParam/selectItemParamByItemCatId")
TbItemParam selectItemParamByItemCatId(@RequestParam("itemCatId") Long itemCatId);
@PostMapping("/service/itemParam/selectItemParamAll")
PageResult selectItemParamAll(@RequestParam("page") Integer page,@RequestParam("rows") Integer rows);
@RequestMapping("/service/itemParam/insertItemParam")
Integer insertItemParam(@RequestBody TbItemParam tbItemParam);
@RequestMapping("/service/itemParam/deleteItemParamById")
Integer deleteItemParamById(@RequestParam("id") Long id);
//--------------------/Service/itemDesc
@PostMapping("/service/itemDesc/insertItemDesc")
Integer insertItemDesc(@RequestBody TbItemDesc tbItemDesc);
@PostMapping("/service/itemDesc/updateItemDesc")
Integer updateItemDesc(@RequestBody TbItemDesc tbItemDesc);
//--------------------/Service/itemParamItem
@PostMapping("/service/itemParamItem/insertTbItemParamItem")
Integer insertTbItemParamItem(@RequestBody TbItemParamItem tbItemParamItem);
@PostMapping("/service/itemParamItem/upateItemParamItem")
Integer upateItemParamItem(@RequestBody TbItemParamItem tbItemParamItem);
}
第三,修改serviceImpl
package com.bjsxt.backend.item.service.impl;
import com.bjsxt.backend.item.feign.CommonItemFeignClient;
import com.bjsxt.backend.item.service.ItemService;
import com.bjsxt.pojo.TbItem;
import com.bjsxt.pojo.TbItemDesc;
import com.bjsxt.pojo.TbItemParamItem;
import com.bjsxt.utils.IDUtils;
import com.bjsxt.utils.PageResult;
import com.bjsxt.utils.Result;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
/**
* 商品管理
*/
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private CommonItemFeignClient commonItemFeignClient;
@Override
public Result selectTbItemAllByPage(Integer page, Integer rows) {
PageResult pageResult = this.commonItemFeignClient.selectTbItemAllByPage(page,rows);
if(pageResult != null && pageResult.getResult() != null && pageResult.getResult().size() > 0){
return Result.ok(pageResult);
}
return Result.error("查无结果");
}
/**
* 添加TbItem,添加TbitemDesc,添加TbItemParamItem
* @param tbItem
* @param desc
* @param itemParams
* @return
*/
@Override
@LcnTransaction
public Result insertTbItem(TbItem tbItem, String desc, String itemParams){
//补齐Tbitem数据
Long itemId = IDUtils.genItemId();
Date date = new Date();
tbItem.setId(itemId);
tbItem.setStatus((byte)1);
tbItem.setUpdated(date);
tbItem.setCreated(date);
Integer tbItemNum = this.commonItemFeignClient.insertTbItem(tbItem);
//补齐商品描述对象
TbItemDesc tbItemDesc = new TbItemDesc();
tbItemDesc.setItemId(itemId);
tbItemDesc.setItemDesc(desc);
tbItemDesc.setCreated(date);
tbItemDesc.setUpdated(date);
Integer tbitemDescNum = this.commonItemFeignClient.insertItemDesc(tbItemDesc);
//补齐商品规格参数
TbItemParamItem tbItemParamItem = new TbItemParamItem();
tbItemParamItem.setItemId(itemId);
tbItemParamItem.setParamData(itemParams);
tbItemParamItem.setUpdated(date);
tbItemParamItem.setCreated(date);
Integer itemParamItmeNum = this.commonItemFeignClient.insertTbItemParamItem(tbItemParamItem);
if(tbItemNum == null || tbitemDescNum== null ||itemParamItmeNum ==null ){
throw new RuntimeException();
}
return Result.ok();
}
/**
* 删除商品
* @param itemId
* @return
*/
@Override
@LcnTransaction
public Result deleteItemById(Long itemId){
TbItem item = new TbItem();
item.setId(itemId);
item.setStatus((byte)3);
Integer itemNum = this.commonItemFeignClient.deleteItemById(item);
if(itemNum == null){
throw new RuntimeException();
}
return Result.ok();
}
/**
* 预更新商品查询
* @param itemId
* @return
*/
@Override
public Result preUpdateItem(Long itemId) {
Map<String,Object> map = this.commonItemFeignClient.preUpdateItem(itemId);
if(map != null){
return Result.ok(map);
}
return Result.error("查无结果");
}
/**
* 更新商品:更新TbItem表,更新TbitemDesc表,更新TbItempParamItem表
* @param tbItem
* @param desc
* @param itemParams
* @return
*/
@Override
@LcnTransaction
public Result updateTbItem(TbItem tbItem, String desc, String itemParams) {
//更新商品
Integer itemNum = this.commonItemFeignClient.updateTbitem(tbItem);
//更新商品描述
TbItemDesc tbItemDesc = new TbItemDesc();
tbItemDesc.setItemId(tbItem.getId());
tbItemDesc.setItemDesc(desc);
Integer itemDescNum = this.commonItemFeignClient.updateItemDesc(tbItemDesc);
//更新商品规格参数
TbItemParamItem tbItemParamItem = new TbItemParamItem();
tbItemParamItem.setParamData(itemParams);
tbItemParamItem.setItemId(tbItem.getId());
Integer itemParamItemNum = this.commonItemFeignClient.upateItemParamItem(tbItemParamItem);
if(itemNum ==null || itemDescNum == null || itemParamItemNum ==null){
throw new RuntimeException();
}
return Result.ok();
}
}
第一,访问Kibana
第三,添加配置文件logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--该日志将日志级别不同的log信息保存到不同的文件中 -->
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<springProperty scope="context" name="springAppName"
source="spring.application.name" />
<!-- 日志在工程中的输出位置 -->
<property name="LOG_FILE" value="${BUILD_FOLDER:-build}/${springAppName}" />
<!-- 控制台的日志输出样式 -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<!-- 日志输出编码 -->
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<!-- 为logstash输出的JSON格式的Appender -->
<appender name="logstash"
class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>192.168.70.140:9250</destination>
<!-- 日志输出编码 -->
<encoder
class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>UTC</timeZone>
</timestamp>
<pattern>
<pattern>
{
"severity": "%level",
"service": "${springAppName:-}",
"trace": "%X{X-B3-TraceId:-}",
"span": "%X{X-B3-SpanId:-}",
"exportable": "%X{X-Span-Export:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"rest": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<!-- 日志输出级别 -->
<root level="DEBUG">
<appender-ref ref="console" />
<appender-ref ref="logstash" />
</root>
</configuration>