137.【SpringCloud-快速搭建】

微服务框架搭建

  • (一)、SpringCloud-Parent
    • 1.创建一个SpringBoot项目
    • 2.导入我们的依赖
  • (二)、SpringCloud-API (实体类)
    • 1.创建一个SpringBoot项目
    • 2.导入我们的依赖
    • 3.创建我们的实体类
  • (三)、SpringCloud-dept (业务A)
    • 1.创建一个SpringBoot项目
    • 2.导入我们的依赖
    • 3.配置我们的配置信息
    • 4.配置我们的MVC框架
      • (1).Dao层和XML
      • (2).server和serverImpl
      • (3).controller
    • 5.测试
      • (1).测试查询
      • (2).测试添加
      • (3).查询全部
  • (四)、SpringCloud-counsmer (用户)
    • 1.创建一个SpringBoot项目
    • 2.导入我们的依赖
    • 3.配置我们的配置信息
    • 4.配置我们的MVC框架
      • (1).controller
    • 5.测试
      • (1).查询全部
      • (2).测试添加
      • (3).测试查找
  • (五)、==⭐配置Eureka注册中心 (服务端)⭐==
    • 1.创建一个SpringBoot项目
    • 2.导入我们的依赖
    • 3.配置我们的配置信息
    • 4.添加注册中心注解
    • 5.测试
  • (六)、SpringCloud-Provider (服务提供者)
    • 1.创建一个SpringBoot项目
    • 2.导入我们的依赖
    • 3.配置我们的配置信息
    • 4.向注册中心注册服务
    • 5.测试
  • (七)、SpringCloud-Provider (服务提供者完整版)
    • 1.导入我们的依赖
    • 2.配置我们的配置文件
    • 3.配置我们的MVC三层框架
      • (1).dao层和mapper的配置
      • (2).server和serverImpl的配置
      • (3).controller的配置
    • 4.向注册中心注册服务
  • (八)、SpringCloud-Consumer (消费者-完整版)
    • 1.导入我们的依赖
    • 2.配置我们的配置文件
    • 3.配置我们的MVC框架
    • 4.向注册中心注册服务
    • 5.测试

(一)、SpringCloud-Parent

1.创建一个SpringBoot项目

然后只保留这四个文件
137.【SpringCloud-快速搭建】_第1张图片

2.导入我们的依赖


<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.0modelVersion>

    
    <modules>
        <module>demo01module>
        <module>demo02module>
    modules>
    
    <groupId>com.jsxsgroupId>
    <artifactId>SpringCloundTestartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>SpringCloundTestname>
    <description>SpringCloud-parentdescription>
    
    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <junit.version>4.12junit.version>
        <log4j.version>1.2.17log4j.version>
        <lombok.version>1.16.18lombok.version>
    properties>

    
    <packaging>pompackaging>

    
    <dependencyManagement>
        
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Hoxton.SR8version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.3.3.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.6version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.1.10version>
            dependency>
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>1.3.2version>
            dependency>
            
            <dependency>
                <groupId>ch.qos.logbackgroupId>
                <artifactId>logback-coreartifactId>
                <version>1.2.3version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>${junit.version}version>
            dependency>
            
            <dependency>
                <groupId>log4jgroupId>
                <artifactId>log4jartifactId>
                <version>${log4j.version}version>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>
        dependencies>
    dependencyManagement>
project>

(二)、SpringCloud-API (实体类)

这里的API相当于是我们实体类,实体类全部放在这!!! 实体类不需要启动类和测试类,所以我们需要把这个实体类的启动类给手动删除。

1.创建一个SpringBoot项目

137.【SpringCloud-快速搭建】_第2张图片

2.导入我们的依赖


<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.0modelVersion>
    
    <parent>
        <groupId>com.jsxsgroupId>
        <artifactId>SpringCloundTestartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    
    <groupId>com.jsxsgroupId>
    <artifactId>demo01artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demo01name>
    <description>Demo project for Spring Bootdescription>
    
    <properties>
        <java.version>1.8java.version>
    properties>
    
    <dependencies>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

