自学实前后端践项目3 Spring Cloud微服务 3
二.服务消费者整合menu
创建新的文件夹
创建服务消费者依赖
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId><version>2.0.2.RELEASE</version></dependency> </dependencies>
创建消费者bootstrap.yml
spring:application:name: clientprofiles:active: devcloud:config:uri: http://localhost:8762fail-fast: true
配置中心创建连接配置(服务消费者)
server:port: 8030 spring:application:name: clientthymeleaf:prefix: classpath:/static/suffix: .htmleureka:client:service-url:defaultZone: http://localhost:8761/eureka/instance:prefer-ip-address: true
ClientApplication 启动类 package com.redhat;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableFeignClients
public class ClientApplication {public static void main(String[] args) {SpringApplication.run(ClientApplication.class,args);} }测试menu创建feign接口 先把menu里面的Menu类复制到client来创建的entity中创建feign接口
package com.redhat.feign;import com.redhat.entity.Menu; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable;import java.util.List;@FeignClient(value = "menu") public interface MenuFeign {@GetMapping("/menu/findAll/{index}/{limit}")public List<Menu> findAll(@PathVariable("index")int index, @PathVariable("limit")int limit); }创建
package com.redhat.controller;import com.redhat.entity.Menu; import com.redhat.feign.MenuFeign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController @RequestMapping("/client") public class ClientHandler {@Autowiredprivate MenuFeign menuFeign;@GetMapping("/findAll/{index}/{limit}")public List<Menu>findAll(@PathVariable("index") int index,@PathVariable("limit") int limit){return menuFeign.findAll(index,limit);} } 启动服务测试功能
下一步在client文件下创建已分享相关前端及配置
链接:https://pan.baidu.com/s/10u21aq60Rzs1AWnizCYpag
提取码:r9va
修改一下clienthandler
package com.redhat.controller;import com.redhat.entity.Menu; import com.redhat.feign.MenuFeign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;@Controller @RequestMapping("/client") public class ClientHandler {@Autowiredprivate MenuFeign menuFeign;@GetMapping("/findAll/{index}/{limit}")public List<Menu>findAll(@PathVariable("index") int index,@PathVariable("limit") int limit){return menuFeign.findAll(index,limit);}@GetMapping("/redirect/{location}")public String redirect(@PathVariable("location") String location){ return location;} }
把新加的文件重建一下工程加入到目标文件中
重新启动client测试访问
这个框架可以查找Layui官方文档进行了解和学习
调用数据接口再修改clienthandler代码如下
package com.redhat.controller;import com.redhat.entity.Menu; import com.redhat.feign.MenuFeign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;import java.util.List;@Controller @RequestMapping("/client") public class ClientHandler {@Autowiredprivate MenuFeign menuFeign;@GetMapping("/findAll")@ResponseBodypublic List<Menu>findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){int index=(page-1)*limit;return menuFeign.findAll(index,limit);}@GetMapping("/redirect/{location}")public String redirect(@PathVariable("location") String location){ return location;} } 下面提示表示返回的值数据已经有了但是数据格式不对,必须返回menu对数据进行封装
封装数据
package com.redhat.entity;import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;import java.util.List;@Data @AllArgsConstructor @NoArgsConstructor public class MenuVO {private int code;private String msg;private int count;private List<Menu> data; } 将它复制到client对应位置等待调用
修改client文件下的feign
package com.redhat.feign;import com.redhat.entity.MenuVO; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable;@FeignClient(value = "menu") public interface MenuFeign {@GetMapping("/menu/findAll/{index}/{limit}")public MenuVO findAll(@PathVariable("index")int index, @PathVariable("limit")int limit); }
修改client文件下的handler
package com.redhat.controller;import com.redhat.entity.MenuVO; import com.redhat.feign.MenuFeign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;@Controller @RequestMapping("/client") public class ClientHandler {@Autowiredprivate MenuFeign menuFeign;@GetMapping("/findAll")@ResponseBodypublic MenuVO findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){int index=(page-1)*limit;return menuFeign.findAll(index,limit);}@GetMapping("/redirect/{location}")public String redirect(@PathVariable("location") String location){ return location;} } 最后把前端配置修改为
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all"> </head> <body> <div class="layui-container" style="width: 700px;height: 600px;margin-top: 0px;padding-top: 60px;"><div style="margin-left: 460px; width: 200px;">欢迎回来! <button class="layui-btn layui-btn-warm layui-btn-radius">退出</button></a></div><table class="layui-hide" id="test" lay-filter="test"></table><script type="text/html" id="barDemo"><a class="layui-btn layui-btn-xs" lay-event="order">订购</a></script><script th:src="@{/layui/layui.js}" charset="utf-8"></script><script>layui.use('table', function(){var table = layui.table;table.render({elem: '#test',url:'/client/findAll',title: '菜单列表',cols: [[{field:'id', width:100, title: '编号', sort: true},{field:'name', width:200, title: '菜品'},{field:'price', width:100, title: '单价'},{field:'flavor', width:100, title: '口味'},{field:'tid',width:100, title: '分类',templet:function(data){return "三鲜"}},{fixed: 'right', title:'操作', toolbar: '#barDemo', width:70}]],page: true});//监听行工具事件table.on('tool(test)', function(obj){var data = obj.data;if(obj.event === 'order'){window.location.href="/order/save/"+data.id;}});});</script></div> </body> </html>
因为复制了封装类所以重建一下工程然后启动各服务
测试如下
自学实前后端践项目3 Spring Cloud微服务 3相关推荐
- 自学实前后端践项目3 Spring Cloud微服务 8
九.后台管理系统 1.前面基本上的功能都已经实现了,最后就行优化和界面管理! 1)过滤器接口实现直接访问主页面的时候需要先登录的功能 在client里面创建filter创建UserFilter 类然后 ...
- 自学实前后端践项目3 Spring Cloud微服务 6
六.服务消费者整合user 先添加两个前端: 链接:https://pan.baidu.com/s/1phn9ejGF_21Gp-WGJ9qsCA 提取码:ul6d 在User下创建封装VO pac ...
- 自学实前后端践项目3 Spring Cloud微服务 2
创建服务提供者menu 数据库文件设计资料 -- MySQL dump 10.13 Distrib 8.0.11, for macos10.13 (x86_64) -- -- Host: 127.0 ...
- 自学实前后端践项目2 phone Store 1
phone Store 基于移动端的手机 基于Spring Boot +Vue 前端技术栈 Vue+Vant UI +less 相关插件安装 npm i vant -S npm i axios -S ...
- 自学实前后端践项目4 MMall商城 1
一.开发环境 1.JDK8以上+Spring Boot 2.3.0+Thymeleaf+MyBatis Plus3.3.1+MySQL8.0+ 2.部署:Linux,,(阿里云 腾讯云)JDK8+,M ...
- 从天气项目看Spring Cloud微服务治理
网上搜集的资源,个人感觉还行,分享了 从天气项目看Spring Cloud微服务治理 网盘地址:https://pan.baidu.com/s/1ggn5uld 密码: n6bn 备用地址(腾讯微云) ...
- Servlet+MyBatis项目转Spring Cloud微服务,多数据源配置修改建议
一.项目需求 在开发过程中,由于技术的不断迭代,为了提高开发效率,需要对原有项目的架构做出相应的调整. 二.存在的问题 为了不影响项目进度,架构调整初期只是把项目做了简单的maven管理,引入spri ...
- Spring Cloud 微服务项目操作实战流程(完结)
Spring Cloud入门项目操作实战流程 Day01~02 〇.Service - 业务服务结构 商品服务 item service,端口 8001 用户服务 user service,端口 81 ...
- Java之 Spring Cloud 微服务的 SpringCloud Config 配置中心(第四个阶段)【二】【SpringBoot项目实现商品服务器端调用】
SpringCloud学习目录点击跳转对应的文章 Java之 Spring Cloud 微服务搭建(第一个阶段)[一][SpringBoot项目实现商品服务器端是调用] Java之 Spring Cl ...
最新文章
- private关键字和构造方法
- codeviz安装使用全记录
- 22种代码坏味道及重构手段
- Google I/O 2019上提及的Javascript新特性
- python面试-Python面试
- CTF web题总结--http header 修改、cookie注入
- 给一个Table添加合计行[Tips]
- JavaScript常见面试题和答案
- 借助格式化输出过canary保护
- c 语言str.size,C/C++ strlen(str)和str.length()和str.size()的区别
- 第十届蓝桥杯大赛青少年创意编程C++组省赛 第2题 小猫吃鱼
- PHP类UTF8编码内的繁简转换-繁体-简体
- Android开发笔记(七十八)异常容错处理
- redis 公网 安全_redis配置之安全配置
- 强大的日志分析工具AWStats
- Caffe学习:使用pycaffe绘制网络结构
- piano+apk+android,PianOli | F-Droid - Free and Open Source Android App Repository
- java软件测试技术栈
- 360 自动 html 极速模式,用Meta标签代码让360双核浏览器默认极速模式打开网站不是兼容模式(顺带解决很多兼容性问题)...
- Unable to find a single main class from the following candidates 。。。
热门文章
- JVM日历:Java 2018大事回顾
- ✠OpenGL-4-管理3D图形数据
- Unity的Realtime GI, Probe Volumes, LOD Groups
- 凯哥自媒体:要不要辞职做自媒体?看过本文再做决定!
- ICRA2023|基于时空融合的驾驶场景视频雨滴移除算法+数据集
- 实战篇:SEK之买卖方向成交量分析
- element-vue 如何在文本框后添加文字
- 记国产数据库人大金仓使用
- 两招挽救Office word文档中的乱码(转)
- 计算机毕业设计SpringBoot美容院后台管理系统[包运行成功]