• SpringCloud与微服务Ⅳ --- Rest微服务构建案例工程模块


    一.父工程搭建

    父工程pom:

    <?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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.wang.springcloud</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <junit.version>4.12</junit.version>
            <log4j.version>1.2.17</log4j.version>
            <lombok.version>1.16.18</lombok.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>1.5.9.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.0.4</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>1.0.31</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>1.3.0</version>
                </dependency>
                <dependency>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-core</artifactId>
                    <version>1.2.3</version>
                </dependency>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <finalName>microservice</finalName>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <delimiters>
                            <delimit>$</delimit>
                        </delimiters>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>

    二.Entity子工程搭建

    将实体类抽离出来独立一个工程,方便多个工程调用,使用时只需要导入该工程的maven依赖就可以引用了。

    2.1 pom依赖

    <?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>microservice</artifactId>
            <groupId>com.wang.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <artifactId>microservice-api</artifactId>
        <packaging>jar</packaging>
    
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
        </dependencies>
    </project>

    2.2 实体类(这里用了lombok)

    package com.wang.springcloud.entities;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    
    import java.io.Serializable;
    @AllArgsConstructor //全参构造
    @NoArgsConstructor //无参构造
    @Data //生产getter和setter方法
    @Accessors(chain = true) //链式风格访问
    public class Dept implements Serializable { //Entity
        private Long deptno;
        private String dname;
        private String db_source;//来自哪个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库
    
        public Dept(String dname) {
            this.dname = dname;
        }
    
    }

    三.服务提供者

    3.1 pom依赖

    <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">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.wang.springcloud</groupId>
            <artifactId>microservice</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>microservicecloud-provider-dept-8001</artifactId>
    
        <dependencies>
            <!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
            <dependency>
                <groupId>com.wang.springcloud</groupId>
                <artifactId>microservice-api</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!-- actuator监控信息完善 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!-- 将微服务provider侧注册进eureka -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</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-test</artifactId>
            </dependency>
            <!-- 修改后立即生效,热部署 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
        </dependencies>
    
    </project>

    3.2 系统配置文件

    server:
      port: 8001
    mybatis:
      config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路径
      type-aliases-package: com.wang.springcloud.entities #所有entity别名类所在包
      mapper-locations: classpath:mybatis/mapper/**/*.xml #mapper映射文件
    
    spring:
      application:
        name: microservice-dept
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource  #数据源类型
        driver-class-name: org.gjt.mm.mysql.Driver  #数据库驱动
        url: jdbc:mysql://localhost:3306/cloudDB01  #数据库url
        username: root
        password: 123456
        dbcp2:
          min-idle: 5 #数据库连接池的最小维持连接数
          initial-size: 5 #初始化连接数
          max-total: 5  #最大连接数
          max-wait-millis: 200  #等待连接获取的最大超时时间

    3.3 mybatis的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="cacheEnabled" value="true" /><!-- 二级缓存开启 -->
        </settings>
    </configuration>

    3.4 mapper接口

    @Mapper
    public interface DeptMapper {
        public boolean addDept(Dept dept);
    
        public Dept findById(Long id);
    
        public List<Dept> findAll();
    }

    3.5 mapper的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.wang.springcloud.mapper.DeptMapper">
    
        <select id="findById" resultType="Dept" parameterType="Long">
            select deptno,dname,db_source from dept where deptno=#{deptno};
        </select>
        <select id="findAll" resultType="Dept">
            select deptno,dname,db_source from dept;
        </select>
        <insert id="addDept" parameterType="Dept">
            INSERT INTO dept(dname,db_source) VALUES(#{dname},DATABASE());
        </insert>
    </mapper>

    3.6 启动类

    package com.wang.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DeptProvider8001 {
        public static void main(String[] args) {
            SpringApplication.run(DeptProvider8001.class,args);
        }
    }

    四.服务消费者

    4.1 pom依赖

    <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">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.wang.springcloud</groupId>
            <artifactId>microservice</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>microservicecloud-consumer-dept-80</artifactId>
        <description>部门微服务消费者</description>
    
        <dependencies>
            <dependency><!-- 自己定义的api -->
                <groupId>com.wang.springcloud</groupId>
                <artifactId>microservice-api</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!-- Ribbon相关 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- 修改后立即生效,热部署 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
        </dependencies>
    </project>

    4.2 系统配置文件

    server:
      port: 80

    4.3 配置类注入RestTemplate

    package com.wang.springcloud.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    
    @Configuration
    public class MyApplicationConfig {
        @Bean
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    }

    4.4 Controller消费服务

    RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的访问restful服务的模板类,是Spring提供的用于访问Rest服务的客户端模板工具类。

    postForObject(url,requestMap,ResponseBean.class)参数分别代表Rest请求地址,请求参数,HTTP响应被转换成的对象类型

    package com.wang.springcloud.controller;
    
    import com.wang.springcloud.entities.Dept;
    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.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.List;
    
    @RestController
    public class DeptController {
        private static final String REST_URL_PREFIX = "http://localhost:8001";
        @Autowired
        private RestTemplate restTemplate;
    
        @RequestMapping(value = "/consumer/dept/add",method = RequestMethod.POST)
        public boolean add(Dept dept){
            return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
        }
    
        @RequestMapping(value = "/consumer/dept/get/{id}",method = RequestMethod.GET)
        public Dept get(@PathVariable("id") Long id){
            return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
        }
    
        @SuppressWarnings("unchecked")
        @RequestMapping(value = "/consumer/dept/list",method = RequestMethod.GET)
        public List<Dept> list(){
            return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
        }
    }
  • 相关阅读:
    MySQL联结查询
    MySQL的一些优化方法
    MySQL 基本操作
    一个关于python定制类的例子
    用python类方法处理参数
    python global的用法
    sqli-libs(29(jspstudy)-31关)
    sqli-libs(23-28a关)
    sqli-libs(11-22关)
    sqli-libs(5-10关)
  • 原文地址:https://www.cnblogs.com/wangxiayun/p/10366700.html
Copyright © 2020-2023  润新知