3.创建我们的实体类

package com.jsxs.pojo;

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

import java.io.Serializable;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:08
 * @PackageName:com.jsxs.pojo
 * @ClassName: Dept
 * @Description: TODO
 * @Version 1.0
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)  //目的是为了打开链式写法,
public class Dept implements Serializable {  // 实体类在网络通信的时候,一定要进行序列化,否则会报错 ORM关系

    private Long deptno;  // 主键
    private String dname;  //部门的名字
    // 这里的数据存在哪个数据库里面。微服务: 一个服务对应一个数据库,同一个信息可能存在不同的数据库中。
    private String db_source;
}

(三)、SpringCloud-dept (业务A)

这里是我们的一个业务:

1.创建一个SpringBoot项目

137.【SpringCloud-快速搭建】_第3张图片

2.导入我们的依赖


<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.0modelVersion>
    
    <parent>
        <groupId>com.jsxsgroupId>
        <artifactId>SpringCloundTestartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    
    <groupId>com.jsxsgroupId>
    <artifactId>demo02artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demo02name>
    <description>Demo project for Spring Bootdescription>
    
    <properties>
        <java.version>1.8java.version>
    properties>
    
    <dependencies>
        
        <dependency>
            <groupId>com.jsxsgroupId>
            <artifactId>demo01artifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-testartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
project>

3.配置我们的配置信息

server:
  port: 8081
# Mybatis的配置
mybatis:
  type-aliases-package: com.jsxs.pojo  # 起别名
  mapper-locations: classpath:mybatis/mapper/*.xml  # 路径配置
  # Spring的配置
spring:
  application:
    name: SpringCloud-provider-dept
  datasource:
    url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 121788
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

4.配置我们的MVC框架

(1).Dao层和XML

com/jsxs/dao/DeptDao.java

package com.jsxs.dao;

import com.jsxs.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:29
 * @PackageName:com.jsxs.dao
 * @ClassName: DeptDao
 * @Description: TODO
 * @Version 1.0
 */
@Mapper
@Repository
public interface DeptDao {

    public boolean addDept(Dept dept);

    public Dept queryById(Long id);

    public List<Dept> all();
}

mybatis/mapper/DeptMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.jsxs.dao.DeptDao">
    
    <insert id="addDept" parameterType="com.jsxs.pojo.Dept">
        insert into dept(deptno,dname,db_source)
        values(#{deptno},#{dname},DATABASE())
    insert>

    <select id="queryById" resultType="com.jsxs.pojo.Dept" parameterType="Long">
        select *from dept where deptno=#{id}
    select>

    <select id="all" resultType="com.jsxs.pojo.Dept">
        select *from dept
    select>
mapper>

(2).server和serverImpl

com/jsxs/service/DeptService.java

package com.jsxs.service;

import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:56
 * @PackageName:com.jsxs.dao.service
 * @ClassName: DeptService
 * @Description: TODO
 * @Version 1.0
 */
public interface DeptService {

    public boolean addDept(Dept dept);

    public Dept queryById(Long id);

    public List<Dept> all();
}

com/jsxs/service/DeptServiceImpl.java

package com.jsxs.service;

import com.jsxs.dao.DeptDao;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:57
 * @PackageName:com.jsxs.dao.service
 * @ClassName: DeptServiceImpl
 * @Description: TODO
 * @Version 1.0
 */

@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    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> all() {
        return deptDao.all();
    }
}

(3).controller

com/jsxs/controller/DeptController.java

package com.jsxs.controller;

import com.jsxs.pojo.Dept;
import com.jsxs.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:59
 * @PackageName:com.jsxs.controller
 * @ClassName: DeptController
 * @Description: TODO
 * @Version 1.0
 */

@RestController
public class DeptController {
    @Resource
    private DeptService deptService;

