<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<modules>
<module>demo01module>
<module>demo02module>
modules>
<groupId>com.jsxsgroupId>
<artifactId>SpringCloundTestartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>SpringCloundTestname>
<description>SpringCloud-parentdescription>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.12junit.version>
<log4j.version>1.2.17log4j.version>
<lombok.version>1.16.18lombok.version>
properties>
<packaging>pompackaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR8version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.3.3.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
dependency>
dependencies>
dependencyManagement>
project>
这里的API相当于是我们实体类,实体类全部放在这!!! 实体类不需要启动类和测试类,所以我们需要把这个实体类的启动类给手动删除。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.jsxsgroupId>
<artifactId>SpringCloundTestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.jsxsgroupId>
<artifactId>demo01artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo01name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package com.jsxs.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @Author Jsxs
* @Date 2023/10/15 19:08
* @PackageName:com.jsxs.pojo
* @ClassName: Dept
* @Description: TODO
* @Version 1.0
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true) //目的是为了打开链式写法,
public class Dept implements Serializable { // 实体类在网络通信的时候,一定要进行序列化,否则会报错 ORM关系
private Long deptno; // 主键
private String dname; //部门的名字
// 这里的数据存在哪个数据库里面。微服务: 一个服务对应一个数据库,同一个信息可能存在不同的数据库中。
private String db_source;
}
这里是我们的一个业务:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.jsxsgroupId>
<artifactId>SpringCloundTestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.jsxsgroupId>
<artifactId>demo02artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo02name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.jsxsgroupId>
<artifactId>demo01artifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-testartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jettyartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
server:
port: 8081
# Mybatis的配置
mybatis:
type-aliases-package: com.jsxs.pojo # 起别名
mapper-locations: classpath:mybatis/mapper/*.xml # 路径配置
# Spring的配置
spring:
application:
name: SpringCloud-provider-dept
datasource:
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
username: root
password: 121788
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
com/jsxs/dao/DeptDao.java
package com.jsxs.dao;
import com.jsxs.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 19:29
* @PackageName:com.jsxs.dao
* @ClassName: DeptDao
* @Description: TODO
* @Version 1.0
*/
@Mapper
@Repository
public interface DeptDao {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List<Dept> all();
}
mybatis/mapper/DeptMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jsxs.dao.DeptDao">
<insert id="addDept" parameterType="com.jsxs.pojo.Dept">
insert into dept(deptno,dname,db_source)
values(#{deptno},#{dname},DATABASE())
insert>
<select id="queryById" resultType="com.jsxs.pojo.Dept" parameterType="Long">
select *from dept where deptno=#{id}
select>
<select id="all" resultType="com.jsxs.pojo.Dept">
select *from dept
select>
mapper>
com/jsxs/service/DeptService.java
package com.jsxs.service;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 19:56
* @PackageName:com.jsxs.dao.service
* @ClassName: DeptService
* @Description: TODO
* @Version 1.0
*/
public interface DeptService {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List<Dept> all();
}
com/jsxs/service/DeptServiceImpl.java
package com.jsxs.service;
import com.jsxs.dao.DeptDao;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 19:57
* @PackageName:com.jsxs.dao.service
* @ClassName: DeptServiceImpl
* @Description: TODO
* @Version 1.0
*/
@Service
public class DeptServiceImpl implements DeptService {
@Resource
private DeptDao deptDao;
@Override
public boolean addDept(Dept dept) {
return deptDao.addDept(dept);
}
@Override
public Dept queryById(Long id) {
return deptDao.queryById(id);
}
@Override
public List<Dept> all() {
return deptDao.all();
}
}
com/jsxs/controller/DeptController.java
package com.jsxs.controller;
import com.jsxs.pojo.Dept;
import com.jsxs.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 19:59
* @PackageName:com.jsxs.controller
* @ClassName: DeptController
* @Description: TODO
* @Version 1.0
*/
@RestController
public class DeptController {
@Resource
private DeptService deptService;
@PostMapping("/dept/add")
public Boolean addDept(Dept dept) {
boolean flag = deptService.addDept(dept);
return flag;
}
@GetMapping("/dept/queryById/{id}")
public Dept queryById(@PathVariable("id") Long id) {
Dept dept = deptService.queryById(id);
return dept;
}
@GetMapping("/dept/all")
public List<Dept> all() {
List<Dept> all = deptService.all();
return all;
}
}
http://localhost:8081/dept/queryById/1
http://localhost:8081/dept/add?deptno=1&dname=李明&db_source=db01
用户使用业务A
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.jsxsgroupId>
<artifactId>SpringCloundTestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.jsxsgroupId>
<artifactId>demo03artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo03name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.jsxsgroupId>
<artifactId>demo01artifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
server:
port: 80
com/jsxs/config/ConfigBean.java
提供多种便捷访问远程 访问http服务的方法,简单的Rest
package com.jsxs.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Author Jsxs
* @Date 2023/10/15 21:16
* @PackageName:com.jsxs.config
* @ClassName: ConfigBean
* @Description: TODO
* @Version 1.0
*/
@Configuration
public class ConfigBean {
// 把RestTemplate注入到Spring中去
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
因为我们的用户层,只需要控制层即可。不需要业务逻辑层什么的。
package com.jsxs.controller;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 21:21
* @PackageName:com.jsxs.controller
* @ClassName: DeptConsumerController
* @Description: TODO
* @Version 1.0
*/
@RestController
public class DeptConsumerController {
@Resource
// (URI url, 实体 map,Class responseType) 地址---- 实体 ----返回类型.class
private RestTemplate restTemplate; // 提供多种便捷访问远程 访问http服务的方法,简单的Rest
// 设置服务层的前缀为常量
private static final String REST_URL_PREFIX = "http://localhost:8081";
// 根据id进行数据的查找
// 用户层的这个访问路径可以随意写,没有必要和服务层的路径完全一致
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long deptno) {
// 这里返回的是: 服务层的路径
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/queryById/" + deptno, Dept.class);
}
// 添加数据
@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
}
// 查找全部数据
@RequestMapping("/consumer/dept/all")
public List<Dept> all() {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/all", List.class);
}
}
==启动
以上的微服务框架并未使用我们的注册中心,不能说是真正的微服务框架的!!!
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.jsxsgroupId>
<artifactId>SpringCloundTestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.jsxsgroupId>
<artifactId>eureka-7001artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>eureka-7001name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
<version>1.4.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
server:
port: 7001
eureka:
instance:
prefer-ip-address: true
hostname: localhost
client:
register-with-eureka: false # 是否将自己注册进注册中心
fetch-registry: false # 是否是注册中心: 是
service-url:
defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka/
package com.jsxs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// ⭐⭐ 开启我们的Eureka服务端
@EnableEurekaServer
public class Eureka7001Application {
public static void main(String[] args) {
SpringApplication.run(Eureka7001Application.class, args);
}
}
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.jsxsgroupId>
<artifactId>SpringCloundTestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.jsxsgroupId>
<artifactId>eureka-provider-8001artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>eureka-provider-8001name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
<version>1.4.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
server:
port: 8001
# Spring的配置
spring:
application:
name: SpringCloud-provider-dept
# Eureka 服务注册到哪里?
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: jsxs-01
package com.jsxs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaProvider8001Application {
public static void main(String[] args) {
SpringApplication.run(EurekaProvider8001Application.class, args);
}
}
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.jsxsgroupId>
<artifactId>SpringCloundTestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.jsxsgroupId>
<artifactId>demo02artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo02name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.jsxsgroupId>
<artifactId>demo01artifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
<version>1.4.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-testartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jettyartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<scope>testscope>
dependency>
dependencies>
project>
server:
port: 8081
# Mybatis的配置
mybatis:
type-aliases-package: com.jsxs.pojo # 起别名
mapper-locations: classpath:mybatis/mapper/*.xml # 路径配置
# Spring的配置
spring:
application:
name: SpringCloud-provider-dept
datasource:
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
username: root
password: 121788
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ #注册到这个注册中心
instance:
instance-id: jsxs-dept-eureka-8081 # 服务者的id为这个
info:
app.name: jsxs-springcloud
company.name: pdd
com/jsxs/dao/DeptDao.java
package com.jsxs.dao;
import com.jsxs.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 19:29
* @PackageName:com.jsxs.dao
* @ClassName: DeptDao
* @Description: TODO
* @Version 1.0
*/
@Mapper
@Repository
public interface DeptDao {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List<Dept> all();
}
mybatis/mapper/DeptMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jsxs.dao.DeptDao">
<insert id="addDept" parameterType="com.jsxs.pojo.Dept">
insert into dept(deptno,dname,db_source)
values(#{deptno},#{dname},DATABASE())
insert>
<select id="queryById" resultType="com.jsxs.pojo.Dept" parameterType="Long">
select *from dept where deptno=#{id}
select>
<select id="all" resultType="com.jsxs.pojo.Dept">
select *from dept
select>
mapper>
com/jsxs/service/DeptService.java
package com.jsxs.service;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 19:56
* @PackageName:com.jsxs.dao.service
* @ClassName: DeptService
* @Description: TODO
* @Version 1.0
*/
public interface DeptService {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List<Dept> all();
}
com/jsxs/service/DeptServiceImpl.java
package com.jsxs.service;
import com.jsxs.dao.DeptDao;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 19:57
* @PackageName:com.jsxs.dao.service
* @ClassName: DeptServiceImpl
* @Description: TODO
* @Version 1.0
*/
@Service
public class DeptServiceImpl implements DeptService {
@Resource
private DeptDao deptDao;
@Override
public boolean addDept(Dept dept) {
return deptDao.addDept(dept);
}
@Override
public Dept queryById(Long id) {
return deptDao.queryById(id);
}
@Override
public List<Dept> all() {
return deptDao.all();
}
}
com/jsxs/controller/DeptController.java
package com.jsxs.controller;
import com.jsxs.pojo.Dept;
import com.jsxs.service.DeptService;
import org.springframework.stereotype.Controller;
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 javax.annotation.Resource;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 19:59
* @PackageName:com.jsxs.controller
* @ClassName: DeptController
* @Description: TODO
* @Version 1.0
*/
@RestController
public class DeptController {
@Resource
private DeptService deptService;
@PostMapping("/dept/add")
public Boolean addDept(Dept dept) {
boolean flag = deptService.addDept(dept);
return flag;
}
@GetMapping("/dept/queryById/{id}")
public Dept queryById(@PathVariable("id") Long id) {
Dept dept = deptService.queryById(id);
return dept;
}
@GetMapping("/dept/all")
public List<Dept> all() {
List<Dept> all = deptService.all();
return all;
}
}
com/jsxs/Demo02Application.java
package com.jsxs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class Demo02Application {
public static void main(String[] args) {
SpringApplication.run(Demo02Application.class, args);
}
}
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.jsxsgroupId>
<artifactId>SpringCloundTestartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.jsxsgroupId>
<artifactId>demo03artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo03name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.jsxsgroupId>
<artifactId>demo01artifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
<version>1.4.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
server:
port: 80
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ #像那一个注册中心注册自己
instance:
instance-id: jsxs-dept-eureka-80 # 编号
spring:
application:
name: SpringCloud-counsumer-dept
com/jsxs/config/ConfigBean.java
package com.jsxs.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Author Jsxs
* @Date 2023/10/15 21:16
* @PackageName:com.jsxs.config
* @ClassName: ConfigBean
* @Description: TODO
* @Version 1.0
*/
@Configuration
public class ConfigBean {
// 把RestTemplate注入到Spring中去
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
com/jsxs/controller/DeptConsumerController.java
package com.jsxs.controller;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author Jsxs
* @Date 2023/10/15 21:21
* @PackageName:com.jsxs.controller
* @ClassName: DeptConsumerController
* @Description: TODO
* @Version 1.0
*/
@RestController
public class DeptConsumerController {
@Resource
// (URI url, 实体 map,Class responseType) 地址---- 实体 ----返回类型.class
private RestTemplate restTemplate; // 提供多种便捷访问远程 访问http服务的方法,简单的Rest
// 设置服务层的前缀为常量
private static final String REST_URL_PREFIX = "http://localhost:8081";
// 根据id进行数据的查找
// 用户层的这个访问路径可以随意写,没有必要和服务层的路径完全一致
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long deptno) {
// 这里返回的是: 服务层的路径
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/queryById/" + deptno, Dept.class);
}
// 添加数据
@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
}
// 查找全部数据
@RequestMapping("/consumer/dept/all")
public List<Dept> all() {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/all", List.class);
}
}
package com.jsxs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class Demo03Application {
public static void main(String[] args) {
SpringApplication.run(Demo03Application.class, args);
}
}