我们新建一个服务器提供者module子模块,类似前面建的common公共模块,名称是 microservice-student-consumer-1001
pom.xml修改:
<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>
<parent>
<groupId>com.java1234.springcloud</groupId>
<artifactId>microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservice-student-consumer-80</artifactId>
<dependencies>
<dependency>
<groupId>com.java1234.springcloud</groupId>
<artifactId>microservice-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
application.yml配置文件:
server:
port: 80
context-path: /
SpringCloudConfig配置类:
package com.java1234.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* SpringCloud相关配置
* @author Administrator
*
*/
@Configuration
public class SpringCloudConfig {
/**
* 调用服务模版
* @return
*/
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
主要是定义一个bean RestTemplate对象; springcloud消费者,服务提供者之间的交互是http rest方式,比dubbo rpc方式更加灵活方便点;
StudentConsumerController类:
package com.java1234.controller;
import com.java1234.entity.Student;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
/**
* 服务消费者-学生信息控制器
* @author Administrator
*
*/
@RestController
@RequestMapping("/student")
public class StudentConsumerController {
@Resource
private RestTemplate restTemplate;
private final String PRE_HOST = "http://localhost:1001";
/**
* 添加或者修改学生信息
* @param student
* @return
*/
@PostMapping(value="/save")
public boolean save(Student student){
return restTemplate.postForObject(PRE_HOST+"/student/save",student,Boolean.class);
}
/**
* 查询学生信息
* @return
*/
@GetMapping(value="/list")
public List<Student> list(){
return restTemplate.getForObject(PRE_HOST+"/student/list",List.class);
}
/**
* 根据id查询学生信息
* @return
*/
@GetMapping(value="/get/{id}")
public Student get(@PathVariable("id") Integer id){
return restTemplate.getForObject(PRE_HOST+"/student/get/"+id,Student.class);
}
/**
* 根据id删除学生信息
* @return
*/
@GetMapping(value="/delete/{id}")
public boolean delete(@PathVariable("id") Integer id){
return restTemplate.getForObject(PRE_HOST+"/student/get/"+id,Boolean.class);
}
}
再搞一个启动类StudentConsumerApplication_80:
package com.java1234;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class StudentConsumerApplication_80 {
public static void main(String[] args) {
SpringApplication.run(StudentConsumerApplication_80.class,args);
}
}
这里的话 加了特殊配置 排除了 数据源注入,不加的话 会报错,老版本没有这个问题;
但是这里有个主意点,用postman测试的时候
服务提供端实体接收不到数据,因为是http rest方式交互,这里要求必须加上 @RequestBody
/**
* 添加或者修改学生信息
* @param student
* @return
*/
@PostMapping(value="/save")
public boolean save(@RequestBody Student student){
try{
studentService.save(student);
return true;
}catch(Exception e){
return false;
}
}
这样就OK 具体看视频操作演示;