您现在的位置是:首页 >学无止境 >SpringCloud01:SpringCloud介绍、服务提供者、服务消费者网站首页学无止境
SpringCloud01:SpringCloud介绍、服务提供者、服务消费者
SpringCloud和SpringBoot的关系
SpringBoot专注于快速、方便地开发单个个体微服务,SpringCloud关注全局的治理框架,它将SpringBoot开发的一个个单体微服务整合并管理起来,为各个微服务之间提供:管理配置、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等集成服务
SpringCloud
SpringCloud和Dubbo的最大区别:SpringCloud抛弃了Dubbo的RPC通信,采用的是基于HTTP的REST方式
自学网站
Spring Cloud Netfix:Spring Cloud Netflix【spring cloud中文版】--spring cloud中文文档
中文API文档:Spring Cloud Dalston 中文文档 参考手册 中文版
SpringCloud中国社区:Spring Cloud中国社区
SpringCloud中文网:Spring Cloud中文网-官方文档中文版
SpringCloud版本选择
版本说明
实际开发版本关系
创建父工程
创建一个maven工程,去掉src目录
配置pom.xml
<?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>org.example</groupId>
<artifactId>SpringCloud</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包方式 pom-->
<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>
<!--springCloud的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringBoot-->
<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>
<!--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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
服务提供者
一、创建子module1(springcloud-api)
1、创建数据库
create database db01
use db01
create table dept(
deptno int primary key not null auto_increment,
dname varchar(20),
db_source varchar(20)
)
insert into dept(dname,db_source) values('开发部',DATABASE());
insert into dept(dname,db_source) values('销售部',DATABASE());
insert into dept(dname,db_source) values('运维部',DATABASE());
insert into dept(dname,db_source) values('测试部',DATABASE());
insert into dept(dname,db_source) values('财务部',DATABASE());
2、创建实体类
@Data
@NoArgsConstructor
@Accessors(chain = true) //链式写法
public class Dept implements Serializable {
private Integer deptno;
private String dname;
//表示这个数据是存在哪个数据库的字段
//微服务:一个服务对应一个数据库,同一个信息可能存在不同的数据库
private String dbSource;
public Dept(String dname) {
this.dname = dname;
}
}
二、创建子模块2(springcloud-provider-dept-8081)
1、pom.xml
<?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>SpringCloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-provider-dept-8001</artifactId>
<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>
<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>
<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>
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
2、Dao
①DeptDao
@Mapper
@Repository
public interface DeptDao {
public Integer addDept(Dept dept);
public Dept queryById(Integer deptno);
public List<Dept> queryAll();
}
②DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springcloud.dao.DeptDao">
<resultMap id="DeptMap" type="Dept">
<id property="deptno" column="deptno"></id>
<result property="dname" column="dname"></result>
<result property="dbSource" column="db_source"></result>
</resultMap>
<insert id="addDept" parameterType="Dept">
insert into dept(dname, db_source) values(#{dname}, DATABASE())
</insert>
<select id="queryById" parameterType="Integer" resultMap="DeptMap">
select * from dept where deptno = #{deptno}
</select>
<select id="queryAll" resultMap="DeptMap">
select * from dept
</select>
</mapper>
3、application.xml
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.springcloud.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
#spring配置
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
drive-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db01?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: 123456
4、service
①DeptService
public interface DeptService {
public Integer addDept(Dept dept);
public Dept queryById(Integer deptno);
public List<Dept> queryAll();
}
②DeptServiceImpl
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptDao deptDao;
@Override
public Integer addDept(Dept dept) {
return deptDao.addDept(dept);
}
@Override
public Dept queryById(Integer deptno) {
return deptDao.queryById(deptno);
}
@Override
public List<Dept> queryAll() {
return deptDao.queryAll();
}
}
5、controller
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping("/dept/add")
public Integer addDept(Dept dept){
return deptService.addDept(dept);
}
@GetMapping("/dept/get/{deptno}")
public Dept queryById(@PathVariable("deptno") Integer deptno){
return deptService.queryById(deptno);
}
@GetMapping("/dept/list")
public List<Dept> queryAll(){
return deptService.queryAll();
}
}
6、主启动类
//启动类
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args){
SpringApplication.run(DeptProvider_8001.class,args);
}
}
服务消费者
三、创建子模块3(springcloud-consumer-dept-80)
1、pom.xml
<?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>SpringCloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-dept-80</artifactId>
<!--实体类+web-->
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
2、 application.xml
server:
port: 8080
3、ConfigBean(用来注册RestTemplate的Bean)
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
4、controller
@RestController
public class DeptConsumerController {
//理解:消费者,不应该有service
@Autowired
private RestTemplate restTemplate; //提供多种便捷访问远程http服务的方法,简单的Restful服务模板
private static final String REST_URL_PREFIX = "http://localhost:8001";
@RequestMapping("/consumer/dept/add")
public Integer addDept(Dept dept){
return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept,Integer.class);
}
@RequestMapping("/consumer/dept/get/{deptno}")
public Dept queryById(@PathVariable("deptno") Integer deptno){
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + deptno, Dept.class);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> list(){
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
}
}