springcloud学习4——消费者

从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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值