数据库

  

  数据库名称为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. Prometheus客户端开发:腾讯云CLB

    一:简介 随着prometheus的使用人群逐渐扩大,官方定义的client exporter虽然能满足我们的大部分需求,但是很多监控还是需要我们自定义开发,以下内容就是基于腾讯云SDK,对腾讯云CL ...

  2. JS 暴虐算法查找

      @dd|ad|fds|d@dd|ad|fds|d@dd|ad|fds|d@   var e = [];     window.onload = function () {         var ...

  3. Spring注解之@RestControllerAdvice

    前言 前段时间部门搭建新系统,需要出异常后统一接口的返回格式,于是用到了Spring的注解@RestControllerAdvice.现在把此注解的用法总结一下. 用法 首先定义返回对象Respons ...

  4. Linux命令实战(四)

    1.Linux上的文件管理类命令都有哪些,其常用的使用方法及相关示例演示. 文件或目录的新建 touch :将每个文件的访问时间和修改时间修改为当前时间.若文件不存在将会创建为空文件,除非使用-c或- ...

  5. Python 基础 内置函数 迭代器与生成器

    今天就来介绍一下内置函数和迭代器 .生成器相关的知识 一.内置函数:就是Python为我们提供的直接可以使用的函数. 简单介绍几个自己认为比较重要的 1.#1.eval函数:(可以把文件中每行中的数据 ...

  6. aop的简单使用(代码和配置记录)

    Spring aop 简单示例 简单的记录一下spring aop的一个示例 基于两种配置方式: 基于xml配置 基于注解配置 这个例子是模拟对数据库的更改操作添加事物 其实并没有添加,只是简单的输出 ...

  7. 深入理解计算机系统 第三章 程序的机器级表示 Part2 第二遍

    第一遍对应笔记链接 https://www.cnblogs.com/stone94/p/9943779.html 本章汇编代码中常出现的几个指令及其含义 1.push 操作数的个数:1 将操作数(一般 ...

  8. W与V模型的联系与区别

      很多小白一定要注意:        看准那个是开发的工作哪个是测试的工作,不要弄混了!!!   软件测试的V模型 以“编码”为黄金分割线,将整个过程分为开发和测试,并且开发和测试之间是串行的关系 ...

  9. ThinkPHP6 核心分析:系统服务

    什么是系统服务?系统服务是对于程序要用到的类在使用前先进行类的标识的绑定,以便容器能够对其进行解析(通过服务类的 register 方法),还有就是初始化一些参数.注册路由等(不限于这些操作,主要是看 ...

  10. byteCTF 2019

    本文作者:z3r0yu  由“合天智汇”公众号首发,未经允许,禁止转载! 0x00 前言 周末的比赛质量还是挺高的,特别是boring_code,有点烧脑但是做的就很开心. 0x01 boring_c ...