springcloud01- SpringCloud快速搭建

2021-04-13 18:27

阅读:610

标签:不用   序列   response   搭建   localhost   http   repos   sql   风格   

SpringCloud快速搭建

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


pomUTF-81.81.84.121.16.10org.springframework.cloud
            spring-cloud-dependencies
            Greenwich.SR1pomimportorg.springframework.boot
            spring-boot-dependencies
            2.1.4.RELEASEpomimportmysql
            mysql-connector-java
            5.1.47com.alibaba
            druid
            1.1.10org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2junit
            junit
            ${junit.version}org.projectlombok
            lombok
            ${lombok.version}log4j
            log4j
            1.2.17ch.qos.logback
            logback-core
            1.2.3

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

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

org.projectlombok
        lombok
    

? 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)导入子工程需要的依赖

org.example
        springcloud-api
        1.0-SNAPSHOTjunit
        junit
    mysql
        mysql-connector-java
    com.alibaba
        druid
    ch.qos.logback
        logback-core
    log4j
        log4j
    org.mybatis.spring.boot
        mybatis-spring-boot-starter
    org.springframework.boot
        spring-boot-test
        org.springframework.boot
            spring-boot-starter-web
        org.springframework.boot
            spring-boot-starter-jetty
        org.springframework.boot
            spring-boot-devtools
        

? 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配置文件(可以省略)

? 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 queryAll();
}

? 5)编写对应的mapper文件


      insert into dept values (#{dName},DATABASE());
  

? 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 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 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 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)导入依赖


org.example
        springcloud-api
        1.0-SNAPSHOTorg.mybatis.spring.boot
        mybatis-spring-boot-starter
    org.springframework.boot
        spring-boot-devtools
    

? 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 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 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快速搭建

标签:不用   序列   response   搭建   localhost   http   repos   sql   风格   

原文地址:https://www.cnblogs.com/mjjh/p/13340824.html


评论


亲,登录后才可以留言!