从b站学习springcloud,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒
b站链接:https://www.bilibili.com/video/av55304977
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru
上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103326409
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103332091
下面的内容总结:子工程→pom→application→entity的Student→handler→启动类
本节和上一节实现几乎一样,不同点:类的名字不同,加进了注册中心(即加了application)
实现细节:
1.创建module模块,命名为consumer
2.进入该模块pom加入代码:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
3.在resourse中创建application.yml
加入代码:
server:
port: 8020
spring:
application:
name: consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true #ip地址进行注册
4.在java包中创包com.southwind,在southwind中创建启动类ConsumerApplication.java
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.southwind创包entity,吧resttemplate中的entity/Student复制过来
6.在southwind中创包controller,在controller中创ConsumerHandler.java
填入代码:
package com.southwind.controller;
import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collection;
@RestController
@RequestMapping("/consumer")
public class ConsumerHandler {
@Autowired
private RestTemplate restTemplate;
/*增 改*/
@PostMapping("/save")
public void save(@RequestBody Student student){
restTemplate.postForEntity("http://localhost:8010/student/save",student,null);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
restTemplate.put("http://localhost:8010/student/update",student);
}
/*删*/
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id")long id){
restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
}
/*查*/
@GetMapping("/findAll")
public Collection<Student> findAll(){
return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
}
/*查*/
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id")long id){
return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);
}
}
7.测试,进入postman
a.全查:
get→http://localhost:8020/consumer/findAll
b.查询
get→ http://localhost:8020/consumer/findById/2
c.存数据
post→ http://localhost:8020/consumer/save
检查
get→http://localhost:8020/consumer/findAll
d.更新数据
put→ http://localhost:8020/consumer/update
检查
get→http://localhost:8020/consumer/findAll
e.删除数据
delete→ http://localhost:8020/consumer/deleteById/4
检查:
get→http://localhost:8020/consumer/findAll
说明成功创建消费者!
上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103326409
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103332091