服务消费者:服务的调用方,即依赖其他服务的服务。
1.创建一个maven项目
在项目pom.xml里添加统一依赖,并把打包方式改为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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springcloud</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>springcloud</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath /> </parent> <properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> <mysql.version>8.0.19</mysql.version> <druid.version>1.1.10</druid.version> <mybatis.plus.version>3.1.0</mybatis.plus.version> <mybatis.plus.generator.version>3.2.0</mybatis.plus.generator.version> <velocity.engine.version>2.2</velocity.engine.version> <lombok.version>1.18.12</lombok.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>springcloud-dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis.plus.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.在项目下创建数据库操作的module,即存放实体类和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>springcloud</artifactId> <groupId>com.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springcloud-dao</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-dao</name> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--该服务使用了mybatis-plus来简化代码--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
创建实体类
package com.example.springcloud.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.extension.activerecord.Model; import com.baomidou.mybatisplus.annotation.TableId; import java.io.Serializable; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class User extends Model<User> { private static final long serialVersionUID=1L; /** * 主键 */ @TableId(value = "id", type = IdType.AUTO) private Long id; /** * 姓名 */ private String name; /** * 数据库名 */ private String dbName; @Override protected Serializable pkVal() { return this.id; } }
<?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.example.springcloud.mapper.UserMapper"> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="com.example.springcloud.domain.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="db_name" property="dbName" /> </resultMap> <!-- 通用查询结果列 --> <sql id="Base_Column_List"> id, name, db_name </sql> </mapper>
package com.example.springcloud.mapper; import com.example.springcloud.domain.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; public interface UserMapper extends BaseMapper<User> { }
3.在项目下创建服务提供者
添加依赖
<?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>springcloud</artifactId> <groupId>com.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springcloud-provider-8888</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-provider-8888</name> <dependencies> <!--引入springcloud-dao服务,就可以调用该服务下的类--> <dependency> <groupId>com.example</groupId> <artifactId>springcloud-dao</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> </dependencies> </project>
添加配置
server: port: 8888 spring: application: name: springcloud-provider #注册中心中的服务名 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root password: "123456" type: com.alibaba.druid.pool.DruidDataSource
@Service public class UserServiceImpl implements UserService { //省略代码。。。。。。。。。。。。。 }
@RestController @RequestMapping("/user") public class IndexController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUserById(@PathVariable("id")Long id){ User user = userService.getUserById(id); return user; } @GetMapping("/") public List<User> getAllUser(){ return userService.getAllUser(); } //因为消费者传输的对象都是JSON类型,所以需加@RequestBody注解,否则会报错 @PostMapping("/add") public int addUser(@RequestBody User user){ return userService.addUser(user); } }
@SpringBootApplication @MapperScan("com.example.springcloud.mapper") public class Provider_8888 { public static void main(String[] args) { SpringApplication.run(Provider_8888.class,args); } }
springcloud中由服务消费者调用服务提供者一共有两种方法rest和feign
<?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>springcloud</artifactId> <groupId>com.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-consumer</artifactId> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>springcloud-dao</artifactId> </dependency> </dependencies> </project>
添加配置
server:
port: 80
创建RestTemplate的注入配置类,方便调用
@Configuration public class BeanConfig { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
创建controller调用服务提供者
package com.example.springcloud.controller; import com.example.springcloud.domain.User; 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.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController @RequestMapping("/consumer") public class IndexController { c @Autowired private RestTemplate restTemplate;c // http://ip地址:调用服务的端口/[服务提供者controller类上的RequestMapping/] //如果服务提供者controller类上不加RequestMapping,[]里的路径可去掉 private static final String URL = "http://localhost:8888/user/"; @RequestMapping("/user/{id}") public User getUserById(@PathVariable("id")Integer id) { return restTemplate.getForObject(URL+id, User.class); } @RequestMapping("/user/list") public List<User> getAllUser(){ return restTemplate.getForObject(URL, List.class); } @RequestMapping("/user/add") public int addUser(User dept){ return restTemplate.postForObject(URL+"add", dept, Integer.class); } }
创建启动类
//停止SpringBootApplication数据连接池自动配置 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class Consumer { public static void main(String[] args) { SpringApplication.run(Consumer.class, args); } }
访问http://localhost/consumer/user/1 ,服务消费者则会通过 http://localhost:8888/user/1 获取返回值User
通过Feign调用
创建feign接口,对应服务提供者者controller的方法
@Component @FeignClient(value = "SPRINGCLOUD-PROVIDER")//对应服务名,服务提供者者的spring.application.name @RequestMapping("/user") public interface UserFeignClient { @GetMapping("/{id}") User getUserById(@PathVariable("id") Integer id); @GetMapping("/") List<User> getAllUser(); @PostMapping("/add") int addUser(@RequestBody User user); }
通过feign调用服务提供者
package com.example.springcloud.controller; import com.example.consumer.service.HystrixService; import com.example.springcloud.domain.User; import com.example.springcloud.feign.UserFeignClient; 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.RestController; import java.util.List; @RestController @RequestMapping("/consumer") public class IndexController { @Autowired private UserFeignClient userFeignClient; @RequestMapping("/user/{id}") public User getDeptById(@PathVariable("id")Integer id){ return userFeignClient.getUserById(id); } @RequestMapping("/user/list") public List<User> getAllDept(){ return userFeignClient.getAllUser(); } @RequestMapping("/user/add") public int addDept(User user){ return userFeignClient.addUser(user); } }
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableFeignClients(basePackages = {"com.example.springcloud"})//对应feign接口所在的包 public class FeignConsumer { public static void main(String[] args) { SpringApplication.run(FeignConsumer.class, args); } }