数据库

  

  数据库名称为Product;

创建api子工程,项目名为springcloud_api

  

  Product实体类

public class Product implements Serializable {
private Integer pid; private String productName; private Integer quantity; public Product(Integer pid, String productname, Integer quantity) {
this.pid = pid;
this.productName = productname;
this.quantity = quantity;
} public Product() {
super();
} public Integer getPid() {
return pid;
} public void setPid(Integer pid) {
this.pid = pid;
} public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
} public Integer getQuantity() {
return quantity;
} public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}

    公共模块可以达到通用目的,也即需要用到部门实体的话,不用每个工程都定义一份,直接引用本模块即可。

创建生产者,名称为springcloud_provider

  

  导入依赖

<dependencies>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.wn</groupId>
<artifactId>springcloud_api</artifactId>
<version>0.0.-SNAPSHOT</version>
<scope>compile</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>

  application.properties文件

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///invoicingsystem
spring.datasource.username=root
spring.datasource.password= spring.jpa.show-sql=true mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

  dao接口层

@Repository("productDao")
public interface ProductDao { //查询全部
public List<Product> getAll(); //根据id查询列表
public Product getid(@Param("pid") Integer pid); }

  dao.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- mapper为映射的根节点,namespace指定Dao接口的完整类名
mybatis会依据这个接口动态创建一个实现类去实现这个接口,
而这个实现类是一个Mapper对象--> <mapper namespace="com.wn.springcloud_provider.dao.ProductDao">
<resultMap id="MapList" type="com.wn.springcloud_api.entity.Product">
<id property="pid" column="pid"></id>
<result property="productName" column="productName"></result>
<result property="quantity" column="quantity"></result>
</resultMap> <!--绑定商品名称下拉框-->
<select id="getAll" resultType="com.wn.springcloud_api.entity.Product">
SELECT * FROM product
</select> <select id="getid" resultType="com.wn.springcloud_api.entity.Product">
SELECT * FROM product WHERE pid=#{pid}
</select> </mapper>

  service接口层

public interface ProductService {

    //查询全部
public List<Product> getAll(); //根据id查询列表
public Product getid(Integer pid); }

  service接口实现层

@Service("productServices")
public class ProductServiceImpl implements ProductService { @Resource(name = "productDao")
private ProductDao dao; @Override
public List<Product> getAll() {
return dao.getAll();
} @Override
public Product getid(Integer pid) {
return dao.getid(pid);
}
}

  controller层

@Controller
@RequestMapping("/product")
public class ProductController { @Resource(name = "productServices")
private ProductService service; //查询全部
@RequestMapping(value = "/getAll",method = RequestMethod.GET)
@ResponseBody
public List<Product> getAll() {
List<Product> all = service.getAll();
for (Product product:all){
System.out.println(product.getProductName());
}
return all;
} @RequestMapping(value = "/getid/{pid}",method = RequestMethod.GET)
@ResponseBody
public Product getid(@PathVariable("pid") Integer pid){
Product getid = service.getid(pid);
return getid;
} }

  启动类

  

@SpringBootApplication
@MapperScan("com.wn.springcloud_provider.*")
public class SpringcloudProviderApplication { public static void main(String[] args) {
SpringApplication.run(SpringcloudProviderApplication.class, args);
} }

  实现结果 

   

    

创建消费者,名称为springcloud_consumer

  

  导入依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>com.wn</groupId>
<artifactId>springcloud_api</artifactId>
<version>0.0.-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

  application.properties文件

server.port=

  配置类

package com.wn.springcloud_consumer.Bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; @Configuration
public class ConfigBean { @Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
} }

    RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集

  消费者的controller

package com.wn.springcloud_consumer.controller;

import com.wn.springcloud_api.entity.Product;
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;
import org.springframework.web.client.RestTemplate;
import java.util.List; @RestController
public class ProductController { private static final String REST_URL_PREFIX="http://localhost:8080"; @Autowired
private RestTemplate restTemplate; //查询全部
@SuppressWarnings("unckecked")
@RequestMapping("/controller/product/getAll")
public List<Product> getAll(){
System.out.println("--------------------");
return restTemplate.getForObject(REST_URL_PREFIX+"/product/getAll",List.class);
} //根据id查询列表
@RequestMapping("/controller/product/getid/{pid}")
public Product getid(@PathVariable("pid") Integer pid){
return restTemplate.getForObject(REST_URL_PREFIX+"/product/getid/"+pid,Product.class);
} }

  启动类

@SpringBootApplication
public class SpringcloudConsumerApplication { public static void main(String[] args) {
SpringApplication.run(SpringcloudConsumerApplication.class, args);
} }

  实现结果

    

    

