当前位置: 首页 > 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自行测试可以 

 

相关文章:

  • Linux云计算 |【第四阶段】RDBMS1-DAY2
  • Python 在区块链智能合约开发中的应用与实践
  • 数字化转型中的信息技术:驱动力与挑战
  • 芝法酱学习笔记(0.5)——使用jenkins做自动打包
  • 【 Java 】工具类 —— Collections 与 Arrays 的实用操作全解析
  • Llama3:全模型GQA与tiktoken分词的新突破
  • BFS之最短路径模型
  • 解决银河麒麟V10中/data目录执行权限问题
  • JDK1.8安装配置教程(图文结合,最简洁易懂)
  • undeclared identifier ‘UNITY_PREV_MATRIX_M‘ - Unity Shader自己写URP,引用内部 hlsl
  • 15年408计算机网络
  • FPGA-Vivado-IP核-逻辑分析仪(ILA)
  • 机器学习和深度学习的区别
  • 多模态——基于XrayGLM的X光片诊断的多模态大模型
  • MYSQL(学习笔记)
  • 分享的文章《人生如棋》
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • in typeof instanceof ===这些运算符有什么作用
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • PHP 7 修改了什么呢 -- 2
  • React 快速上手 - 07 前端路由 react-router
  • spark本地环境的搭建到运行第一个spark程序
  • Terraform入门 - 1. 安装Terraform
  • Webpack 4 学习01(基础配置)
  • 爱情 北京女病人
  • 基于web的全景—— Pannellum小试
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 使用agvtool更改app version/build
  • 跳前端坑前,先看看这个!!
  • 突破自己的技术思维
  • 找一份好的前端工作,起点很重要
  • Java性能优化之JVM GC(垃圾回收机制)
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • #C++ 智能指针 std::unique_ptr 、std::shared_ptr 和 std::weak_ptr
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • (23)Linux的软硬连接
  • (MATLAB)第五章-矩阵运算
  • (PADS学习)第二章:原理图绘制 第一部分
  • (补)B+树一些思想
  • (二)fiber的基本认识
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (源码分析)springsecurity认证授权
  • (转)setTimeout 和 setInterval 的区别
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • .net 4.0发布后不能正常显示图片问题