我的一次微服务实战-SpringCloud学习

2022-05-05 16:21:29 浏览数 (1)

项目结构

这是我第一个SpringCloud微服务的学习项目。主要采用公司框架(基于SpringCloud定制)

1.项目结构

公共接口:给服务提供公共接口

demo-comsumer-service:服务B,会远程调用服务A

demo-service:服务A

demo-api
工程目录
主要类文件

StudentApi

StudentApi.java是一个接口。定义一些方法

Student.java为一个公用实体类

Sudent

pom.xml
代码语言:javascript复制
<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>
        <groupId>xxx.xxx</groupId>
        <artifactId>xxx-demo</artifactId>
        <version>2.0.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>demo-api</artifactId>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>xxx.xxx</groupId>
            <artifactId>xxx-starter</artifactId>
        </dependency>
    </dependencies>

</project>

xml文件配置如上

demo-service
工程目录

之前的demo-api需要install。

pom.xml
代码语言:javascript复制
<!--Springboot相关-->
<parent>
        <groupId>xxx.xxx</groupId>
        <artifactId>xxx-demo</artifactId>
        <version>2.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>demo-service</artifactId>
    <packaging>jar</packaging>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
          
          
          <!--省略-->
          
           <dependencies>
       <!--demo-api接口导入,会使用到Student.java-->
        <dependency>
            <groupId>xxx.xxx</groupId>
            <artifactId>demo-api</artifactId>
            <version>2.0.0-SNAPSHOT</version>
        </dependency>
            
         <!--SpringBoot启动相关-->
        <dependency>
            <groupId>xxx.xxx</groupId>
            <artifactId>mushroom-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>xxx.xxx</groupId>
            <artifactId>mushroom-test-starter</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>
配置文件

application.properties内容如下:

demo-service配置文件

类文件
代码语言:javascript复制
StudentDemo.java
@MyBatisDao
public interface StudentDao extends BaseDao<Student>,InsertHisMapper<Student>{
}
StudentService.java
@Service
public class StudentService extends CrudService<StudentDao,Student>{
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Student student){
        super.create(student);
    }
}
StudentController.java
@RestController
@RequestMapping(value = "/${xxx.api.prefix}/student")
@Api(tags = "student-service", description = "微服务开发Demo学习")
@PermissionGroup(name = "Demo服务学习", code = "demo:student")
public class StudentController extends BaseController implements StudentApi{

    private final StudentService studentService;

    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }


    @Override
    @RequiresPermissions("demo:student:view")
    @GetMapping("get")
    @ApiOperation("获取student对象")
    public Student get(String id) {
        return studentService.get(id);
    }
}
Application.java
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
}

}

启动Application.java

端口:9011

登录注册中心:

注册中心1

发现服务已经注册。

demo-comsumer-service
工程目录
pom.xml

文件同demo-service pom.xml文件

类文件

StudentFeignClient.java

feignclient

ConsumerStudentControlle.java

远程调用demo-service里面的方法:

代码语言:javascript复制
@RestController
@RequestMapping(value = "/${mushroom.api.prefix}/consumerStudent")
@Api(tags = "consumer-student", description = "远程调用demo-service")
@PermissionGroup(name = "Demo服务学习", code = "demo:student")
public class ConsumerStudentController {

    @Autowired
    private StudentFeignClient studentFeignClient;


    @RequiresPermissions("demo:student:view")
    @GetMapping("/consumerStudent/get")
    @ApiOperation("获取student对象")
    public Student get(String id){
        //这里调用了服务demo-service里面的get方法,底层通过http://ip:port/路径   实现
        return this.studentFeignClient.get(id);
    }

}
ConsumerApplication.java
@EnableFeignClients
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args){
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

配置文件

demo-service的配置文件。

启动demo-consumer-service,注册中心会注册该服务。

注册中心2

测试

打开swagger

测试

测试成功。

打开数据库:

数据库

和数据库里面的数据吻合。

另外附上个人博客github

Blog:https://lvshen9.gitee.io/ GitHub:https://github.com/lvshen9?tab=repositories

0 人点赞