Release Train | Boot Version |
---|---|
2021.0.x aka Jubilee | 2.6.x |
2020.0.x aka Ilford | 2.4.x, 2.5.x (Starting with 2020.0.3) |
Hoxton | 2.2.x, 2.3.x (Starting with SR5) |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
二、创建maven父工程注意:Spring Cloud 官方已经停止对 Dalston、Edgware、Finchley 和 Greenwich 的版本更新。
导入相关依赖
4.0.0
com.sunl
SpringCloud
1.0-SNAPSHOT
pom
UTF-8
1.8
1.8
4.13.2
1.18.22
1.2.17
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR12
pom
import
org.springframework.boot
spring-boot-dependencies
2.3.2.RELEASE
pom
import
mysql
mysql-connector-java
8.0.28
com.alibaba
druid
1.2.8
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
ch.qos.logback
logback-core
1.2.3
junit
junit
${junit.version}
log4j
log4j
${log4j.version}
org.projectlombok
lombok
${lombok.version}
三、创建新的提供者module
1.导入相关依赖
SpringCloud
com.sunl
1.0-SNAPSHOT
4.0.0
cloud-provider-dept-8001
8
8
com.sunl
cloud-api
1.0-SNAPSHOT
junit
junit
test
mysql
mysql-connector-java
com.alibaba
druid
ch.qos.logback
logback-core
org.mybatis.spring.boot
mybatis-spring-boot-starter
org.springframework.boot
spring-boot-test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-devtools
2.配置提供者文件
server:
port: 8001
mybatis:
type-aliases-package: com.sunl.cloud.entity
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
spring:
application:
name: cloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/cloudDB01?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
3.配置mybatis文件:mybatis-config.xml
4.编写sql文件:DeptMapper.java、DeptMapper.xml
package com.sunl.cloud.mapper;
import com.sunl.cloud.entity.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @program: SpringCloud
* @description: 部门接口
* @author: Sunl
* @create: 2022-05-14 20:41
**/
@Mapper
@Repository
public interface DeptMapper {
/**
* 增加部门
* @param dept
* @return
*/
public boolean addDept(Dept dept);
/**
* 根据id查询部门
* @param id
* @return
*/
public Dept queryDept(Long id);
/**
* 获取所有的部门信息
* @return
*/
public List getAllDept();
}
insert into dept(dept_name,db_source)
values (#{deptName},DATABASE());
5.编写业务层代码:DeptService.java、DeptServiceImpl.java
package com.sunl.cloud.service;
import com.sunl.cloud.entity.Dept;
import java.util.List;
/**
* @program: SpringCloud
* @description: 部门业务服务层
* @author: Sunl
* @create: 2022-05-14 20:50
**/
public interface DeptService {
/**
* 增加部门
* @param dept
* @return
*/
public boolean addDept(Dept dept);
/**
* 根据id查询部门
* @param id
* @return
*/
public Dept queryDept(Long id);
/**
* 获取所有的部门信息
* @return
*/
public List getAllDept();
}
package com.sunl.cloud.service.impl;
import com.sunl.cloud.entity.Dept;
import com.sunl.cloud.mapper.DeptMapper;
import com.sunl.cloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @program: SpringCloud
* @description: 部门业务服务实现类
* @author: Sunl
* @create: 2022-05-14 20:51
**/
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
/**
* 增加部门
*
* @param dept
* @return
*/
@Override
public boolean addDept(Dept dept) {
return deptMapper.addDept(dept);
}
/**
* 根据id查询部门
*
* @param id
* @return
*/
@Override
public Dept queryDept(Long id) {
return deptMapper.queryDept(id);
}
/**
* 获取所有的部门信息
*
* @return
*/
@Override
public List getAllDept() {
return deptMapper.getAllDept();
}
}
6.编写控制层代码:DeptController.java
package com.sunl.cloud.controller;
import com.sunl.cloud.entity.Dept;
import com.sunl.cloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;
/**
* @program: SpringCloud
* @description: 部门控制层
* @author: Sunl
* @create: 2022-05-14 20:54
**/
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping("/dept/add")
public boolean addDept(Dept dept){
return deptService.addDept(dept);
}
@GetMapping("/dept/get/{id}")
public Dept getDept(@PathVariable("id") Long id) {
Dept dept = deptService.queryDept(id);
if (dept == null) {
throw new RuntimeException("Fail");
}
return dept;
}
@GetMapping("/dept/list")
public List queryAll() {
deptService.getAllDept()
return deptService.getAllDept();
}
}
7.编写提供者启动类
package com.sunl.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @program: SpringCloud
* @description: 启动类
* @author: Sunl
* @create: 2022-05-14 20:58
**/
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
三、创建新的消费者module
1.编写提供者启动类:ConsumerDept_80.java
package com.sun.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @program: SpringCloud
* @description: 消费者启动类
* @author: Sunl
* @create: 2022-05-14 22:51
**/
@SpringBootApplication
public class ConsumerDept_80 {
public static void main(String[] args) {
SpringApplication.run(ConsumerDept_80.class,args);
}
}
2.编写控制层代码:DeptConsumerController.java
package com.sun.cloud.controller;
import com.sunl.cloud.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @program: SpringCloud
* @description: 消费者控制类
* @author: Sunl
* @create: 2022-05-14 21:28
**/
@RestController
public class DeptConsumerController {
/**
* 消费者 : 不应该有service层
* RestTemplate 有很多方法给我们直接调用 ! 它的类中没有Bean所以要我们自己把它注册到Bean中
* public ResponseEntity getForEntity(String url, Class responseType, Map uriVariables)
*/
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://localhost:8001";
@RequestMapping("/consumer/dept/add")
@ResponseBody
public boolean addDept(Dept dept){
//远程只能用 post 方式请求,那么这里也只能通过 post 方式获取
return Boolean.TRUE.equals(restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class));
}
@RequestMapping("/consumer/dept/get/{id}")
@ResponseBody
public Dept queryDept(@PathVariable("id") Long id){
//service不在本项目中,所以要去远程项目获取
//远程只能用 get 方式请求,那么这里也只能通过 get 方式获取
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
}
@RequestMapping("/consumer/dept/list")
@ResponseBody
public List queryAllDept(){
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
}
}
3.ConfigBean配置代码:ConfigBean.java
package com.sun.cloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @program: SpringCloud
* @description: 配置类
* @author: Sunl
* @create: 2022-05-14 21:32
**/
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
4.导入相关依赖
SpringCloud
com.sunl
1.0-SNAPSHOT
4.0.0
cloud-consumer-dept-80
8
8
com.sunl
cloud-api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
5.配置提供者文件
server:
port: 80
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)