当前位置: 首页 > news >正文

SpringCloud-EurekaClient

创建Module pom.xml

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
spring:application:name: provider # 应用程序的名称,通常用于服务注册server:port: 8010 # 服务器监听的端口号eureka:client:service-url:defaultZone: http://localhost:8761/eureka/ # Eureka 服务器的地址,用于注册服务instance:prefer-ip-address: true # 是否将当前 IP 地址进行注册到EurekaService

创建代码

package com.cloud.eurekaclient.controller;import com.cloud.eurekaclient.entity.Student;
import com.cloud.eurekaclient.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.Collection;@RestController
@RequestMapping("/student")
public class StudentHandler {@Autowiredprivate StudentRepository studentRepository;@GetMapping("/findAll")public Collection<Student> findAll(){return studentRepository.findAll();}@GetMapping("/findById/{id}")public Student findById(@PathVariable("id") long id){return studentRepository.findById(id);}@PostMapping("/save")public void save(@RequestBody Student student){studentRepository.saveOrUpdate(student);}@PutMapping("/update")public void update(@RequestBody Student student){studentRepository.saveOrUpdate(student);}@DeleteMapping("/deleteByI/{id}")public void deleteById(@PathVariable("id") long id){studentRepository.deleteById(id);}
}
package com.cloud.eurekaclient.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {private long id;private String name;private int age;
}

 

package com.cloud.eurekaclient.repository.impl;import com.cloud.eurekaclient.entity.Student;
import com.cloud.eurekaclient.repository.StudentRepository;
import org.springframework.stereotype.Repository;import java.util.Collection;
import java.util.HashMap;
import java.util.Map;@Repository
public class StudentRepositoryImpl implements StudentRepository {private static Map<Long,Student> studentMap;static{studentMap = new HashMap<>();studentMap.put(1L,new Student(1L,"张三",22));studentMap.put(2L,new Student(2L,"李四",32));studentMap.put(3L,new Student(3L,"王五",41));studentMap.put(4L,new Student(4L,"徐6",53));}@Overridepublic Collection<Student> findAll() {return studentMap.values();}@Overridepublic Student findById(long id) {return studentMap.get(id);}@Overridepublic void saveOrUpdate(Student student) {studentMap.put(student.getId(),student);}@Overridepublic void deleteById(long id) {studentMap.remove(id);}
}

apipost自行测试可以 

 


http://www.mrgr.cn/news/38274.html

相关文章:

  • 继承实现单例模式的探索(一)
  • 探索基因奥秘:汇智生物如何利用组蛋白甲基化修饰测序技术革新农业植物基因组研究?
  • 反距离加权插值(IDW)讲解与MATLAB代码
  • 多模态——基于XrayGLM的X光片诊断的多模态大模型
  • 深度学习实战TT100K中国交通标志检测【数据集+YOLOv5模型+源码+PyQt5界面】
  • Go语言切片复习记录
  • 一次眼睛受损然后恢复的过程
  • 机械键盘驱动调光DIY--【DAREU】
  • 不知道孩子用的台灯哪个牌子好?家长买灯看护眼台灯十大排名!
  • BAAI 团队发布多模态模型 Emu3
  • 如何选择主数据管理系统平台
  • PCL uniform_sampling均匀采样抽稀
  • 【React】react项目中的redux使用
  • 基于SpringBoot+Vue的高校实习管理系统
  • 【机器学习】ID3、C4.5、CART 算法
  • Linux oracle数据库静默安装
  • 宝塔frp配置
  • 知识付费APP开发指南:基于在线教育系统源码的技术详解
  • 从零开始手写STL库:Unordered_Map
  • SpringBoot教程(安装篇) | Docker Desktop的安装(Windows下的Docker环境)