springcloud 实现简单的 消费者和生产者 模式(Restfule 的风格)
一、springcloud 实现简单的 消费者和生产者 模式(Restfule 的风格) 1.实现简单的消费者和生产者 springcloud使用的http协议进行传输数据,也就是说springcloud仍然使用的是RESTFUL的风格 1.1.搭建工程(!!!springboot的项目是否需要运行都打jar包!!!(springboot工程中不需要war包)) 搭建一个总的父级工程 20190926-springcloud-parent(pom):只做jar包的管理 把SRC 目录干掉 // 问题一、pom是啥? <packaging>pom</packaging> POM是最简单的打包类型。它生成的构件只是它本身。 pom 项目里没有java代码,也不执行任何代码,只是为了聚合工程或传递依赖用的。 // 问题二、jar和war和pom的区别 ? /* pom是maven依赖文件 jar是java普通项目打包 war是java web项目打包 */ 在总的父级项目(20190926-springcloud-parent)下有新建三个子级项目。 把这三个项目的 SRC 目录干掉,因为他们都是父级项目。不须要。 20190926-springcloud-management 20190926-springcloud-provider-management 20190926-springcloud-consumer-management
二、 1.搭建常规工程 1.1 20190926-springcloud-management(pom):只做jar包的管理 --20190926-springcloud-model(jar) --20190926-springcloud-mapper(jar)--->依赖于model --20190926-springcloud-service(jar)--->依赖于mapper @Service:是spring框架所提供的 1.2搭建服务生产者工程 依赖于service 20190926-springcloud-provider-management(pom):只做jar包的管理 20190926-springcloud-provider-8081(jar):需要提供数据, 也就是说必须要连接数据库,对数据库做增删改查(springboot-web-starter,mysql驱动包,mybatis包,druid包...)--->依赖于service 1.3搭建服务消费者工程 consumer 消费者 依赖于model 20190926-springcloud-consumer-management(pom):只做jar包的管理 20190926-springcloud-consumer-6081(jar):只需要和客户端打交道(只需要页面), 不再需要数据的支持,所有的数据来源都来源于provider--->依赖于model
三、jar包管理,如何添加jar包?
1.最高级的jar包 20190926-springcloud-parent
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.aaa</groupId> <artifactId>20190927-springcloud-parent</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>202190927-springcloud-management</module> <module>20190927-springcloud-provider-management</module> <module>20190927-springcloud-consumer-management</module> </modules> <packaging>pom</packaging> <!-- 项目jar包的构建: 之前使用的parent标签直接引入项目中,可以继承父项目的jar包 但是项目中已经使用到了springcloud和springboot springcloud和springboot需要些两个parent,但是在pom工程中不能这样实现 所以直接引入springboot和springcloud的jar包工程,并且使用dependencyManagement标签进行管理,用到了什么jar包直接重写 springboot-parent, springcloud-parent的父级工程都是dependencies --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.8.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR3</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.11.8</version> <exclusions> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.11.8</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> </dependencies> </dependencyManagement> </project>
2. 20190926-springcloud-consumer-management 消费者的父级架构。
<?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>20190927-springcloud-parent</artifactId> <groupId>com.aaa</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>20190927-springcloud-consumer-management</artifactId> <packaging>pom</packaging> <modules> <module>20190927-springcloud-consumer-6081</module> </modules> </project>
2.1 子级架构 20190926-springcloud-consumer-6081?---------依赖于model
<?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>20190927-springcloud-consumer-management</artifactId> <groupId>com.aaa</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>20190927-springcloud-consumer-6081</artifactId> <!-- 消费者 依赖于 model 和页面交互 --> <dependencies> <dependency> <groupId>com.aaa</groupId> <artifactId>20190927-springcloud-model</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 微服务,将 spring-boot-starter-web添加进来 需要修改默认的端口号。 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
3. 20190926-springcloud-provider-management 生产者
<?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>20190927-springcloud-parent</artifactId> <groupId>com.aaa</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>20190927-springcloud-provider-management</artifactId> <packaging>pom</packaging> <modules> <module>20190927-springcloud-provider-8081</module> </modules> </project>
3.1 子级架构 20190926-springcloud-provider-8081?---------依赖于service
<?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>20190927-springcloud-provider-management</artifactId> <groupId>com.aaa</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>20190927-springcloud-provider-8081</artifactId> <dependencies> <dependency> <groupId>com.aaa</groupId> <artifactId>20190927-springcloud-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
4. 20190926-springcloud-management(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>20190927-springcloud-parent</artifactId> <groupId>com.aaa</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>202190927-springcloud-management</artifactId> <!-- pom 做jar包的管理。 --> <packaging>pom</packaging> <modules> <module>20190927-springcloud-model</module> <module>20190927-springcloud-mapper</module> <module>20190927-springcloud-service</module> </modules> </project>
4.1 mapper 项目的jar包管理。------------依赖于 model。
<?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>202190927-springcloud-management</artifactId> <groupId>com.aaa</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>20190927-springcloud-mapper</artifactId> <dependencies> <dependency> <groupId>com.aaa</groupId> <artifactId>20190927-springcloud-model</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 需要引入mysql mybatis springboot druid --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</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-starter-web</artifactId> </dependency> </dependencies> </project>
4.2 service项目?-------------依赖于 mapper
<?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>202190927-springcloud-management</artifactId> <groupId>com.aaa</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>20190927-springcloud-service</artifactId> <dependencies> <dependency> <groupId>com.aaa</groupId> <artifactId>20190927-springcloud-mapper</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
4.3 model?
<?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>202190927-springcloud-management</artifactId> <groupId>com.aaa</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>20190927-springcloud-model</artifactId> </project>
四、创建config 包,添加配置文件?
4.1 消费者consumer 的配置?
4.2 生产者provider 的配置?
五、实现Restfule的风格,消费者的controller 调用生产者的controller
5.1 model
package com.aaa.zxf.model; import java.io.Serializable; public class User implements Serializable { private long id; private String name; private long age; public User() { } public User(long id, String name, long age) { this.id = id; this.name = name; this.age = age; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getAge() { return age; } public void setAge(long age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != user.id) return false; if (age != user.age) return false; return name != null ? name.equals(user.name) : user.name == null; } @Override public int hashCode() { int result = (int) (id ^ (id >>> 32)); result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (int) (age ^ (age >>> 32)); return result; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
5.2 mapper
package com.aaa.zxf.mapper; import com.aaa.zxf.model.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface UserMapper { // 利用注解的方式 ,查询数据库 @Select("select id,name,age from user") List<User> selectAll(); }
5.3 service
package com.aaa.zxf.service; import com.aaa.zxf.mapper.UserMapper; import com.aaa.zxf.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> selectAllUsers() { return userMapper.selectAll(); } }
5.4 provider的controller
package com.aaa.zxf.controller; import com.aaa.zxf.model.User; import com.aaa.zxf.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { // 注入依赖 @Autowired private UserService userService; /** * 获得所有的用户信息。 * @return */ @RequestMapping("/userAll") public List<User> selectAllUsers() { System.out.println("8081"); return userService.selectAllUsers(); } }
Restful的具体的实现,consumer 的controller。
package com.aaa.zxf.controller; import com.aaa.zxf.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class UserController { @Autowired private RestTemplate restTemplate; /** *调用 service ,consumer项目的controller 调用的是 provider项目的controller (遵循 http 协议) * * 模拟http协议,就可以实现,两个controller之间的调用。 * * getForObject: 使用get 请求方式, 该方法有两个参数。 * * 1. URL 请求的路径 localhost:8081/userAll * 2. 规定的返回值类型 *
注意! 将provider 和 consumer 两者之间的 RequestMapping 的路径 ,保持一致!!! * @return */ @RequestMapping("/userAll") public List<User> selectAllUsers() { return restTemplate.getForObject("http://localhost:8081/userAll",List.class); } }
六,consumer的config配置?