    @PostMapping("/dept/add")
    public Boolean addDept(Dept dept) {
        boolean flag = deptService.addDept(dept);
        return flag;
    }

    @GetMapping("/dept/queryById/{id}")
    public Dept queryById(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        return dept;
    }

    @GetMapping("/dept/all")
    public List<Dept> all() {
        List<Dept> all = deptService.all();
        return all;
    }
}

5.测试

(1).测试查询

http://localhost:8081/dept/queryById/1
137.【SpringCloud-快速搭建】_第4张图片

(2).测试添加

http://localhost:8081/dept/add?deptno=1&dname=李明&db_source=db01
137.【SpringCloud-快速搭建】_第5张图片

(3).查询全部

137.【SpringCloud-快速搭建】_第6张图片

(四)、SpringCloud-counsmer (用户)

用户使用业务A

1.创建一个SpringBoot项目

137.【SpringCloud-快速搭建】_第7张图片

2.导入我们的依赖


<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.0modelVersion>
    
    <parent>
        <groupId>com.jsxsgroupId>
        <artifactId>SpringCloundTestartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    
    <groupId>com.jsxsgroupId>
    <artifactId>demo03artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demo03name>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    
    <dependencies>
        
        <dependency>
            <groupId>com.jsxsgroupId>
            <artifactId>demo01artifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

3.配置我们的配置信息

server:
  port: 80

com/jsxs/config/ConfigBean.java
提供多种便捷访问远程 访问http服务的方法,简单的Rest

package com.jsxs.config;

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