Rest微服务案例的更多相关文章

  1. 基于Kubernates微服务案例

    企业业务上云的三种架构 容器的三个视角 从运维角度 数据工程师角度 开发角度微服务化 12 Factor Related Reference: https://kubernetes.io/https: ...

  2. java框架之SpringCloud(2)-Rest微服务案例

    在上一章节已经对微服务与 SpringCloud 做了介绍,为方便后面学习,下面以 Dept 部门模块为例做一个微服务通用 Demo —— Consumer 消费者(Client) 通过 REST 调 ...

  3. Rest微服务案例(二)

    1. 创建父工程 Maven Project 新建父工程microservicecloud,packaging是pom模式,pom.xml内容如下: <!-- SpringBoot父依赖 --& ...

  4. SpringCloud学习(2)——Rest微服务案例

    创建父工程: microservicecloud  创建公共模块api:microservicecloudapi SQL脚本: 此学习路线总共创建3个库, 分别为clouddb01, clouddb0 ...

  5. 从Uber微服务看最佳实践如何炼成?

    导读:Uber成长非常迅速,工程师团队快速扩充,据说Uber有2000名工程师,8000个代码仓库,部署了1000多个微服务.微服务架构是Uber应对技术团队快速增长,功能快速上线很出色的解决方案.本 ...

  6. 看完这篇微服务架构设计思想,90%的Java程序员都收藏了

    本博客强烈推荐: Java电子书高清PDF集合免费下载 https://www.cnblogs.com/yuxiang1/p/12099324.html 微服务 软件架构是一个包含各种组织的系统组织, ...

  7. 【DDD/CQRS/微服务架构案例】在Ubuntu 14.04.4 LTS中运行WeText项目的服务端

    在<WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例>文章中,我介绍了自己用Visual Studio 2015(C# 6.0 with .NET Frame ...

  8. WeText项目:一个基于.NET实现的DDD、CQRS与微服务架构的演示案例

    最近出于工作需要,了解了一下微服务架构(Microservice Architecture,MSA).我经过两周业余时间的努力,凭着自己对微服务架构的理解,从无到有,基于.NET打造了一个演示微服务架 ...

  9. SpringCloud(1)---基于RestTemplate微服务项目案例

    基于RestTemplate微服务项目 在写SpringCloud搭建微服务之前,我想先搭建一个不通过springcloud只通过SpringBoot和Mybatis进行模块之间额通讯.然后在此基础上 ...

随机推荐

  1. 详解Spring Security的HttpBasic登录验证模式

    一.HttpBasic模式的应用场景 HttpBasic登录验证模式是Spring Security实现登录验证最简单的一种方式,也可以说是最简陋的一种方式.它的目的并不是保障登录验证的绝对安全,而是 ...

  2. java基础阶段几个面试题

    1.说出你对面向对象的理解 在我理解,面向对象是向现实世界模型的自然延伸,这是一种“万物皆对象”的编程思想.在现实生活中的任何物体都可以归为一类事物,而每一个个体都是一类事物的实例.面向对象的编程是以 ...

  3. 致和我一样迷茫的Java程序员们

    缘起 从事近7年Java开发之后,在2019年这个寒冷的冬天里,我终于迎来了人生中的第一次裁员. 啊,30岁之后的裁员真让人焦虑. 按照以往惯例,在面试心仪的公司之前,需要先面试一些不那么心仪的公司热 ...

  4. ASP.NET Core 1.0: 指定Static File中的文件作为default page

    指定一个网站的default page是很容易的事情.譬如IIS Management中,可以通过default page来指定,而默认的index.html, index.htm之类,则早已经被设置 ...

  5. 【idea】高德地图可以关爱一下高个汽车

    现状:1.交通事故时不时能看到大卡车,双层巴士在城市里限高区域时的车祸 原因分析:1.司机对路况不熟,驶入新的限高路,造成事故2.司机对车况不熟,临时换的车驾驶,忘记车高的变化3.司机路况车况都熟,道 ...

  6. Python日志模块logging简介

    日志处理是项目的必备功能,配置合理的日志,可以帮助我们了解系统的运行状况.定位位置,辅助数据分析技术,还可以挖掘出一些额外的系统信息. 本文介绍Python内置的日志处理模块logging的常见用法. ...

  7. Anaconda 笔记

    Anaconda笔记 conda 功能 管理版本的切换 安装其他的包 conda 创建python27环境 conda create --name python27 python=2.7 conda ...

  8. 堡垒机的核心武器:WebSSH录像实现

    WebSSH终端录像的实现终于来了 前边写了两篇文章『Asciinema:你的所有操作都将被录制』和『Asciinema文章勘误及Web端使用介绍』深入介绍了终端录制工具Asciinema,我们已经可 ...

  9. opencv 7 直方图与匹配

    图像直方图概述 直方图的计算与绘制 计算直方图:calcHist()函数 找寻最值:minMaxLoc()函数 示例程序:绘制H-S直方图 #include "opencv2/highgui ...

  10. 剑指Offer-23.二叉搜索树的后序遍历序列(C++/Java)

    题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 分析: 二叉树的后序遍历也就是先访问左子树,再访问右 ...