数据库

  

  数据库名称为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. 如何学习python,个人的一些简单见解

    什么是重要的东西 思考学习是一个什么样的过程 我们每个人都学习过数学,肯定都知道数学的学习过程是什么,我们刚开始学习数学的时候会学习一些简单的公式和概念,比如加减乘除,随着学习的深入,我们发现在大学之 ...

  2. IPv6笔记-地址结构与分类

    1.地址基础 IPv6地址由被划分为8个16位块的128位组成. 然后将每个块转换为由冒号符号分隔的4位十六进制数字. 2001::3238:00E1:0063:0000:0000:FEFB 每一块多 ...

  3. egret常用功能

    egret常用功能<pre>//////////////////////////////////////////////////////////////////////////////// ...

  4. 还看不懂同事的代码?超强的 Stream 流操作姿势还不学习一下

    Java 8 新特性系列文章索引. Jdk14都要出了,还不能使用 Optional优雅的处理空指针? Jdk14 都要出了,Jdk8 的时间处理姿势还不了解一下? 还看不懂同事的代码?Lambda ...

  5. 《计算机网络 自顶向下方法》 第3章 运输层 Part1

    由于个人精力和智商有限,又喜欢想太多.钻牛角尖,导致学习系统性知识很痛苦,尝试改变学习方式,慢慢摸索 现在看到 rdt2.0,又有点看不下去 现在的想法: 要有个目标,且有截止时间(作业模式.考试模式 ...

  6. 这份最新Python面试精选问题你会几道?

    相信很多小伙伴学python以后都想进大厂,但是进大厂前你得了解些大厂面试题,可以在面试前复习下,以下是精选的5道python面试题: 第一. Python 的特点和优点是什么? Python 可以作 ...

  7. nyoj 24-素数距离问题 (素数算法)

    24-素数距离问题 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:71 题目描述: 现在给出你一些数,要求你写出一个程序,输出这 ...

  8. 领扣(LeetCode)二维区域和检索 个人题解

    给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...

  9. windows下大数据开发环境搭建(1)——Hadoop环境搭建

    所需环境 jdk 8 Hadoop下载 http://hadoop.apache.org/releases.html 配置环境变量 HADOOP_HOME: C:\hadoop-2.7.7 Path: ...

  10. python3 之 判断闰年小实例

    一.方法1: while True: try: year = int(input('请输入一个年份:')) if (year % 4) == 0 and (year % 100) != 0 or (y ...