| 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 的版本更新。
导入相关依赖
三、创建新的提供者module 1.导入相关依赖4.0.0 com.sunl SpringCloud1.0-SNAPSHOT pom UTF-8 1.8 1.8 4.13.2 1.18.22 1.2.17 org.springframework.cloud spring-cloud-dependenciesHoxton.SR12 pom import org.springframework.boot spring-boot-dependencies2.3.2.RELEASE pom import mysql mysql-connector-java8.0.28 com.alibaba druid1.2.8 org.mybatis.spring.boot mybatis-spring-boot-starter2.2.2 ch.qos.logback logback-core1.2.3 junit junit${junit.version} log4j log4j${log4j.version} org.projectlombok lombok${lombok.version}
2.配置提供者文件SpringCloud com.sunl 1.0-SNAPSHOT 4.0.0 cloud-provider-dept-80018 8 com.sunl cloud-api1.0-SNAPSHOT junit junittest mysql mysql-connector-javacom.alibaba druidch.qos.logback logback-coreorg.mybatis.spring.boot mybatis-spring-boot-starterorg.springframework.boot spring-boot-testorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-jettyorg.springframework.boot spring-boot-devtools
server:
port: 8001
mybatis:
type-aliases-package: com.sunl.cloud.entity
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper
@Mapper
@Repository
public interface DeptMapper {
public boolean addDept(Dept dept);
public Dept queryDept(Long id);
public List getAllDept();
}
5.编写业务层代码:DeptService.java、DeptServiceImpl.javainsert into dept(dept_name,db_source) values (#{deptName},DATABASE()); select dept_no deptNo, dept_name deptName, db_source dbSource from dept where dept_no = #{deptNo};
package com.sunl.cloud.service;
import com.sunl.cloud.entity.Dept;
import java.util.List;
public interface DeptService {
public boolean addDept(Dept dept);
public Dept queryDept(Long id);
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;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public boolean addDept(Dept dept) {
return deptMapper.addDept(dept);
}
@Override
public Dept queryDept(Long id) {
return deptMapper.queryDept(id);
}
@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;
@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;
@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;
@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;
@RestController
public class DeptConsumerController {
@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;
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
4.导入相关依赖
5.配置提供者文件SpringCloud com.sunl 1.0-SNAPSHOT 4.0.0 cloud-consumer-dept-808 8 com.sunl cloud-api1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-devtools
server: port: 80