/**
 * @Author Jsxs
 * @Date 2023/10/15 21:16
 * @PackageName:com.jsxs.config
 * @ClassName: ConfigBean
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
public class ConfigBean {

    //  把RestTemplate注入到Spring中去
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

4.配置我们的MVC框架

因为我们的用户层,只需要控制层即可。不需要业务逻辑层什么的。

(1).controller

package com.jsxs.controller;

import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 21:21
 * @PackageName:com.jsxs.controller
 * @ClassName: DeptConsumerController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
public class DeptConsumerController {
    @Resource
    // (URI url, 实体 map,Class responseType) 地址---- 实体 ----返回类型.class
    private RestTemplate restTemplate;   // 提供多种便捷访问远程   访问http服务的方法,简单的Rest

    //  设置服务层的前缀为常量
    private static final String REST_URL_PREFIX = "http://localhost:8081";

    //  根据id进行数据的查找
    // 用户层的这个访问路径可以随意写,没有必要和服务层的路径完全一致
    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long deptno) {
        // 这里返回的是: 服务层的路径
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/queryById/" + deptno, Dept.class);
    }

    //  添加数据
    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    //  查找全部数据
    @RequestMapping("/consumer/dept/all")
    public List<Dept> all() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/all", List.class);
    }
}

5.测试

==启动

(1).查询全部

137.【SpringCloud-快速搭建】_第8张图片

(2).测试添加

137.【SpringCloud-快速搭建】_第9张图片

(3).测试查找

137.【SpringCloud-快速搭建】_第10张图片

以上的微服务框架并未使用我们的注册中心,不能说是真正的微服务框架的!!!

(五)、⭐配置Eureka注册中心 (服务端)⭐

1.创建一个SpringBoot项目

137.【SpringCloud-快速搭建】_第11张图片

2.导入我们的依赖


<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.0modelVersion>
    
    <parent>
        <groupId>com.jsxsgroupId>
        <artifactId>SpringCloundTestartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    
    <groupId>com.jsxsgroupId>
    <artifactId>eureka-7001artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>eureka-7001name>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
            <version>1.4.6.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-testartifactId>
            <scope>testscope>
        dependency>

    dependencies>

project>

3.配置我们的配置信息

server:
  port: 7001

eureka:
  instance:
    prefer-ip-address: true
    hostname: localhost
  client:
    register-with-eureka: false  # 是否将自己注册进注册中心
    fetch-registry: false  # 是否是注册中心:  是
    service-url:
      defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka/

4.添加注册中心注解

package com.jsxs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication

// ⭐⭐ 开启我们的Eureka服务端
@EnableEurekaServer

public class Eureka7001Application {

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

}

5.测试

137.【SpringCloud-快速搭建】_第12张图片

(六)、SpringCloud-Provider (服务提供者)

1.创建一个SpringBoot项目

137.【SpringCloud-快速搭建】_第13张图片

2.导入我们的依赖


<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.0modelVersion>
    
    <parent>
        <groupId>com.jsxsgroupId>
        <artifactId>SpringCloundTestartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    
    <groupId>com.jsxsgroupId>
    <artifactId>eureka-provider-8001artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>eureka-provider-8001name>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
            <version>1.4.6.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

project>

3.配置我们的配置信息

server:
  port: 8001
  # Spring的配置
spring:
  application:
    name: SpringCloud-provider-dept
  # Eureka 服务注册到哪里?
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: jsxs-01

4.向注册中心注册服务

package com.jsxs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient

public class EurekaProvider8001Application {

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

}

5.测试

137.【SpringCloud-快速搭建】_第14张图片

(七)、SpringCloud-Provider (服务提供者完整版)

1.导入我们的依赖


<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.0modelVersion>
    
    <parent>
        <groupId>com.jsxsgroupId>
        <artifactId>SpringCloundTestartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    
    <groupId>com.jsxsgroupId>
    <artifactId>demo02artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demo02name>
    <description>Demo project for Spring Bootdescription>
    
    <properties>
        <java.version>1.8java.version>
    properties>
    
    <dependencies>
        
        <dependency>
            <groupId>com.jsxsgroupId>
            <artifactId>demo01artifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

        
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
            <version>1.4.6.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-testartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiterartifactId>
            <scope>testscope>
        dependency>
    dependencies>
project>

2.配置我们的配置文件

server:
  port: 8081
# Mybatis的配置
mybatis:
  type-aliases-package: com.jsxs.pojo  # 起别名
  mapper-locations: classpath:mybatis/mapper/*.xml  # 路径配置
  # Spring的配置
spring:
  application:
    name: SpringCloud-provider-dept
  datasource:
    url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 121788
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    
eureka:
  client:
    service-url: 
      defaultZone: http://localhost:7001/eureka/ #注册到这个注册中心
  instance:
    instance-id: jsxs-dept-eureka-8081 # 服务者的id为这个
    
info:
  app.name: jsxs-springcloud
  company.name: pdd

3.配置我们的MVC三层框架

(1).dao层和mapper的配置

com/jsxs/dao/DeptDao.java

package com.jsxs.dao;

import com.jsxs.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:29
 * @PackageName:com.jsxs.dao
 * @ClassName: DeptDao
 * @Description: TODO
 * @Version 1.0
 */
@Mapper
@Repository
public interface DeptDao {

    public boolean addDept(Dept dept);

    public Dept queryById(Long id);

    public List<Dept> all();
}

mybatis/mapper/DeptMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.jsxs.dao.DeptDao">
    
    <insert id="addDept" parameterType="com.jsxs.pojo.Dept">
        insert into dept(deptno,dname,db_source)
        values(#{deptno},#{dname},DATABASE())
    insert>

    <select id="queryById" resultType="com.jsxs.pojo.Dept" parameterType="Long">
        select *from dept where deptno=#{id}
    select>

    <select id="all" resultType="com.jsxs.pojo.Dept">
        select *from dept
    select>
mapper>

(2).server和serverImpl的配置

com/jsxs/service/DeptService.java

package com.jsxs.service;

import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:56
 * @PackageName:com.jsxs.dao.service
 * @ClassName: DeptService
 * @Description: TODO
 * @Version 1.0
 */
public interface DeptService {

    public boolean addDept(Dept dept);

    public Dept queryById(Long id);

    public List<Dept> all();
}

com/jsxs/service/DeptServiceImpl.java

package com.jsxs.service;

import com.jsxs.dao.DeptDao;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:57
 * @PackageName:com.jsxs.dao.service
 * @ClassName: DeptServiceImpl
 * @Description: TODO
 * @Version 1.0
 */

@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    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> all() {
        return deptDao.all();
    }
}

