新建一个服务器提供者module子模块,类似前面建的common公共模块,名称是 microservice-student-consumer-1001
server:
port: 80
context-path: /
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方式更加灵活方便点;
package com.java1234.controller;
import java.util.List;
import javax.annotation.Resource;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.java1234.entity.Student;
/**
*
* @author Administrator
*
*/
@RestController
@RequestMapping("/student")
public class StudentConsumerController {
@Resource
private RestTemplate restTemplate;
/**
* 添加或者修改学生信息
* @param student
* @return
*/
@PostMapping(value="/save")
private boolean save(Student student){
return restTemplate.postForObject("http://localhost:1001/student/save", student, Boolean.class);
}
/**
* 查询学生信息
* @return
*/
@SuppressWarnings("unchecked")
@GetMapping(value="/list")
public List
return restTemplate.getForObject("http://localhost:1001/student/list", List.class);
}
/**
* 根据id查询学生信息
* @return
*/
@GetMapping(value="/get/{id}")
public Student get(@PathVariable("id") Integer id){
return restTemplate.getForObject("http://localhost:1001/student/get/"+id, Student.class);
}
/**
* 根据id删除学生信息
* @return
*/
@GetMapping(value="/delete/{id}")
public boolean delete(@PathVariable("id") Integer id){
try{
restTemplate.getForObject("http://localhost:1001/student/delete/"+id, Boolean.class);
return true;
}catch(Exception e){
return false;
}
}
}
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);
}
}
这里的话 加了特殊配置 排除了 数据源注入,不加的话 会报错,老版本没有这个问题;
我们测试下:
http://localhost/student/list
http://localhost/student/get/1
服务提供端实体接收不到数据,因为是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;
}
}