文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

教你创建springcloud微服务的基础子服务的超详细过程

2024-04-02 19:55

关注

本文主要是创建两个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

修改为如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>openfeign-stu</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wq</groupId>
    <artifactId>springcloud-stu</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-stu</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
</project>

2. openfeign-stu pom.xml中修改为如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.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">
    <parent>
        <artifactId>springcloud-stu</artifactId>
        <groupId>com.wq</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>openfeign-stu</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>feign-provider</module>
        <module>feign-consumer</module>
        <module>feign-api</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
    </dependencies>
</project>

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插件

修改为如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.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">
    <parent>
        <artifactId>openfeign-stu</artifactId>
        <groupId>com.wq</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>feign-provider</artifactId>
    
    <dependencies>
        <dependency>
            <groupId>com.wq</groupId>
            <artifactId>feign-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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;
    
    @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 {
    
    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 {
    
    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

<?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="com.wq.feign.provider.dao.ProductMapper">
    <resultMap id="BaseResultMap" type="com.wq.feign.provider.domain.entity.Product">
        <result column="id" jdbcType="BIGINT" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="stock" jdbcType="INTEGER" property="stock" />
    </resultMap>
    <!--查询-->
    <select id="selectById" resultType="com.wq.feign.provider.domain.entity.Product">
        select * from product where id = #{id}
    </select>
    <!--删除-->
    <delete id="deleteById" parameterType="java.lang.Long">
        delete from product where id = #{id}
    </delete>
    <!--修改-->
    <update id="updateById" parameterType="com.wq.feign.provider.domain.entity.Product">
        update product set name = #{name} where id = #{id}
    </update>
    <!--新增-->
    <insert id="insertOne" parameterType="com.wq.feign.provider.domain.entity.Product">
       insert into product(name, stock) values (#{name}, #{stock})
    </insert>
</mapper>

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
    @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,还有提供者地址在消费者里面写死了等等,这些问题,我们接下来会一一解决。

到此这篇关于教你创建springcloud微服务的基础子服务的文章就介绍到这了,更多相关springcloud微服务内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