(3).controller的配置

com/jsxs/controller/DeptController.java

package com.jsxs.controller;

import com.jsxs.pojo.Dept;
import com.jsxs.service.DeptService;
import org.springframework.stereotype.Controller;
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 javax.annotation.Resource;
import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 19:59
 * @PackageName:com.jsxs.controller
 * @ClassName: DeptController
 * @Description: TODO
 * @Version 1.0
 */

@RestController
public class DeptController {
    @Resource
    private DeptService deptService;

    @PostMapping("/dept/add")
    public Boolean addDept(Dept dept) {
        boolean flag = deptService.addDept(dept);
        return flag;
    }

    @GetMapping("/dept/queryById/{id}")
    public Dept queryById(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        return dept;
    }

    @GetMapping("/dept/all")
    public List<Dept> all() {
        List<Dept> all = deptService.all();
        return all;
    }
}

4.向注册中心注册服务

com/jsxs/Demo02Application.java

package com.jsxs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient

public class Demo02Application {

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

}

(八)、SpringCloud-Consumer (消费者-完整版)

1.导入我们的依赖


<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.0modelVersion>
    
    <parent>
        <groupId>com.jsxsgroupId>
        <artifactId>SpringCloundTestartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    
    <groupId>com.jsxsgroupId>
    <artifactId>demo03artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demo03name>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    
    <dependencies>
        
        <dependency>
            <groupId>com.jsxsgroupId>
            <artifactId>demo01artifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
        
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
            <version>1.4.6.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

2.配置我们的配置文件

server:
  port: 80

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/  #像那一个注册中心注册自己
  instance:
    instance-id: jsxs-dept-eureka-80  # 编号
spring:
  application:
    name: SpringCloud-counsumer-dept

3.配置我们的MVC框架

com/jsxs/config/ConfigBean.java

package com.jsxs.config;

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

/**
 * @Author Jsxs
 * @Date 2023/10/15 21:16
 * @PackageName:com.jsxs.config
 * @ClassName: ConfigBean
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
public class ConfigBean {

    //  把RestTemplate注入到Spring中去
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

com/jsxs/controller/DeptConsumerController.java

package com.jsxs.controller;

import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Jsxs
 * @Date 2023/10/15 21:21
 * @PackageName:com.jsxs.controller
 * @ClassName: DeptConsumerController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
public class DeptConsumerController {
    @Resource
    // (URI url, 实体 map,Class responseType) 地址---- 实体 ----返回类型.class
    private RestTemplate restTemplate;   // 提供多种便捷访问远程   访问http服务的方法,简单的Rest

    //  设置服务层的前缀为常量
    private static final String REST_URL_PREFIX = "http://localhost:8081";

    //  根据id进行数据的查找
    // 用户层的这个访问路径可以随意写,没有必要和服务层的路径完全一致
    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long deptno) {
        // 这里返回的是: 服务层的路径
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/queryById/" + deptno, Dept.class);
    }

    //  添加数据
    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    //  查找全部数据
    @RequestMapping("/consumer/dept/all")
    public List<Dept> all() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/all", List.class);
    }
}

4.向注册中心注册服务

package com.jsxs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient

public class Demo03Application {

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

}

5.测试

  • 先开启注册中心: eureka->7001;
  • 然后开启我们的服务提供者: demo2->8081;
  • 最后开启我们的服务消费者: demo3->80

137.【SpringCloud-快速搭建】_第15张图片

你可能感兴趣的:(spring,cloud,spring,后端)