Rest微服务案例
Posted wnwn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rest微服务案例相关的知识,希望对你有一定的参考价值。
数据库
数据库名称为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.1</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.1-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.38</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=123 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.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
application.properties文件
server.port=8081
配置类
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微服务案例的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud学习系列-Rest微服务构建-整体父工程Project
SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端Eureka 服务信息Eureka 发现管理Eureka 安全配置Eureka-HA(高可用) 机制Eur(代码片段