openfeign实现前台服务调用文章服务实现查看文章列表及读取文章功能
源码地址
https://gitee.com/liuerchong/news-artical
首先搭建文章服务
用https://blog.csdn.net/liuerchong/article/details/119035213文章中的代码生成器生成代码,复制到article文章服务路径下,源码如下:
代码格式自行格式化
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>news-artical</artifactId>
<groupId>com.liu.news</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ap-article</artifactId>
<dependencies>
<dependency>
<groupId>com.liu.news</groupId>
<artifactId>artical-common-db</artifactId>
</dependency>
<dependency>
<groupId>com.liu.news</groupId>
<artifactId>artical-common-public</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
</project>
controller
package com.liu.news.artical.controller.article;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liu.news.artical.entity.article.ApArticle;
import com.liu.news.artical.services.article.service.IApArticleService;
import com.liu.news.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* @Package: com.liu.news.artical.entity.article.ApArticle
* @Description: <文章信息表,存储已发布的文章前端控制器>
* @Author: Liuerchong
* @CreateDate: 2021-07-23
* @UpdateUser: Liuerchong
* @UpdateDate: 2021-07-23
* @UpdateRemark: <>
* @Version: 1.0
*/
@RestController
@Api(tags = "ApArticle-相关接口")
@RequestMapping(value = "ApArticle")
public class ApArticleController {
private static final Logger logger = LoggerFactory.getLogger(ApArticleController.class);
@Autowired
private IApArticleService iApArticleService;
@GetMapping("{id}")
@ApiOperation(value = "文章信息表,存储已发布的文章获取某一实体")
public R getIApArticleServiceDetails(@PathVariable Integer id) {
return R.ok(iApArticleService.getById(id));
}
/**
* 列表查询(非分页)
*
* @return
*/
@RequestMapping("/list")
@SentinelResource(value = "articleList")
public R list() {
ApArticle model = new ApArticle();
List<ApArticle> list = iApArticleService.list();
// todo 再包装一层
return R.ok(list);
}
/**
* 列表查询(分页)
*
* @return
*/
@RequestMapping("/pageList")
public Object pageList(Integer pageNum, Integer pageSize) {
ApArticle model = new ApArticle();
IPage<ApArticle> page = iApArticleService.pageList(model,pageNum,pageSize);
// todo 再包装一层
return R.ok(page);
}
@PostMapping
@ApiOperation(value = "文章信息表,存储已发布的文章新增数据")
public R saveIApArticleService(@RequestBody ApArticle dto) {
return R.ok(iApArticleService.save(dto));
}
@PutMapping("{id}")
@ApiOperation(value = "文章信息表,存储已发布的文章修改数据")
public R modifyIApArticleService(@RequestBody ApArticle dto, @PathVariable Integer id) {
dto.setId(id);
return R.ok(iApArticleService.updateById(dto)) ;
}
@DeleteMapping("batch")
@ApiOperation(value = "文章信息表,存储已发布的文章批量删除数据")
public R batchRemoveIApArticleService(@RequestParam(value = "ids") List<Integer> ids) {
return R.ok(iApArticleService.removeByIds(ids)) ;
}
@DeleteMapping("{id}")
@ApiOperation(value = "文章信息表,存储已发布的文章删除数据")
public R removeIApArticleService(@PathVariable Integer id) {
return R.ok(iApArticleService.removeById(id)) ;
}
}
service
package com.liu.news.artical.services.article.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liu.news.artical.entity.article.ApArticle;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* @Package: com.liu.news.artical.services.article.service
* @Description: <文章信息表,存储已发布的文章服务类>
* @Author: Liuerchong
* @CreateDate: 2021-07-23
* @UpdateUser: Liuerchong
* @UpdateDate: 2021-07-23
* @UpdateRemark: <>
* @Version: 1.0
*/
public interface IApArticleService extends IService<ApArticle> {
/**
* 查询
*
* @param model
* @return
*/
List<ApArticle> list(ApArticle model);
/**
* 分页查询
*
* @param model
* @param pageNum 第几页
* @param pageSize 每页数量
* @return
*/
IPage<ApArticle> pageList(ApArticle model, Integer pageNum, Integer pageSize);
}
package com.liu.news.artical.services.article.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.news.artical.entity.article.ApArticle;
import com.liu.news.artical.mapper.article.ApArticleMapper;
import com.liu.news.artical.services.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
/**
-
@Package: com.liu.news.artical.services.article.service.impl
-
@Description: <文章信息表,存储已发布的文章服务实现类>
-
@Author: Liuerchong
-
@CreateDate: 2021-07-23
-
@UpdateUser: Liuerchong
-
@UpdateDate: 2021-07-23
-
@UpdateRemark: <>
-
@Version: 1.0
*/
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {@Override public List< ApArticle> list(ApArticle model) { QueryWrapper< ApArticle> wrapper = new QueryWrapper(); return super.list(wrapper); } @Override public IPage< ApArticle> pageList(ApArticle model, Integer pageNum, Integer pageSize) { QueryWrapper< ApArticle> wrapper = new QueryWrapper(model); return this.page(new Page<>(pageNum, pageSize), wrapper); } }
mapper
package com.liu.news.artical.mapper.article;
import com.liu.news.artical.entity.article.ApArticle;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @Package: com.liu.news.artical.mapper.article
* @Description: <文章信息表,存储已发布的文章Mapper接口>
* @Author: Liuerchong
* @CreateDate: 2021-07-23
* @UpdateUser: Liuerchong
* @UpdateDate: 2021-07-23
* @UpdateRemark: <>
* @Version: 1.0
*/
@Mapper
public interface ApArticleMapper extends BaseMapper<ApArticle> {
}
实体层
package com.liu.news.artical.entity.article;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* @Package: com.liu.news.artical.entity.article
* @Description: <文章信息表,存储已发布的文章实体>
* @Author: Liuerchong
* @CreateDate: 2021-07-23
* @UpdateUser: Liuerchong
* @UpdateDate: 2021-07-23
* @UpdateRemark: <>
* @Version: 1.0
*/
@Data
@ApiModel(value = "ApArticle实体")
public class ApArticle implements Serializable {
/**
* 序列化时候使用
*/
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.ASSIGN_ID)
@ApiModelProperty(value = "", notes = "")
private Integer id;
@ApiModelProperty(value = "标题", notes = "")
private String title;
@ApiModelProperty(value = "文章作者的ID", notes = "")
private Integer authorId;
@ApiModelProperty(value = "作者昵称", notes = "")
private String authorName;
@ApiModelProperty(value = "文章所属频道ID", notes = "")
private Integer channelId;
@ApiModelProperty(value = "频道名称", notes = "")
private String channelName;
@ApiModelProperty(value = "文章布局", notes = "0 无图文章 1 单图文章 2 多图文章")
private Integer layout;
@ApiModelProperty(value = "文章标记",
notes = "0 普通文章 1 热点文章 2 置顶文章 3 精品文章 4 大V 文章")
private Integer flag;
@ApiModelProperty(value = "文章图片", notes = "多张逗号分隔")
private String images;
@ApiModelProperty(value = "文章标签最多3个", notes = "逗号分隔")
private String labels;
@ApiModelProperty(value = "点赞数量", notes = "")
private Integer likes;
@ApiModelProperty(value = "收藏数量", notes = "")
private Integer collection;
@ApiModelProperty(value = "评论数量", notes = "")
private Integer comment;
@ApiModelProperty(value = "阅读数量", notes = "")
private Integer views;
@ApiModelProperty(value = "省市", notes = "")
private Integer provinceId;
@ApiModelProperty(value = "市区", notes = "")
private Integer cityId;
@ApiModelProperty(value = "区县", notes = "")
private Integer countyId;
@ApiModelProperty(value = "创建时间", notes = "")
private LocalDateTime createdTime;
@ApiModelProperty(value = "发布时间", notes = "")
private LocalDateTime publishTime;
@ApiModelProperty(value = "同步状态", notes = "")
private Boolean syncStatus;
@ApiModelProperty(value = "来源", notes = "")
private Integer origin;
}
启动类
package com.liu.news.artical;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author lx
* @version 1.0
* @description: TODO
* @date 2021/7/23 13:33
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ApArticleApplication {
public static void main(String[] args) throws InterruptedException{
SpringApplication.run(ApArticleApplication.class, args);
}
}
配置文件
server:
port: 9070
spring:
profiles:
active: dev
application:
name: ap-artical
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
username: nacos
password: nacos
server-addr: 172.17.169.81:8848
namespace: b561281a-fb95-411e-b75b-cf36bfc90854
group: ATICAL_CLNEWS_DEV_GROUP
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml
namespace: ${spring.cloud.nacos.discovery.namespace}
group: ${spring.cloud.nacos.discovery.group}
shared-configs[0]:
data-id: user-prompt-info.yml # 配置文件名-Data Id
group: ${spring.cloud.nacos.discovery.group} # 默认为DEFAULT_GROUP
refresh: true # 是否动态刷新,默认为false
sentinel:
transport:
port: 8719 # spring.cloud.sentinel.transport.port 端口配置会在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互。比如 Sentinel 控制台添加了1个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中
dashboard: 127.0.0.1:5003
eager: true
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动包
url: jdbc:mysql://localhost:3307/artical?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username: root
password: root
mybatis-plus:
global-config:
db-config:
field-strategy: not_empty
#驼峰下划线转换
column-underline: true
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
db-type: mysql
refresh: false
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
启动服务,进行访问
搭建前台服务
新建模块 article-reception
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>news-artical</artifactId>
<groupId>com.liu.news</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>article-reception</artifactId>
<dependencies>
<dependency>
<groupId>com.liu.news</groupId>
<artifactId>artical-common-utils</artifactId>
</dependency>
<dependency>
<groupId>com.liu.news</groupId>
<artifactId>artical-common-public</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
配置文件
bootstrap.yml
server:
port: 8060
spring:
profiles:
active: dev
application:
name: article-reception
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
username: nacos
password: nacos
server-addr: 172.17.169.81:8848
namespace: b561281a-fb95-411e-b75b-cf36bfc90854
group: ATICAL_CLNEWS_DEV_GROUP
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml
namespace: ${spring.cloud.nacos.discovery.namespace}
group: ${spring.cloud.nacos.discovery.group}
application.yml
feign:
lient:
config:
default:
connectTimeout: 50000
client:
config:
default:
readTimeout: 190000
okhttp:
enabled: true
httpclient:
enabled: false
max-connections: 1000
max-connections-per-route: 100
connection-timeout: 190000
connection-timer-repeat: 190000
server:
undertow:
max-http-post-size: 0
# 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程,数量和CPU 内核数目一样即可
io-threads: 4
# 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载 io-threads*8
worker-threads: 32
# 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
# 每块buffer的空间大小,越小的空间被利用越充分
buffer-size: 1024
# 每个区分配的buffer数量 , 所以pool的大小是buffer-size * buffers-per-region
# buffers-per-region: 1024 # 这个参数不需要写了
# 是否分配的直接内存
direct-buffers: true
management:
endpoints:
web:
exposure:
include: "*"
启动类
package com.liu.news.article.reception;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author lx
* @version 1.0
* @description: TODO
* @date 2021/7/19 14:34
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ArticleReceptionApplication {
public static void main(String[] args) {
SpringApplication.run(ArticleReceptionApplication.class, args);
}
}
controller
package com.liu.news.article.reception.controller;
import com.liu.news.article.reception.service.ArticleReceptionFeignService;
import com.liu.news.result.R;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author lx
* @version 1.0
* @description: TODO
* @date 2021/7/27 9:33
*/
@RestController
public class ArticleReceptionController {
@Resource
private ArticleReceptionFeignService articleReceptionFeignService;
@GetMapping(value = "/articleReception/articleList", produces = MediaType.APPLICATION_JSON_VALUE)
public String getPaymentById(){
return articleReceptionFeignService.list();
}
}
package com.liu.news.article.reception.service;
import com.liu.news.result.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
@Component
@FeignClient(name = "ap-artical" )
public interface ArticleReceptionFeignService {
@GetMapping(value = "/ApArticle/list")
public String list();
}
启动服务