框架整合搭建实战练习记录2
来源:互联网 发布:淘宝直播抽奖狂点屏幕 编辑:程序博客网 时间:2024/06/07 04:42
上一篇文章里实现了一个基本的用户服务程序
框架整合搭建实战练习记录1
这里对程序进行改造
由原先的单用户服务情况下增加Eureka服务注册中心、Zuul智能路由、用户服务里增加用户余额的业务、order订单服务程序(order里增加Feign服务,用来调用用户余额服务)。
过程简要记录,具体可以看代码https://github.com/li39000yun/cloud3
一、增加注册服务中心eureka
1、新建一个model,eureka-server
2、修改application.properties为application.yml,使用yaml格式,设置port端口号8761
server: port: 8761eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/3、启动类上增加注解@EnableEurekaServer
package com.lyq;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}
二、修改用户服务demo
1、增加用户余额功能,每个用户绑定一个账户金额
2、对外开放接口,实现两个功能
a)根据用户id查询余额
b)根据用户id修改余额
package com.lyq.controller;import com.lyq.model.AccTotal;import com.lyq.service.AccTotalService;import com.lyq.utils.AjaxJson;import org.apache.commons.lang.math.NumberUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * 账户余额对外接口 * Created by 云强 on 2017/11/12. */@RestControllerpublic class AccTotalRestController { @Autowired AccTotalService accTotalService; /** * 查询用户余额 * @param userId * @return */ @RequestMapping("/accTotal/find/{userId}") public AccTotal findByUserId(@PathVariable("userId") Long userId) { return accTotalService.findByUserId(userId); } /** * 修改账户余额 * 给指定账户添加余额,如需要减少,则传入负数 * @param userId 用户id * @param money 修改余额 * @return */ @RequestMapping("/accTotal/update/{userId}") public AjaxJson updateAccTotal(@PathVariable("userId") Long userId,Double money) { AjaxJson j = new AjaxJson(); try{ // 获取余额 AccTotal accTotal = accTotalService.findByUserId(userId); // 修改余额 accTotal.setAccTotal(accTotal.getAccTotal()+money); // 更新余额 accTotalService.updateTotal(accTotal); j.setObj(accTotal); } catch (Exception e) { j.setSuccess(false); j.setMsg(e.getMessage()); } return j; }}
二、增加订单服务order
1、新建一个model,order
2、实现订单基本的增删改查功能,订单包括用户id,和金额
3、启动类开启feign功能
package com.lyq;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.support.SpringBootServletInitializer;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication@EnableEurekaClient@EnableFeignClientspublic class OrderApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(OrderApplication.class); } public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); }}4、Feign接口实现相关类
接口调用类AccTotalService
package com.lyq.service;import com.lyq.model.AccTotal;import com.lyq.utils.AjaxJson;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;/** * 调用接口类 * Created by 云强 on 2017/11/10. */@FeignClient(value = "service-demo")public interface AccTotalService { /** * 查询用户余额 * * @param userId * @return */ @RequestMapping("/accTotal/find/{userId}") public AccTotal findByUserId(@PathVariable("userId") Long userId); /** * 修改账户余额 * 给指定账户添加余额,如需要减少,则传入负数 * * @param userId 用户id * @param money 修改余额 * @return */ @RequestMapping("/accTotal/update/{userId}") public AjaxJson updateAccTotal(@PathVariable("userId") Long userId,@RequestParam(value = "money") Double money);}
控制层实现订单余额修改调用功能
package com.lyq.controller;import com.lyq.model.OrderEntity;import com.lyq.service.AccTotalService;import com.lyq.service.OrderService;import com.lyq.utils.AjaxJson;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.*;import java.util.List;/** * 订单控制层 * Created by 云强 on 2017/11/8. */@RestControllerpublic class OrderController { @Autowired OrderService orderService; @Autowired AccTotalService accTotalService; @Value("${server.port}") String port; @RequestMapping("/hello") public String home(@RequestParam String name) { return "hi " + name + ",i am from port:" + port; } @RequestMapping("/order/get/{id}") public OrderEntity orderFindOne(@PathVariable("id") String id) { return orderService.findOrderById(id); } @RequestMapping("/order/getList") public List<OrderEntity> orderFindAll() { return orderService.findAll(); } @RequestMapping("/submit/{id}") public AjaxJson sumbit(@PathVariable("id") String id) { AjaxJson j = new AjaxJson(); try { OrderEntity orderEntity = orderService.findOrderById(id); return accTotalService.updateAccTotal(orderEntity.getUserId(), orderEntity.getMoney()); } catch (Exception e) { j.setSuccess(false); j.setMsg(e.getMessage()); } return j; }}
5、界面增加提交功能,当提交时调用用户账户服务,通过feign调用服务修改账户金额
三、增加zuul路由网关功能(作为客户端统一入口)
1、新建model:service-zuul
2、application.yml配置如下,增加用户及用户账户服务(demo)和订单服务(order)路由规则,端口号设置8769
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8769spring: application: name: service-zuulzuul: routes: api-a: path: /api-a/** serviceId: service-demo api-b: path: /api-b/** serviceId: service-feign demo: path: /demo/** serviceId: service-demo order: path: /order/** serviceId: service-order3、启动类开启zuul代理功能
package com.lyq;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@SpringBootApplication@EnableZuulProxy@EnableEurekaClientpublic class ServiceZuulApplication {public static void main(String[] args) {SpringApplication.run(ServiceZuulApplication.class, args);}}四、测试
1、依次启动eureka-server、demo、order、service-zuul
2、访问http://localhost:8761/ 可以看到用户服务、订单服务、zuul服务已经注册进去了
3、访问http://localhost:8769/demo/accTotal
自动跳转到http://localhost:8081/accTotal/list 打开了账户界面
4、访问http://localhost:8769/order打开订单服务列表界面
5、操作提交,再查看demo的用户余额,发现余额增加了,说明接口调用成功,成功实现两个服务之间的数据调用
保存成功返回值:
用户余额界面:
本次改造先到这里
- 框架整合搭建实战练习记录2
- 框架整合搭建实战练习记录1
- SpringMVC_Spring_Mybits框架整合记录
- ssm框架整合记录
- redis搭建实战记录
- SSH框架搭建整合
- SSM框架搭建整合
- 练习:QQ 框架搭建
- Spark实战(2)----hadoop集群框架搭建
- SSI框架整合实战成果
- 《SSM框架实战》 整合 Freemarker
- Struts 2核心技术与Java EE框架整合开发实战
- ssm 框架整合学习记录
- SSH整合之框架搭建
- SSH整合之框架搭建 .
- 01、SSM框架整合搭建
- SSM整合(1.框架搭建)
- ACE框架整合记录2Tab插件-【完结】
- 四大组件之ContentProvider
- 简单网页计算器代码
- N
- 数据语言和约束
- Oracle数据库安装过程中先决检查条件失败
- 框架整合搭建实战练习记录2
- 基于OKhttp循环队列发送心跳包,上传,多条上传 管理类
- Spring mvc批量图片上传
- autopilot 源代码
- Pointfree 编程风格
- Servlet.service() for servlet [spring] in context with path [/]
- IE6下图片的浏览剪裁与上传
- 使用ZooKeeper实现队列
- 斯坦福大学公开课IOS 7学习笔记 (1)