• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

springCloud总结

开发技术 开发技术 4小时前 4次浏览

springCloud

1、创建一个简单的springcloud项目

先看基本项目结构

springCloud总结

 

 springcloud:父项目,总项目,对jar包版本等管控

springcloud-api:api主要是提供实体类的一个api模块

spring-cloud-provide-***-host:这个是一个服务提供方,提供服务(后台)

spring-cloud-consumer-host:这个是消费方(前台API)

下面对每个模块分析:

springcloud

pom:
<packaging>pom</packaging>
<!-- 版本号-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<junit.version>4.12</junit.version>
<lombok.version>1.16.10</lombok.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- springcloud的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</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>
<!-- 主要用于日志和测试-->
<!-- log4j-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>

主要用来提供jar包版本管理

springcloud-api:

springCloud总结

 

 Dept:
@Data
@NoArgsConstructor
@Accessors(chain = true)// 链式写法
public class Dept implements Serializable {

private Integer deptId;
private String deptName;
// 这个数据存在哪个数据库的字段 一个服务对应一个数据库,同一个信息可能存在不同的数据库
private String dbSource;

public Dept(String deptName, String dbSource) {
this.deptName = deptName;
this.dbSource = dbSource;
}
}

注:务必实现序列化

spring-cloud-provide-***-host:服务提供方

springCloud总结

 

 需要与数据库交互,基本的后端程序

pom文件:

<?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>com.zmd</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-cloud-provide-dept-18021</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 首先我们需要拿到实体类,所以需要配置我们的api moudel-->
<dependency>
<groupId>com.zmd</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>
<!-- 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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</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>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>

</project>

application:

server:
port: 18021
# mybatis配置
mybatis:
type-aliases-package: com.zmd.springcloud.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis.xml
# spring配置
spring:
application:
name: spring-cloud-provide-dept-18021
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/sringcloud01?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
username: root
password: root

spring-cloud-consumer-host;消费方

springCloud总结

 

 

pom文件只需要导入web启动器还有api包

application只需要配置端口号重点是ConfigBean

ConfigBean:

package com.zmd.config;

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

@Configuration
public class ConfigBean {

@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

需要将RestTemplate交给spring管理

controller:


@RestController
@RequestMapping("consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;

@RequestMapping(value = "getDeptById",method = RequestMethod.POST)
@ResponseBody
public Dept getDeptById(Dept dept){
System.out.println(dept);
String url = "http://localhost:18021/deptController/selectById";

return restTemplate.postForObject(url,dept,Dept.class);
}

@RequestMapping(value = "getDepts",method = RequestMethod.POST)
@ResponseBody
public List<Dept> getDepts(){
String url = "http://localhost:18021/deptController/selectAll";
return restTemplate.getForObject(url,List.class);
}

@RequestMapping(value = "addDept",method = RequestMethod.POST)
@ResponseBody
public Boolean addDept(Dept dept){
String url = "http://localhost:18021/deptController/addDept";
return restTemplate.postForObject(url,dept,Boolean.class);
}
}

什么是Eureka

 


程序员灯塔
转载请注明原文链接:springCloud总结
喜欢 (0)