微服务框架搭建
- (一)、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项目
然后只保留这四个文件
2.导入我们的依赖
<?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><!--1.⭐ 这个是后面我们有多少子项目,然后都写在这里进行统一管理。 --><modules><module>demo01</module><module>demo02</module></modules><!--2.⭐ 这个是我们本父类的三大件 --><groupId>com.jsxs</groupId><artifactId>SpringCloundTest</artifactId><version>0.0.1-SNAPSHOT</version><name>SpringCloundTest</name><description>SpringCloud-parent</description><!--3.⭐ 进行统一管理我们的版本信息 --><properties><java.version>1.8</java.version><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><!--4. ⭐ 更改打包方式--><packaging>pom</packaging><!--5. 使用依赖管理--><dependencyManagement><!--6. ⭐创建依赖项--><dependencies><!-- SpringCloud 的依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR8</version><type>pom</type><scope>import</scope></dependency><!-- SpringBoot 的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.3.RELEASE</version><type>pom</type><scope>import</scope></dependency><!-- MySql 数据库--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><!-- 导入德鲁伊数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><!--SpringBoot 启动器--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!--日志测试~--><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</version></dependency><!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><!-- log4j--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency></dependencies></dependencyManagement>
</project>
(二)、SpringCloud-API (实体类)
这里的API相当于是我们实体类,实体类全部放在这!!! 实体类不需要启动类和测试类,所以我们需要把这个实体类的启动类给手动删除。
1.创建一个SpringBoot项目
2.导入我们的依赖
<?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><!--1. ⭐指定他的父亲 --><parent><groupId>com.jsxs</groupId><artifactId>SpringCloundTest</artifactId><version>0.0.1-SNAPSHOT</version></parent><!--2. ⭐三件套 --><groupId>com.jsxs</groupId><artifactId>demo01</artifactId><version>0.0.1-SNAPSHOT</version><name>demo01</name><description>Demo project for Spring Boot</description><!--3. ⭐指定版本 --><properties><java.version>1.8</java.version></properties><!--4. ⭐依赖性 --><dependencies><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></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项目
2.导入我们的依赖
<?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><!--1.⭐指定我们的父类 --><parent><groupId>com.jsxs</groupId><artifactId>SpringCloundTest</artifactId><version>0.0.1-SNAPSHOT</version></parent><!--2.⭐三件套 --><groupId>com.jsxs</groupId><artifactId>demo02</artifactId><version>0.0.1-SNAPSHOT</version><name>demo02</name><description>Demo project for Spring Boot</description><!--4.⭐版本控制 --><properties><java.version>1.8</java.version></properties><!--5.⭐依赖 --><dependencies><!--6.⭐⭐ 引入我们的实体类数据-》API --><dependency><groupId>com.jsxs</groupId><artifactId>demo01</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></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><!-- test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></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-deptdatasource:url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8username: rootpassword: 121788driver-class-name: com.mysql.jdbc.Drivertype: 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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 1. 我们首先要绑定工作空间-->
<mapper namespace="com.jsxs.dao.DeptDao"><!-- 2. 我们编写SQL语句 --><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 {@Resourceprivate DeptDao deptDao;@Overridepublic boolean addDept(Dept dept) {return deptDao.addDept(dept);}@Overridepublic Dept queryById(Long id) {return deptDao.queryById(id);}@Overridepublic 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 {@Resourceprivate 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
(2).测试添加
http://localhost:8081/dept/add?deptno=1&dname=李明&db_source=db01
(3).查询全部
(四)、SpringCloud-counsmer (用户)
用户使用业务A
1.创建一个SpringBoot项目
2.导入我们的依赖
<?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><!--1. ⭐ 引入我们的父项目 --><parent><groupId>com.jsxs</groupId><artifactId>SpringCloundTest</artifactId><version>0.0.1-SNAPSHOT</version></parent><!--2. ⭐本项目的三件套 --><groupId>com.jsxs</groupId><artifactId>demo03</artifactId><version>0.0.1-SNAPSHOT</version><name>demo03</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><!--3.⭐ 依赖选项 --><dependencies><!--1. ⭐⭐ 这个引入我们的实体类 --><dependency><groupId>com.jsxs</groupId><artifactId>demo01</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- 热部署工具--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></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中去@Beanpublic 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<T> responseType) 地址---- 实体 ----返回类型.classprivate 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).查询全部
(2).测试添加
(3).测试查找
以上的微服务框架并未使用我们的注册中心,不能说是真正的微服务框架的!!!
(五)、⭐配置Eureka注册中心 (服务端)⭐
1.创建一个SpringBoot项目
2.导入我们的依赖
<?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><!--1.⭐引入我们的父类 --><parent><groupId>com.jsxs</groupId><artifactId>SpringCloundTest</artifactId><version>0.0.1-SNAPSHOT</version></parent><!--2.⭐自己的三件套 --><groupId>com.jsxs</groupId><artifactId>eureka-7001</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-7001</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><!--3.⭐导入我们的依赖 --><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.4.6.RELEASE</version></dependency><!-- 热部署工具 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><scope>test</scope></dependency></dependencies></project>
3.配置我们的配置信息
server:port: 7001eureka:instance:prefer-ip-address: truehostname: localhostclient: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服务端
@EnableEurekaServerpublic class Eureka7001Application {public static void main(String[] args) {SpringApplication.run(Eureka7001Application.class, args);}}
5.测试
(六)、SpringCloud-Provider (服务提供者)
1.创建一个SpringBoot项目
2.导入我们的依赖
<?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><!--1.引入我们的父类 --><parent><groupId>com.jsxs</groupId><artifactId>SpringCloundTest</artifactId><version>0.0.1-SNAPSHOT</version></parent><!--2.自己的三件套 --><groupId>com.jsxs</groupId><artifactId>eureka-provider-8001</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-provider-8001</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><!--3.依赖 --><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.6.RELEASE</version></dependency><!-- ⭐一定要添加web的驱动,要不然启动不成功!!! --><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><scope>test</scope></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@EnableEurekaClientpublic class EurekaProvider8001Application {public static void main(String[] args) {SpringApplication.run(EurekaProvider8001Application.class, args);}}
5.测试
(七)、SpringCloud-Provider (服务提供者完整版)
1.导入我们的依赖
<?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><!--1.⭐指定我们的父类 --><parent><groupId>com.jsxs</groupId><artifactId>SpringCloundTest</artifactId><version>0.0.1-SNAPSHOT</version></parent><!--2.⭐三件套 --><groupId>com.jsxs</groupId><artifactId>demo02</artifactId><version>0.0.1-SNAPSHOT</version><name>demo02</name><description>Demo project for Spring Boot</description><!--4.⭐版本控制 --><properties><java.version>1.8</java.version></properties><!--5.⭐依赖 --><dependencies><!--6.⭐⭐ 引入我们的实体类数据-》API --><dependency><groupId>com.jsxs</groupId><artifactId>demo01</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- 7. ⭐⭐⭐ eureka客户端的和监视器 --><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.6.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></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><!-- test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><scope>test</scope></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-deptdatasource:url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8username: rootpassword: 121788driver-class-name: com.mysql.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceeureka:client:service-url: defaultZone: http://localhost:7001/eureka/ #注册到这个注册中心instance:instance-id: jsxs-dept-eureka-8081 # 服务者的id为这个info:app.name: jsxs-springcloudcompany.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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 1. 我们首先要绑定工作空间-->
<mapper namespace="com.jsxs.dao.DeptDao"><!-- 2. 我们编写SQL语句 --><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 {@Resourceprivate DeptDao deptDao;@Overridepublic boolean addDept(Dept dept) {return deptDao.addDept(dept);}@Overridepublic Dept queryById(Long id) {return deptDao.queryById(id);}@Overridepublic 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 {@Resourceprivate 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@EnableEurekaClientpublic class Demo02Application {public static void main(String[] args) {SpringApplication.run(Demo02Application.class, args);}}
(八)、SpringCloud-Consumer (消费者-完整版)
1.导入我们的依赖
<?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><!--1. ⭐ 引入我们的父项目 --><parent><groupId>com.jsxs</groupId><artifactId>SpringCloundTest</artifactId><version>0.0.1-SNAPSHOT</version></parent><!--2. 本项目的三件套 --><groupId>com.jsxs</groupId><artifactId>demo03</artifactId><version>0.0.1-SNAPSHOT</version><name>demo03</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><!--3.⭐ 依赖选项 --><dependencies><!--1.这个引入我们的实体类 --><dependency><groupId>com.jsxs</groupId><artifactId>demo01</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!--2.引入我们的erueka和监视器 --><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.6.RELEASE</version></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></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.配置我们的配置文件
server:port: 80eureka: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中去@Beanpublic 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<T> responseType) 地址---- 实体 ----返回类型.classprivate 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@EnableEurekaClientpublic class Demo03Application {public static void main(String[] args) {SpringApplication.run(Demo03Application.class, args);}}
5.测试
- 先开启注册中心: eureka->7001;
- 然后开启我们的服务提供者: demo2->8081;
- 最后开启我们的服务消费者: demo3->80