springcloud01- SpringCloud快速搭建

Posted 林森001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud01- SpringCloud快速搭建相关的知识,希望对你有一定的参考价值。

SpringCloud快速搭建

1.导入父依赖(maven工程)

<!--打包方式-->
<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>
    <lombok.version>1.16.10</lombok.version>
</properties>

<dependencyManagement>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--日志、测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>
</dependencyManagement>

1.搭建子工程(API 专门是实体类的工程)

? 1)导入子工程需要的依赖

<dependencies>
    <!--当前Module自己需要的依赖,如果父依赖中已经配置了版本,这里就不用写了-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

? 2)创建数据,创建表

? 3)创建实体类(所有的实体类实现序列化)

package com.mjh.springcloud.pojo;

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

import java.io.Serializable;

@Data
@NoArgsConstructor
@Accessors(chain = true)//链式写法
public class Dept  implements Serializable {
    private Long deptNo;//bigint  用Long
    private String dName;
    private String db_source;//这个数据是存在于那个数据库的字段  微服务  一个服务对应一个数据库

    public Dept(String dName) {
        this.dName = dName;
    }

    /**
     * 链式写法
     * Dept dept=new Dept();
     * dept.setName("11").setId(4).setAge(7)
     */
}

2.搭建服务提供者工程

1)导入子工程需要的依赖

<dependencies>
    <!--我们需要拿到实体类,所以要配置api module-->
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>springcloud-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </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>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
     <!--test-->
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        </dependency>
</dependencies>

? 2)编写配置文件

server:
  port: 8001
  #mybatis文件
mybatis:
  type-aliases-package: com.mjh.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

#Spring配置
spring:
  application:
    name: springcloud-privider-dept

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456

? 3)编写mybatis配置文件(可以省略)

<?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>
        <!-- 是否开启缓存 , 配置全局性 cache 的 ( 开 / 关) default:true  -->
        <setting name="cacheEnabled" value="false"/>

    </settings>
</configuration>

? 4)编写Dao层接口

package com.mjh.springcloug.dao;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface Dept {
    public boolean addDept(Dept dept);
    public Dept queryById(Long id);
    public List<Dept> queryAll();
}

? 5)编写对应的mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mjh.springcloug.dao.DeptDao">
  <insert id="addDept" parameterType="Dept">
      insert into dept values (#{dName},DATABASE());
  </insert>
    <select id="queryById" resultType="Dept" parameterType="Long">
        select * from dept where deptNo=#{deptNo};
    </select>
    <select id="queryAll" resultType="Dept">
        select * from dept;
    </select>
</mapper>

? 6)service层

package com.mjh.springcloug.service;

import com.mjh.springcloud.pojo.Dept;
import com.mjh.springcloug.dao.DeptDao;

import java.util.List;

public interface DeptService {
    public boolean addDept(Dept dept);
    public Dept queryById(Long id);
    public List<Dept> queryAll();
}
package com.mjh.springcloug.service;

import com.mjh.springcloud.pojo.Dept;
import com.mjh.springcloug.dao.DeptDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl  implements DeptService {
    @Autowired
   private DeptDao deptDao;

    @Override
    public boolean addDept(Dept dept) {
        return deptDao.addDept(dept);
    }

    @Override
    public Dept queryById(Long id) {
        return deptDao.queryById(id);
    }

    @Override
    public List<Dept> queryAll() {
        return deptDao.queryAll();
    }
}

? 7)controller层

package com.mjh.springcloug.controller;

import com.mjh.springcloud.pojo.Dept;
import com.mjh.springcloug.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 提供restful风格,让外界调用我们的方法
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }
    @GetMapping("/dept/queryById/{id}")
    public Dept queryById(@PathVariable("id") Long id){
        return deptService.queryById(id);
    }
    public List<Dept> queryAll(){
        return deptService.queryAll();
    }

}

? 8)主启动类

package com.mjh.springcloug;

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

//启动类
@SpringBootApplication
public class DeptPrivider_8001 {
    public static void main(String[] args) {
        SpringApplication.run( DeptPrivider_8001.class,args);
    }
}

3.搭建服务消费者(消费者不连数据库)

1)导入依赖

<!--实体类+web-->
<dependencies>
    <!--我们需要拿到实体类,所以要配置api module-->
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>springcloud-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <!--热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

? 2)编写pom.xml

server:
  port: 80

? 3)消费者不应该有service层,所以我们要想办法把提供者的URL去到,就要在spring的Bean里注入RestTemplate,,,里边有很多请求

package com.mjh.springcloud.config;

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

@Configuration    //@Configuration 相当于spring里面的 applicationContext.xml
public class ConfigBean {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

? 4)controller层

package com.mjh.springcloud.controller;

import com.mjh.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.xml.ws.RequestWrapper;
import java.util.List;

@Controller
public class DeptConsumerController {
    //理解:消费者不应该有service层
    //RestTemplate......供我们直接调用就可以了,注册到spring中
    //(url,实体:map,Class<T> responseType)
    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PERFIx="http://localhost:8001";

   @RequestMapping("/consumer/dept/add")
   public boolean add(Dept dept){
       return restTemplate.postForObject(REST_URL_PERFIx+"/dept/add",dept,Boolean.class);
   }

    @RequestMapping("/consumer/dept/queryById/{id}")
    public Dept queryById(@PathVariable("id") Long id){
       //服务端给你什么方法你就调用什么方法
        return restTemplate.getForObject(REST_URL_PERFIx+"/dept/queryById/"+id,Dept.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> list(){
       return restTemplate.getForObject(REST_URL_PERFIx+"/dept/queryAll",List.class);
    }
}

? 5)主启动类

package com.mjh.springcloud;


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

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

以上是关于springcloud01- SpringCloud快速搭建的主要内容,如果未能解决你的问题,请参考以下文章

springcloud—— 网关服务Gateway

# SpringCloud 服务治理实现(Consul)

微服务SpringCloud之服务注册与发现

Spring Cloud 概述

##走过路过不容错过之##JavaEE 框架篇四 SpringCloud

用没用过SpringCloud,就看你会不会这35个面试题!