大家好,又见面了,我是你们的朋友全栈君。
数据库
父工程
1、建 Module:supergo_parent
2、改 pom
代码语言:javascript复制<?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.supergo</groupId>
<artifactId>supergo_parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--声明为父项目-->
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--管理子工程: 1,锁定坐标版本 2,管理子工程(统一打包,部署) -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
<mybatis.starter.version>1.3.2</mybatis.starter.version>
<mapper.starter.version>2.0.2</mapper.starter.version>
<druid.starter.version>1.1.10</druid.starter.version>
<mysql.version>5.1.32</mysql.version>
<pageHelper.starter.version>1.2.3</pageHelper.starter.version>
<fastDFS.client.version>1.27.0.0</fastDFS.client.version>
<jaxb.version>2.3.0</jaxb.version>
<activation>1.1.1</activation>
<springfox>2.2.2</springfox>
<lambok>1.16.20</lambok>
<fastjson>1.2.9</fastjson>
<json-token>0.9.0</json-token>
<joda-time>2.9.7</joda-time>
<comment-lang>3.9</comment-lang>
<aliyun-dayu-core>3.3.1</aliyun-dayu-core>
<aliyun-dayu-sdk>1.0.0</aliyun-dayu-sdk>
<swagger-boot>1.9.0.RELEASE</swagger-boot>
<wxpay.version>0.0.3</wxpay.version>
<lcn.last.version>4.1.0</lcn.last.version>
<httpclient.version>4.5.3</httpclient.version>
<codingapi.txlcn.version>5.0.2.RELEASE</codingapi.txlcn.version>
</properties>
<!--管理坐标-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>${fastDFS.client.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun-dayu-core}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>${aliyun-dayu-sdk}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${json-token}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${comment-lang}</version>
</dependency>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.starter.version}</version>
</dependency>
<!-- 通用Mapper启动器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.starter.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pageHelper.starter.version}</version>
</dependency>
<!-- druid启动器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.starter.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>${activation}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lambok}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson}</version>
</dependency>
<!--微信支付-->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>${wxpay.version}</version>
</dependency>
<!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<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>
3、删除 src目录
搭建 Eureka 注册中心
1、建 Module :supergo_eureka7001
2、改 pom
代码语言:javascript复制<?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>supergo_parent</artifactId>
<groupId>com.supergo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>supergo_eureka10086</artifactId>
<!--注册中心: 服务端-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3、建 yml
代码语言:javascript复制# 端口
server:
port: 7001
eureka:
instance:
# 单机版【hostname: 127.0.0.1 # Eureka 服务端的实例名称】
# 集群
hostname: eureka7001.com
client:
register-with-eureka: false # 表示不向注册中心注册自己
fetch-registry: false # 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
# 设置与eureka server 交互的地址查询和注册服务都需要依赖这个地址】
# 单机版【defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/
# 集群:需要去守望另外一台注册中心
# defaultZone: http://eureka7002.com:7002/eureka/
# 注册中心的自我保护机制【默认是开启的】
server:
enable-self-preservation: false # 关闭自我保护机制,保证不可用的服务及时删除
eviction-interval-timer-in-ms: 2000 # 改变时间间隔[失效服务剔除]
4、启动类
代码语言:javascript复制package com.xj0927;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //发现 Eureka
public class Eureka7001 {
public static void main(String[] args) {
SpringApplication.run(Eureka7001.class, args);
}
}
添加基础模块
这些基础模块是可重用的,不仅是本项目,其它项目也可使用
涉及到的基础模块:
- supergo-common
- supergo-mapper
- supergo-pojo
- supergo-base-service
supergo-common
该模块主要是工具类和通用类,基类等
比如:
代码语言:javascript复制package com.supergo.http;
import java.io.Serializable;
/** * HTTP结果封装:如果请求处理正常就调用ok方法,如果有数据返回就将数据封装返回; 如果请求出错,就调用error方法,返回出错状态码 信息 */
public class HttpResult implements Serializable{
private int code = 200;
private String msg;
private Object data;
public static HttpResult error() {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");
}
public static HttpResult error(String msg) {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
}
public static HttpResult error(int code, String msg) {
HttpResult r = new HttpResult();
r.setCode(code);
r.setMsg(msg);
return r;
}
public static HttpResult ok(String msg) {
HttpResult r = new HttpResult();
r.setMsg(msg);
return r;
}
public static HttpResult ok(Object data) {
HttpResult r = new HttpResult();
r.setData(data);
return r;
}
public static HttpResult ok() {
return new HttpResult();
}
.....
}
分页请求实体类:
代码语言:javascript复制package com.supergo.page;
import java.io.Serializable;
/** * @Description: 封装分页的请求参数 * @Author: xj0927 * @Date Created in 2020/12/23 12:56 */
public class PageRequest implements Serializable{
private Integer currentPage=1;
private Integer pageSize=10;
....
}
分页结果实体类:
代码语言:javascript复制package com.supergo.page;
import java.io.Serializable;
import java.util.List;
/** * @Description: 封装分页的结果 * @Author: xj0927 * @Date Created in 2020/12/23 12:56 */
public class PageResult implements Serializable {
private Long total;
private int page=1;
private List<?> rows;
private int pageSize=10;
public PageResult() {
}
public PageResult(Long total, List<?> rows) {
this.total = total;
this.rows = rows;
}
public PageResult(Long total, List<?> rows,int page) {
this.total = total;
this.rows = rows;
this.page = page;
}
......
}
全部工具类:
spergo-mapper
存放生成的 Mapper 接口和 映射文件,而实体类抽取出来放在另一个模块 supergo-pojo 中,然后引入到该模块中使用
本项目使用的是 tk-mybatis,如何使用插件反向生成接口、实体类和映射文件,可参照:https://blog.csdn.net/XJ0927/article/details/111590559
贴一下该模块使用到的 pom 依赖:
代码语言:javascript复制<?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>supergo_parent</artifactId>
<groupId>com.supergo</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>supergo-mapper</artifactId>
<dependencies>
<!--引入pojo:主要是实体类-->
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--数据库相关东西-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--tk-mybatis 反向生成所需要的的插件-->
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>com.github.misterchangray.mybatis.generator.plugins</groupId>
<artifactId>myBatisGeneratorPlugins</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
supergo-pojo
用于存放实体类
supergo-base-service
存放 service 层的接口和实现类的通用基类,通过它我们具体的 service 就不用了在写 crud 操作了,类比 mybatis plus 中的 Service 和 ServiceImpl 这两个类
引入 pom
代码语言:javascript复制<?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>supergo_parent</artifactId>
<groupId>com.supergo</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>supergo-base-service</artifactId>
<dependencies>
<!--存放的映射文件和接口,因为该模块引入了实体模块,所以也间接引入了实体类-->
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo-mapper</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
接口规范
代码语言:javascript复制package com.supergo.service.base;
import com.supergo.page.PageResult;
import com.supergo.http.HttpResult;
import java.io.Serializable;
import java.util.List;
/** * 功能描述:增删改查通用service */
public interface BaseService<T> {
/** * 根据主键查询 * * @param id 主键 * @return 实体对象 */
public T findOne(Serializable id);
/** * 查询全部 * * @return 实体对象集合 */
public HttpResult findAll();
/** * 根据条件查询列表 * * @param t 查询条件对象 * @return */
public List<T> findByWhere(T t);
/** * 分页查询列表 * * @param page 页号 * @param rows 页大小 * @return 分页实体对象 */
public PageResult findPage(Integer page, Integer rows);
/** * 根据条件分页查询列表 * * @param page 页号 * @param rows 页大小 * @param t 查询条件对象 * @return 分页实体对象 */
public PageResult findPage(Integer page, Integer rows, T t);
/** * 新增 * * @param t 实体对象 */
public void add(T t);
/** * 根据主键更新 * * @param t 实体对象 */
public void update(T t);
/** * 批量删除 * * @param ids 主键集合 */
public void deleteByIds(Serializable[] ids);
}
实现类
代码语言:javascript复制package com.supergo.service.base.impl;
import java.io.Serializable;
import java.util.List;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supergo.service.base.BaseService;
import com.supergo.page.PageResult;
import com.supergo.http.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
/** * 功能描述:增删改查通用service实现类 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {
//spring 4.x 版本之后引入的泛型依赖注入
//通过该mapper,传入泛型,作用类似于xxxMapper
@Autowired
private Mapper<T> mapper;
@Override
public T findOne(Serializable id) {
return mapper.selectByPrimaryKey(id);
}
@Override
public HttpResult findAll() {
return HttpResult.ok(mapper.selectAll());
}
@Override
public List<T> findByWhere(T t) {
return mapper.select(t);
}
@Override
public PageResult findPage(Integer page, Integer rows) {
PageHelper.startPage(page, rows);
List<T> list = mapper.selectAll();
PageInfo<T> pageInfo = new PageInfo<>(list);
return new PageResult(pageInfo.getTotal(),
pageInfo.getList(),page);
}
//分页方法:使用的是PageHelper进行分页
//可以增加限制条件进行分页
@Override
public PageResult findPage(Integer page, Integer rows, T t) {
PageHelper.startPage(page, rows);
List<T> list = mapper.select(t);
PageInfo<T> pageInfo = new PageInfo<>(list);
return new PageResult(pageInfo.getTotal(),
pageInfo.getList(),page);
}
@Override
public void add(T t) {
mapper.insertSelective(t);
}
@Override
public void update(T t) {
mapper.updateByPrimaryKeySelective(t);
}
@Override
public void deleteByIds(Serializable[] ids) {
if (ids != null && ids.length > 0) {
for (Serializable id : ids) {
mapper.deleteByPrimaryKey(id);
}
}
}
}
搭建品牌微服务提供者
这里加了一层 feign 进行调用:
- supergo_manager_service8001:包含我们的实际业务逻辑
- supergo_manager_feign:客户端 Feign 调用的接口定义
supergo_manager 父模块
1、建父 Module:supergo_manager
2、改 pom
代码语言:javascript复制<?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>supergo_parent</artifactId>
<groupId>com.supergo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>supergo_manager</artifactId>
<!--声明为父项目-->
<packaging>pom</packaging>
<!--子模块-->
<modules>
<module>supergo_manager_service8001</module>
<module>supergo_manager_feign</module>
</modules>
</project>
supergo_manager_service8001 业务模块
实现品牌、商品等管理的微服务工程
1、建 Module:supergo_manager_service8001
2、改 pom
代码语言:javascript复制<?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>supergo_manager</artifactId>
<groupId>com.supergo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>supergo_manager_service</artifactId>
<dependencies>
<!--web 与 actuator监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--hystrix 熔断器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--eureka 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--公用service模块-->
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo-base-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--公用 mapper模块-->
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo-mapper</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、建 yml
代码语言:javascript复制# 端口
server:
port: 8001
# 名字
spring:
application:
name: supergo-manager # 代表的就是我以什么样的名字入驻进的注册中心
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动类
url: jdbc:mysql://127.0.0.1:3306/supergo?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: 123456
# druid 专属配置
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 1000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-open-prepared-statements: 50
max-pool-prepared-statement-per-connection-size: 20
# stat是统计,wall是SQL防火墙,防SQL注入的,log4j是用来输出统计数据的
# filters: stat,wall,log4j,config
##是否启用StatFilter默认值true: 排除一些不必要的url
web-stat-filter:
enabled: true
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
#是否启用StatViewServlet默认值true
stat-view-servlet:
allow: 127.0.0.1 #IP 白名单
url-pattern: /druid/* #监控地址,默认 /druid/*
login-username: admin
login-password: admin #
# deny:IP #黑名单
# 最大请求文件的大小
servlet:
multipart:
max-request-size: 5MB
mybatis:
type-aliases-package: com.supergo.pojo #所有别名类所在的包
eureka:
client:
register-with-eureka: true # 表示将自己注册到 eureka server ,默认为 true
fetch-registry: true # 表示是否从eureka server 抓取已有的注册信息,默认为true。单节点为所谓,集群必须为 true,才能配合ribbon使用负载均衡
service-url:
# 单机版:只用注册进一个服务中心【defaultZone: http://127.0.0.1:7001/eureka/】
defaultZone: http://eureka7001.com:7001/eureka/
# 集群版:需要同时注册进每个注册中心
# defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com/eureka/
# 显示的服务主机名称
instance:
prefer-ip-address: true # 访问路径显示 ip【统一:方便调试】
ip-address: 127.0.0.1
instance-id: ${
eureka.instance.ip-address}.${
server.port}
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 10
#网关设置了根路径,默认监控路径发生了变化
health-check-url-path: /api/actuator/health
#actuator服务监控与管理
management:
endpoint:
#开启端点
shutdown:
enabled: true
health:
show-details: always
# 加载所有的端点
endpoints:
web:
exposure:
include: "*"
4、启动类
代码语言:javascript复制package com.supergo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@EnableEurekaClient //注册进注册中心
@MapperScan("com.supergo.mapper") //扫描引入的公用即接口包
public class ManagerService8001 {
public static void main(String[] args) {
SpringApplication.run(ManagerService8001.class, args);
}
}
5、service 层
接口:
代码语言:javascript复制package com.supergo.service;
import com.supergo.pojo.Brand;
import com.supergo.service.base.BaseService;
/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-23 18:40 * @Modified By: */
public interface BrandService extends BaseService<Brand> {
}
实现类:
代码语言:javascript复制package com.supergo.service.imp;
import com.supergo.pojo.Brand;
import com.supergo.service.base.impl.BaseServiceImpl;
import com.supergo.service.BrandService;
import org.springframework.stereotype.Service;
/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-23 18:41 * @Modified By: */
@Service
public class BrandServiceImpl extends BaseServiceImpl<Brand> implements BrandService {
}
6、controller 层
代码语言:javascript复制package com.supergo.controller;
import com.supergo.pojo.Brand;
import com.supergo.http.HttpResult;
import com.supergo.page.PageResult;
import com.supergo.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-23 19:25 * @Modified By: */
@RestController
@RequestMapping("/brand")
public class BrandController {
@Autowired
private BrandService brandService;
//分页查询
@PostMapping("/query/{page}/{rows}")
public HttpResult findPage(@RequestBody Brand brand, @PathVariable Integer page, @PathVariable Integer rows) {
try {
PageResult result = brandService.findPage(page, rows, brand);
//查询成功
return HttpResult.ok(result);
} catch (Exception e) {
e.printStackTrace();
//查询异常
return HttpResult.error();
}
}
}
7、测试
使用 postman 测试
至此,表示服务提供者提供的服务没有问题
supergo_manager_feign调用模块
1、建 Moduel:supergo_manager_feign
2、改 pom
代码语言:javascript复制<?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>supergo_manager</artifactId>
<groupId>com.supergo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>supergo_manager_feign</artifactId>
<dependencies>
<!--服务调用openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--工具类-->
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--实体类-->
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
3、调用接口
代码语言:javascript复制package com.supergo;
import com.supergo.pojo.Brand;
import com.supergo.http.HttpResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/** * @Author: xj0927 * @Description: * @Date Created in 2020-12-23 21:24 * @Modified By: */
@FeignClient(value = "supergo-manager")//发现服务提供者
public interface ApiBrandFeign {
//分页查询
@PostMapping("/brand/query/{page}/{rows}")
public HttpResult findPage(@RequestBody Brand brand, @PathVariable Integer page, @PathVariable Integer rows);
}
4、使用
其他模块,需要使用该服务时,便可以通过该接口进行服务调用
搭建品牌微服务调用者
1、建 Module:supergo_manager_web9001
2、改 pom
代码语言:javascript复制<?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>supergo_parent</artifactId>
<groupId>com.supergo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>supergo_manager_web9002</artifactId>
<dependencies>
<!--eureka 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--hystrix熔断依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--在feign模块使用了tk-mybatis进行数据库的访问, 这里引入feign,会间接引入tk-mybatis 需要排除tk-mybatis,不然会要求进行数据库配置 -->
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo_manager_feign</artifactId>
<exclusions>
<exclusion>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!-- 工具类 -->
<dependency>
<groupId>com.supergo</groupId>
<artifactId>supergo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、建 yml
代码语言:javascript复制# 端口号【openfeign 也是使用在客户端,集成了ribbon】
server:
port: 9001
spring:
application:
name: supergo-manager-web9001
eureka:
client:
register-with-eureka: true # 注册进注册中心
service-url:
# 集群版
# defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
# 单机版
defaultZone: http://eureka7001.com:7001/eureka/
# 显示的服务主机名称
instance:
prefer-ip-address: true # 访问路径显示 ip【统一:方便调试】
ip-address: 127.0.0.1
instance-id: ${
eureka.instance.ip-address}.${
server.port}
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 10
#actuator服务监控与管理
management:
endpoint:
#开启端点
shutdown:
enabled: true
health:
show-details: always
# 加载所有的端点
endpoints:
web:
exposure:
include: "*"
# 设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
# 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
ReadTimeout: 5000
# 指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
logging:
level:
# feign日志以什么级别监控哪个接口
com.supergo.ApiBrandFeign: debug
4、启动类
代码语言:javascript复制package com.supergo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient //注册进注册中心
@EnableFeignClients //发现feign服务
public class ManagerWeb9001 {
public static void main(String[] args) {
SpringApplication.run(ManagerWeb9001.class, args);
}
}
5、controller层
代码语言:javascript复制package com.supergo.controller;
import com.supergo.ApiBrandFeign;
import com.supergo.http.HttpResult;
import com.supergo.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/** * @Author: xj0927 * @Description: feign服务调用者 * @Date Created in 2020-12-23 22:01 * @Modified By: */
@RestController
@RequestMapping("/brand")
public class BrandController {
//feigm服务api接口
@Autowired
private ApiBrandFeign brandFeign;
@PostMapping("/query/{page}/{rows}")
public HttpResult getBrandList(@RequestBody Brand brand, @PathVariable Integer page, @PathVariable Integer rows) {
//feign服务
HttpResult result = brandFeign.findPage(brand, page, rows);
return result;
}
}
6、测试
使用 postman 测试
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152820.html原文链接:https://javaforall.cn