SpringCloud 框架搭建

SpringCloud 框架搭建

创建工作空间

创建父工程

1、创建简单Maven项目,如下

2、删掉src目录

3、修改字符编码,如下

4、开启注解处理,如下

5、选择编译版本,如下

编辑Pom.xml

编辑 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wyx</groupId>
    <artifactId>Spring-Cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEmcoding>UTF-8</project.build.sourceEmcoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
		<!--对应的版本-->
        <spring.boot.dependencies.version>2.4.3</spring.boot.dependencies.version>
        <spring.cloud.dependencies.version>2020.0.2</spring.cloud.dependencies.version>
        <spring.cloud.alibaba.dependencies.version>2.2.1.RELEASE</spring.cloud.alibaba.dependencies.version>
        <mysql.version>8.0.23</mysql.version>
        <druid.version>1.2.5</druid.version>
        <druid.spring.boot.version>1.2.5</druid.spring.boot.version>
        <log4j.version>1.2.17</log4j.version>
        <junit.version>4.13</junit.version>
        <lombok.version>1.18.20</lombok.version>
        <mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version>

    </properties>

    <dependencyManagement>
        <dependencies>
            <!--SpringBoot 依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--Spring Cloud 依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--Spring Cloud Alibaba 依赖-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--Mysql 连接驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <!--druid 依赖-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            
            <!--Spring Boot 启动器-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.spring.boot.version}</version>
            </dependency>

            <!--Lombok 依赖-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>

            <!--log4j 依赖-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            
            <!--junit 单元测试 依赖-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>

            <!--Mybatis-spring-boot 启动器-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
            
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.4</version>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

微服务工程搭建

创建实体类API

1、在父工程下创建一个空maven项目

2、pom.xml导入自己需要的依赖

<!--导入当前模块的依赖,会自动继承父类的依赖,不需要添加版本号-->
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

3、创建实体类

package com.wyx.cloud.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)  //开启链式编程
public class User {
//对应数据库的实体类
    private Long id;
    private String name;
    private Integer age;
    private String sex;

    /*
    *  @Accessors(chain = true)
    *   开启链式编程
    *  如 user.serId(3L).setName("花海");
    * */ 
}
package com.wyx.cloud.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
// 结合前端开发,请求的数据我们只需要放回一个状态码,和信息,外加数据
    private Integer code;
    private String message;
    private T data;

    // 添加一个构造器,返回错误时返回 null
    public CommonResult(Integer code,String message){
        this(code,message,null);
    }
}

4、删除测试目录

服务提供者搭建

1、创建一个空的maven项目

2、编写pom.xml,导入依赖

    <dependencies>
        <!--需要使用实体类,导入我们需要的 Api-->
        <dependency>
            <groupId>com.wyx</groupId>
            <artifactId>SpringCloud-API</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--spring-boot-web 模块 常用的3个-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--mybatis整合spring-boot的启动-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

        <!--mysql连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc驱动-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--日志依赖-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <!--druid-spring-boot 启动器-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        
        <!--jetty 启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        
        <!--热部署插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!--测试插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--lombok 依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

3、编写配置文件 application.yaml(在资源目录下)

server:
  port: 8003  # 启动端口


mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml  # 配置别名包扫描路径
  type-aliases-package: com.wyx.cloud.pojo  # 配置实体类存放路径
  configuration:
    map-underscore-to-camel-case: true    # 数据库的下划线转驼峰命名

spring:
  application:
    name: springcloud-provider-user   # 服务名字
  datasource:  # 配置数据源和数据库连接配置
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud?useSSL=true&useUnicode=true&charterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: 970699

4、编写主启动类

package com.wyx.cloud;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Provider {
    public static void main(String[] args) {
        SpringApplication.run(Provider.class);
    }
}

5、编写 mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--核心配置文件-->
<configuration>
    
    <settings>
        <!--开启二级缓存-->
        <setting name="cacheEnable" value="true"/>
    </settings>
    
