众所周知,springcloud是一个以springboot为主的框架集合体(其他什么优缺点自己搜,哪里都有)。所以当我们开始创建这个项目时自然是从springboot(Maven也可)开始。
我是用的Maven,因为初步demo用springboot我懒得勾。
一、创建maven项目
首先在新建项目里把自动生成的src给删了。然后在项目里建一个maven对象。结构如下:
二、创建实体对象
因为springcloud是将服务分开,所以单独暴露一个服务用来放pojo和dao层(放不放dao层看个人习惯吧)。我的命名习惯是服务类+端口。api结构如下:
package com.demo.springcloud.api.domain;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@NoArgsConstructor
@Accessors(chain = true) //就是同一个对象可以接着点属性比如new People.setId("1").setName("12")
public class People implements Serializable {
private String id;
private String name;
private Integer age;
}
三、生产者
在springcloud中生产者专注于提供接口为消费者服务(在这个简易demo中)。大体结构如下:
1.配置
a.父项目pom
<!--打包方式-->
<packaging>pom</packaging>
<properties>
</properties>
<dependencyManagement>
<!--springcloud-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!--数据库-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!--日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</dependencyManagement>
<build>
</build>
b.配置子工程
<?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>springcloudDemo</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-seriv</artifactId>
<dependencies>
<!-- 这个就是服务提供者,所以需要拿到api里面的对象-->
<dependency>
<groupId>com.demo</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
c.yml配置
#服务端口
server:
port: 8001
#mybatis配置
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.demo.springcloud.api.domain
config-location: classpath:mybatis/mapper-config.xml
#spring配置
spring:
application:
name: people-servive #服务类名
datasource: #数据库配置
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/workformyself?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root
password: password
2.dao层
package com.demo.springcloud.people.dao;
import com.demo.springcloud.api.domain.People;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Mapper
@Repository
public interface PeopleDao {
/**
* 添加
*/
int addPeople(People people);
/**
* 根据ID查找
*/
People getById(String id);
/**
* @Description:列表查询
* @param: map
* @return: List<People>
* @auther: Rosemary
* @date: 2020/9/30 1:04
*/
List<People> list(Map<String, Object> map);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.springcloud.people.dao.PeopleDao">
<insert id="addPeople" parameterType="com.demo.springcloud.api.domain.People">
insert into people
(`id`,
`name`,
`age`,
`data_base`
)
values (
#{id},
#{name},
#{age},
DATABASE()
)
</insert>
<select id="getById" parameterType="String" resultType="com.demo.springcloud.api.domain.People">
select `id`,
`name`,
`age`,
`data_base`
from
people
where id=#{value}
</select>
<select id="list" resultType="com.demo.springcloud.api.domain.People">
select `id`,
`name`,
`age`,
`data_base`
from
people
</select>
</mapper>
4.service层
package com.demo.springcloud.people.serice;
import com.demo.springcloud.api.domain.People;
import java.util.List;
import java.util.Map;
public interface PeopleService {
/**
* 添加
*/
int addPeople(People people);
/**
* 根据ID查找
*/
People getById(String id);
/**
* @Description:列表查询
* @param: map
* @return: List<People>
* @auther: Rosemary
* @date: 2020/9/30 1:04
*/
List<People> list(Map<String, Object> map);
}
package com.demo.springcloud.people.serice.impl;
import com.demo.springcloud.api.domain.People;
import com.demo.springcloud.people.dao.PeopleDao;
import com.demo.springcloud.people.serice.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @author: Rosemary
* @date: 2020/9/30 1:17
* @description:
***/
@Service
public class PeopleServiceImpl implements PeopleService {
@Autowired
private PeopleDao peopleDao;
public int addPeople(People people) {
return peopleDao.addPeople(people);
}
public People getById(String id) {
return peopleDao.getById(id);
}
public List<People> list(Map<String, Object> map) {
return peopleDao.list(map);
}
}
5.controller层
package com.demo.springcloud.people.comtroller;
import com.demo.springcloud.api.domain.People;
import com.demo.springcloud.people.serice.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
/**
* @author: Rosemary
* @date: 2020/9/30 1:19
* @description:
*/
@RestController
@RequestMapping("/provider/people")
public class PeopleController {
@Autowired
private PeopleService peopleService;
@PostMapping("/save")
@ResponseBody
public People save(@RequestBody People people) {
if (people == null) {
return null;
}
if (peopleService.addPeople(people) > 0) {
return people;
} else
return null;
}
@GetMapping("/getById/{id}")
@ResponseBody
public People getById(@PathVariable("id") String id) {
if (id == null || id == "") {
return null;
}
return peopleService.getById(id);
}
@GetMapping("/list")
@ResponseBody
public List<People> list() {
HashMap<String, Object> map = new HashMap<String, Object>();
return peopleService.list(map);
}
}
6.启动类
package com.demo.springcloud.people;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author: Rosemary
* @date: 2020/9/30 1:27
* @description:启动类
*/
@SpringBootApplication
public class PeopleApplication {
public static void main(String[] args) {
SpringApplication.run(PeopleApplication.class, args);
System.out.println("人民服务类启动!!!!!");
}
}
然后服务层启动查看是否可以运行,成功以后创建消费者类
四、消费者服务
消费者不需要service层,只需要从生产者调用就行了。结构是:
1.配置文件
a.yml
server:
port: 8005
b.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>springcloudDemo</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-customer-80</artifactId>
<!--消费者不需要打包,所以只需要实体类和web-->
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.demo</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
</project>
因为消费者并没有引入生产者的包,所以我们需要用另一个形式来解决引入这个问题。
2.配置引入bean(restFul风格)
package com.demo.springcloud.com.demo.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author: Rosemary
* @date: 2020/9/30 17:52
* @description:
*/
@Configuration//类似于configurationText.xml
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
3.controller层
package com.demo.springcloud.com.demo.springcloud.controller;
import com.demo.springcloud.api.domain.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
/**
* @author: Rosemary
* @date: 2020/9/30 17:19
* @description: 消费者是不需要service层的,于是就出现了restFul风格像下面这样
*/
@RestController
@RequestMapping("/customer/peopel")
public class PeopleCustomerController {
@Autowired
private RestTemplate restTemplate;//简单模板。负责调用http的各种方法
private static final String REST_URL_PREFIX = "http://localhost:8001/provider/people";
@RequestMapping("/getById/{id}")
public People getById(@PathVariable("id") String id) {
if (StringUtils.isEmpty(id)) {
return null;
}
//get请求
String s = REST_URL_PREFIX + "/getById/" + id;
People forObject = restTemplate.getForObject(s, People.class);
return forObject;
}
@RequestMapping("/save")
@ResponseBody
public People save(@RequestBody People people) {
if (people == null) {
return null;
}
//post请求
return restTemplate.postForObject(REST_URL_PREFIX + "/save", people, People.class);
}
@RequestMapping("/list")
@ResponseBody
public People list() {
//post请求
return restTemplate.getForObject(REST_URL_PREFIX + "/provider/people/list", People.class);
}
}
4.启动类
package com.demo.springcloud.com.demo.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* @author: Rosemary
* @date: 2020/9/30 18:05
* @description:
*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.demo.*"})
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
System.out.println("人民消费者启动成功!!!!!!");
}
}
一个简易的cloudDemo就搭好了