react-smooth-dnd 拖拽实例(react-smooth-dnd 列表排序)
291
2022-08-16
教你创建springcloud微服务的基础子服务的超详细过程
目录一、创建父项目1. 选择Spring Initializr2. Type选择为Maven POM,java Version选择为83. 勾选一些基本的依赖,如lombok和spring-boot-starter-web4. 创建好了的父项目结构如下:二、创建二级项目1. 选择新建Module2. 选择Maven3. 填写Name4. 修改openfeign pom.xml文件,并删除掉该项目下的src文件5. 创建完成的项目结构如下三、创建子项目feign-provider、feign-consumer、feign-api1. 在feign-provider项目上新建模块2. 选择Parent为openfeign-stu3. 同上步骤创建feign-consumer、feign-api,创建完成后的整体项目结构如下:四、完善feign-api、feign-provider、feign-consumer项目1. springcloud-stu pom.xml中去掉spring-boot-maven-plugin2. openfeign-stu pom.xml中修改为如下:3. feign-api 完善4. feign-provider 完善4.1 pom.xml添加feign-api依赖,及增加spring-boot-maven-plugin插件4.2 ProductController.java4.3 ProductMapper.xml4.4 Product.java4.5 ProductServiceImpl.java4.6 ProductService.java4.7 ProviderApplication.java4.8 ProductMapper.xml4.9 application.yml4.10 sql脚本5. feign-consumer 完善5.1 pom.xml添加feign-api依赖,及增加spring-boot-maven-plugin插件5.2 RestConfig.java5.3 ProductConsumerController.java5.4 ConsumerApplication.java5.5 application.yml五、测试项目六 总结
本文主要是创建两个springboot服务,以在该系列后面的教程中增添springcloud相关组件
一、创建父项目
1. 选择Spring Initializr
2. Type选择为Maven POM,Java Version选择为8
3. 勾选一些基本的依赖,如lombok和spring-boot-starter-web
4. 创建好了的父项目结构如下:
二、创建二级项目
1. 选择新建Module
2. 选择Maven
3. 填写Name
4. 修改openfeign pom.xml文件,并删除掉该项目下的src文件
5. 创建完成的项目结构如下
三、创建子项目feign-provider、feign-consumer、feign-api
1. 在feign-provider项目上新建模块
2. 选择Parent为openfeign-stu
3. 同上步骤创建feign-consumer、feign-api,创建完成后的整体项目结构如下:
四、完善feign-api、feign-provider、feign-consumer项目
1. springcloud-stu pom.xml中去掉spring-boot-maven-plugin
修改为如下:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2. openfeign-stu pom.xml中修改为如下:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3. feign-api 完善
feign-api的项目清单如下:
Result的内容如下:
package com.wq.feign.api.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private int code;
private String msg;
private Object data;
}
pom.xml内容就是创建时的内容,不做改动
4. feign-provider 完善
完善后的项目结构如下:
4.1 pom.xml添加feign-api依赖,及增加spring-boot-maven-plugin插件
修改为如下:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.2 ProductController.java
package com.wq.feign.provider.controller;
import com.wq.feign.api.domain.Result;
import com.wq.feign.provider.domain.entity.Product;
import com.wq.feign.provider.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@Slf4j
@RequestMapping("/")
public class ProductController {
@Resource
private ProductService productService;
/**
* 查询
* @param id
* @return
*/
@GetMapping("product/provider/get/{id}")
public Result selectById(@PathVariable("id") Long id){
return new Result(200, "查询成功", productService.selectById(id));
}
* 删除
@GetMapping("product/provider/delete/{id}")
public Result deleteById(@PathVariable("id") Long id){
return new Result(200, "删除成功", productService.deleteById(id));
* 修改
* @param product
@PostMapping("product/provider/update")
public Result updateById(@RequestBody Product product){
return new Result(200, "修改成功", productService.updateById(product.getId(), product.getName()));
* 新增
@PutMapping( "product/provider/add")
public Result insertById(@RequestBody Product product){
return new Result(200, "修改成功", productService.insertOne(product));
}
4.3 ProductMapper.xml
package com.wq.feign.provider.dao;
import com.wq.feign.provider.domain.entity.Product;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductMapper {
/**
* 查询
* @param id
* @return
*/
public Product selectById(@Param("id") Long id);
* 删除
public int deleteById(@Param("id") Long id);
* 修改
* @param name
public int updateById(@Param("id") Long id, @Param("name") String name);
* 新增
* @param product
public int insertOne(Product product);
}
4.4 Product.java
package com.wq.feign.provider.domain.entity;
import lombok.Data;
@Data
public class Product {
private Long id;
private String name;
private int stock;
}
4.5 ProductServiceImpl.java
package com.wq.feign.provider.service.impl;
import com.wq.feign.provider.dao.ProductMapper;
import com.wq.feign.provider.domain.entity.Product;
import com.wq.feign.provider.service.ProductService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class ProductServiceImpl implements ProductService {
@Resource
private ProductMapper productMapper;
public Product selectById(Long id) {
return productMapper.selectById(id);
}
public int deleteById(Long id) {
return productMapper.deleteById(id);
public int updateById(Long id, String name) {
return productMapper.updateById(id, name);
public int insertOne(Product product) {
return productMapper.insertOne(product);
}
4.6 ProductService.java
package com.wq.feign.provider.service;
import com.wq.feign.provider.domain.entity.Product;
/**
* 商品服务类
*/
public interface ProductService {
/**
* 查询
* @param id
* @return
*/
public Product selectById(Long id);
* 删除
public int deleteById(Long id);
* 修改
* @param name
public int updateById(Long id, String name);
* 新增
* @param product
public int insertOne(Product product);
}
4.7 ProviderApplication.java
package com.wq.feign.provider;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.wq.feign.provider.dao")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
4.8 ProductMapper.xml
select * from product where id = #{id}
delete from product where id = #{id}
update product set name = #{name} where id = #{id}
insert into product(name, stock) values (#{name}, #{stock})
4.9 application.yml
server:
port: 8081
spring:
application:
name: feign-provider-8081
datasource:
url: jdbc:mysql://localhost:3306/springcloud-stu?useUnicode=true&characterEncoding=utf8&userSSL=false
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml # Mybatis 映射文件位置
type-aliases-package: com.wq.feign.provider.domain.entity # 表对应的实体类包
4.10 sql脚本
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`http://id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`stock` int(10) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of product
INSERT INTO `product` VALUES (1, '华为 mate10', 10);
INSERT INTO `product` VALUES (2, '华为 mate20', 20);
INSERT INTO `product` VALUES (3, '华为 mate30', 30);
5. feign-consumer 完善
完善后的项目结构如下
5.1 pom.xml添加feign-api依赖,及增加spring-boot-maven-plugin插件
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5.2 RestConfig.java
package com.wq.feign.consumer.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
5.3 ProductConsumerController.java
package com.wq.feign.consumer.controller;
import com.wq.feign.api.domain.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class ProductConsumerController {
@Resource
RestTemplate restTemplate;
public static String url = "http://localhost:8081/";
/**
* 查询
* @param id
* @return
*/
@GetMapping("product/consumer/get/{id}")
public Result selectById(@PathVariable("id") Long id){
return restTemplate.getForObject(url+"product/provider/get/"+id, Result.class);
}
}
5.4 ConsumerApplication.java
package com.wq.feign.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
5.5 application.yml
server:
port: 8082
spring:
application:
name: feign-consumer-8082
五、测试项目
启动ProviderApplication和ConsumerApplication
使用浏览器访问,得到结果如下:
六 总结
我们至此创建了一个多模块的项目,然后有一个商品服务提供者feign-provider-8081 和一个商品服务消费者feign-consumer-8082 。
然后这就是最简单的两个微服务了,实现了功能的解耦。
但是这个简单的微服务存在着很多问题:
比如都用公共的实体类Result,还有提供者地址在消费者里面写死了等等,这些问题,我们接下来会一一解决。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~