微服务相关流程
#注册中心
server:
port: 8761
eureka:
instance:
hostname: localhost
#将来注册到注册中心的微服务包括注册中心都是用ip地址
preferIpAddress: true
client:
#是否注册到注册中心 注册中心自己不需要注册
registerWithEureka: false
#是否抓取注册中的注册信息
fetchRegistry: false
serviceUrl:
#微服务和客户端用来注册和发现的地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
maven依赖管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MyServerMain {
public static void main(String[] args) {
new SpringApplicationBuilder(MyServerMain.class).web(true).run(args);
}
}
注册中心我是打成jar包部署在linux系统上,方便操作,然后就是视图html跟service逻辑处理
html静态页面
controll层
package cn.ps.controll;
import cn.ps.enity.Emp;
import cn.ps.enity.Results;
import cn.ps.mapper.FeignMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class EmpController {
@Autowired
private FeignMapper mapper;
@GetMapping(value="/emps")
public Results queryEmp(String ename, String sal, String page,String limit){
Results empList = null;
try {
empList=mapper.queryEmp(ename,sal,page,limit);
} catch (Exception e) {
e.printStackTrace();
}
return empList;
}
@PostMapping(value="/addEmp")
public Results saveEmp(Emp emp){
Results gh=new Results();
//新增
try{
mapper.saveEmp(emp);
}catch(Exception e){
gh.setCode(1);
gh.setMsg("新增失败"+e.getMessage());
}
return gh;
}
@DeleteMapping(value="addEmp/{empno}")
public Results deleteEmp(@PathVariable String empno)throws Exception{
Results gh=new Results();
try{
mapper.deleteEmp(empno);
}catch(Exception e){
gh.setCode(1);
gh.setMsg("删除失败"+e.getMessage());
}
return gh;
}
@PutMapping(value="updateEmp/{empno}")
public Results updateEmp(@PathVariable String empno,Emp emp)throws Exception{
Results gh=new Results();
emp.setEmpno(empno);
try{
mapper.updateEmp(emp);
}catch(Exception e){
System.out.println(e.getMessage());
gh.setCode(1);
gh.setMsg("修改失败"+e.getMessage());
}
return gh;
}
}
import lombok.Data; //需要下载插件小辣椒
@Data
public class Emp {
private String empno;
private String ename;
private String job ;
private String sal ;
private String email ;
private String phone;
}
@Data
@NoArgsConstructor
public class Results {
private int code;
private String msg;
private int count;
private List data;
}
@FeignClient(name="USERSERVICE")
public interface FeignMapper {
@GetMapping("/query")
public Results queryEmp(@RequestParam("ename") String ename,@RequestParam("sal") String sal,@RequestParam("curPage") String curPage, @RequestParam("pageNum")String pageNum);
@PostMapping("/save")
public void saveEmp(@RequestBody Emp emp);
@DeleteMapping("/del/{empno}")
public void deleteEmp(@PathVariable("empno") String empno);
@PutMapping("/update")
public void updateEmp(@RequestBody Emp emp);
eureka:
instance:
preferIpAddress: true
hostname: localhost
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.22.130:8761/eureka/
spring:
application:
name: userui
server:
port: 80
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
ps:需要在resource文件目录下创建static/public文件,用来放置layui,html等文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="layui-v2.4.5/layui/css/layui.css">
<script src="layui-v2.4.5/layui/layui.js"></script>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript">
layui.use('form', function() {
var form = layui.form;
form.on('submit(myForm)', function(data) {
layui.table.reload('myTable', {
url : 'emps',
where : data.field,
page : {
curr : 1
//重新从第 1 页开始
}
});
return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});
form.on('submit(addForms)', function(data) {
$.ajax({
url : 'addEmp',
dataType : 'json',
data : data.field,
type : 'post',
success : function(da) {
if (da.code == 0) {
alert("新增成功")
layer.close(getDispe);
layui.table.reload('myTable', {
url : 'emps'
});
} else {
alert(da.msg);
}
},
});
return false;
});
form.on('submit(updateForms)', function(data) {
data.field["_method"] = "put";
$.ajax({
url : 'updateEmp/' + data.field.empno,
dataType : 'json',
data : data.field,
type : 'post',
success : function(da) {
if (da.code == 0) {
alert("修改成功")
layer.close(getDispe);
layui.table.reload('myTable', {
url : 'emps'
});
} else {
alert(da.msg());
}
}
});
return false;
});
}),
layui.use("table",function() {
var table = layui.table;
table.render({
elem : '#myTable',
height : '300',
url : 'emps',//数据接口
page : true,//开启分页
cols : [ [ {type : 'checkbox'},
{field : 'empno',title : '员工编号',width : '12%',sort : true},
{field : 'ename',title : '员工姓名',width : '12%'},
{field : 'sal',title : '员工工资',width : '12%'},
{field : 'job',title : '员工职位',width : '12%'},
{field : 'email',title : '员工邮箱',width : '12%'},
{field : 'phone',title : '员工电话',width : '12%'},
{field : 'operate',title : '操作',width : '12%',
templet : function(d) {
return ""
}
},
] ]
});
});
var getDispe = null;
function add() {
layui.use('layer', function() {
getDispe = layer.open({
type : 1,
area : [ '500px', '400px' ],
content : $("#addForm")
});
});
}
function toDelete(empno) {
$.ajax({
url : 'addEmp/' + empno,
dataType : 'json',
data : {
"_method" : "delete"
},
type : 'post',
success : function(da) {
if (da.code == 0) {
alert("删除成功");
layui.table.reload('myTable', {
url : 'emps'
})
} else {
alert(da.msg());
}
}
});
return false;
}
function toUpdate(ename, job, sal, email, phone, empno) {
$("#updateForm input[name='ename']").val(ename);
$("#updateForm input[name='job']").val(job);
$("#updateForm input[name='sal']").val(sal);
$("#updateForm input[name='email']").val(email);
$("#updateForm input[name='phone']").val(phone);
$("#updateForm input[name='empno']").val(empno);
layui.use('layer', function() {
getDispe = layer.open({
type : 1,
area : [ '500px', '400px' ],
content : $("#updateForm")
});
});
}
</script>
</head>
<body>
<form class="layui-form" action="">
<div class="layui-inline">
<label class="layui-form-label">员工姓名</label>
<div class="layui-input-inline">
<input type="text" name="ename" autocomplete="off"
class="layui-input">
</div>
<label>员工工资</label>
<div class="layui-input-inline">
<input type="text" name="sal" autocomplete="off"
class="layui-input">
</div>
<div class="layui-input-inline" style="width: 200px;">
<button class="layui-btn" lay-submit lay-filter="myForm">查询</button>
<a href="javascript:add()" class="layui-btn">新增</a>
</div>
</div>
</form>
<table id="myTable">
</table>
</body>
</html>
<form id="addForm" class="layui-form" style="display: none">
<div class="layui-form-item">
<label class="layui-form-label">员工姓名</label>
<div class="layui-input-block">
<input type="text" name="ename" lay-verify="required"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">员工职位</label>
<div class="layui-input-block">
<input type="text" name="job" lay-verify="required"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">员工工资</label>
<div class="layui-input-block">
<input type="text" name="sal" lay-verify="number" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">员工邮箱</label>
<div class="layui-input-block">
<input type="text" name="email" lay-verify="email" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">员工电话</label>
<div class="layui-input-block">
<input type="text" name="phone" lay-verify="phone" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="addForms">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
<form id="updateForm" class="layui-form" style="display: none">
<div class="layui-form-item">
<label class="layui-form-label">员工编号</label>
<div class="layui-input-block">
<input type="text" name="empno" required lay- verify="required"
autocomplete="off" class="layui-input" readonly="readonly">
</div>
</div>
<div class="layui-form-item">
<label id="ename" class="layui-form-label">员工姓名</label>
<div class="layui-input-block">
<input type="text" name="ename" required lay- verify="required"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label id="job" class="layui-form-label">员工职位</label>
<div class="layui-input-block">
<input type="text" name="job" required lay- verify="required"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label id="sal" class="layui-form-label">员工工资</label>
<div class="layui-input-block">
<input type="text" name="sal" required lay- verify="number"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label id="email" class="layui-form-label">员工邮箱</label>
<div class="layui-input-block">
<input type="text" name="email" required lay- verify="email"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label id="phone" class="layui-form-label">员工电话</label>
<div class="layui-input-block">
<input type="text" name="phone" required lay- verify="phone"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="updateForms">保存</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
@RestController
public class EmpServiceImpl{
@Autowired
private EmpMapper empMapper;
@GetMapping("/query")
public Results queryEmp( String ename, String sal, String curPage,String pageNum) {
//req请求获取的是字符串 因此转成int类型
int curPageInt=Integer.parseInt(curPage);
int pageNumInt=Integer.parseInt(pageNum);
if(StringUtils.isEmpty(ename)) {
ename = "";
}
//条件构造器
QueryWrapper wrapper = new QueryWrapper();
wrapper.like("ename", ename);
if(StringUtils.isNotEmpty(sal)) {
wrapper.le("sal", Integer.parseInt(sal));
}
// 这里是分页传入的参数:页数和数据量
IPage<Emp> pageData=empMapper.selectPage(new Page(curPageInt,pageNumInt), wrapper);
Results result = new Results();
result.setCode(0);
result.setCount((int)pageData.getTotal());
result.setData(pageData.getRecords());
return result;
}
@PostMapping("/save")
public void saveEmp(@RequestBody Emp emp) {
empMapper.insert(emp);
}
@DeleteMapping("/del/{empno}")
public void deleteEmp(@PathVariable("empno")String empno) {
empMapper.deleteById(empno);
}
@PutMapping("/update")
public void updateEmp(@RequestBody Emp emp) {
empMapper.updateById(emp);
}
}
@Data
public class Emp {
@TableId(type= IdType.AUTO)
private String empno;
private String ename;
private String job ;
private String sal ;
private String email ;
private String phone;
}
@Data
@NoArgsConstructor
public class Results {
private int code;
private String msg;
private int count;
private List data;
}
@Mapper
public interface EmpMapper extends BaseMapper<Emp>{
}
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableEurekaClient
public class ServiceMain {
public static void main(String[] args) throws Exception {
SpringApplication.run(ServiceMain.class, args);
}
@EnableTransactionManagement
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class Config {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
}
eureka:
instance:
preferIpAddress: true
hostname: localhost
client:
registerWithEureka: true
fetchRegistry: false
serviceUrl:
defaultZone: http://192.168.22.130:8761/eureka/
spring:
application:
name: userservice
datasource:
url: jdbc:mysql://localhost/unit02
password: ps123456
driver-class-name: com.mysql.jdbc.Driver
username: root
server:
port: 8881
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>