</configuration>

6、编写mapper

package com.wyx.cloud.mapper;

import com.wyx.cloud.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserMapper {

    int add(User user);

    User getUserById(@Param("id") Long id);

}

7、编写 UserMapper.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.wyx.cloud.mapper.UserMapper">
    <insert id="add" parameterType="user">
        insert into
        user (id,name,age,sex) value (#{id},#{name},#{age},#{sex})
    </insert>

    <select id="getUserById" resultType="user">
        select *
        from user
        where id = #{id}
    </select>

</mapper>

8、编写Service

package com.wyx.cloud.service;

import com.wyx.cloud.pojo.User;
import org.apache.ibatis.annotations.Param;

//service 接口
public interface UserService {

    int add(User user);
    User getUserById(@Param("id") Long id);
}

package com.wyx.cloud.service.impl;

import com.wyx.cloud.mapper.UserMapper;
import com.wyx.cloud.pojo.User;
import com.wyx.cloud.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

//service层实现类
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public int add(User user) {

       return userMapper.add(user);

    }

    @Override
    public User getUserById(Long id) {
        return userMapper.getUserById(id);
    }

}

9、编写Controller

package com.wyx.cloud.controller;

import com.wyx.cloud.pojo.CommonResult;
import com.wyx.cloud.pojo.User;
import com.wyx.cloud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/addUser")
    public CommonResult add(@RequestParam("id") Long id,
                            @RequestParam("name") String name,
                            @RequestParam("age") Integer age,
                            @RequestParam("sex") String sex){

        int result = userService.add(new User(id,name,age,sex));
        if(result > 0){
            return new CommonResult(200,"插入成功", result);
        }else {
            return new CommonResult(444,"插入失败", null);
        }
    }
    @GetMapping("/User/{id}")
    public CommonResult getUser(@PathVariable("id") Long id){

        User user = userService.getUserById(id);
        if(user != null){
            return new CommonResult(200,"查询成功", user);
        }else {
            return new CommonResult(444,"查询失败", null);
        }
    }
}

10、运行测试

热部署功能

1、在子项目中添加热部署依赖

<!--热部署插件-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

2、在父项目中添加以下打包构建

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.4.4</version>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

3、开启IDEA自动编译

4、开启热注册

按下快捷键Ctlr+Shift+Alt+/,选择Registry

消费者模块搭建

1、父模块创建空的maven项目

2、编辑pom.xml 添加依赖

<dependencies>
    <!--需要使用实体类,导入我们需要的 Api-->
    <dependency>
        <groupId>com.wyx</groupId>
        <artifactId>SpringCloud-API</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

</dependencies>

3、编写application.yaml

server:
  port: 80

spring:
  application:
    name: consumer-order

4、编写主启动类

package com.wyx.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Consumer {

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

5、注入RestTemplate 模板

package com.wyx.cloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationConfig {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

6、编写消费者Controller

package com.wyx.cloud.controller;

import com.wyx.cloud.pojo.CommonResult;
import com.wyx.cloud.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class OrderController {

     public  final static String PROVIDER_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/createUser")
    public CommonResult createUser(@RequestParam("id") Long id,
                                   @RequestParam("name") String name,
                                   @RequestParam("age") Integer age,
                                   @RequestParam("sex") String sex){

        return restTemplate.postForObject(PROVIDER_URL+"addUser"+"?id="+id+"&name="+name+
                "&age="+age+"&sex="+sex
                ,null,CommonResult.class);
    }

    @GetMapping("/consumer/getUser/{id}")
    public CommonResult getUser(@PathVariable("id") Long id){
        return restTemplate.getForObject(PROVIDER_URL+"/User/"+id,CommonResult.class);
    }
}

7、启动测试

posted @ 2021-05-15 09:41  橘子有点甜  阅读(603)  评论(0编辑  收藏  举报