当前位置: 首页 > 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.lryc.cn/news/449070.html

相关文章:

  • 配置Scrapy项目
  • 航顺芯片HK32MCU受邀出席汽车芯片国产化与技术创新闭门研讨会
  • 【深度学习】(6)--图像数据增强
  • Vscode 远程切换Python虚拟环境
  • Sqoop面试整理
  • PyCharm 的安装和配置
  • 【工具类:FastJsonRedisSerializer】
  • Spring Cloud Alibaba-(6)Spring Cloud Gateway【网关】
  • 芯科科技2024年Works With开发者大会登陆上海,物联网和人工智能的变革性融合带来无限精彩
  • 华为OD机试 - 匿名信(Python/JS/C/C++ 2024 E卷 100分)
  • Python习题 208:将二维列表数组转置
  • STM32F407HAL库输出互补PWM波以及死区时间计算
  • matlab-对比两张图片的RGB分量的差值并形成直方图
  • SpringBoot集成Matlab软件实战
  • Java---异常及处理
  • 【开源免费】基于SpringBoot+Vue.JS网上购物商城(JAVA毕业设计)
  • 添加vscode插件C/C++ snippets,快速生成LVGL .c/.h文件模版
  • ee trade:如何辨别足金真假
  • GCC使用入门
  • CSS3 字体
  • LeetCode题练习与总结:为运算表达式设计优先级--241
  • 金融科技革命:API接口开放平台,畅通金融服务之路
  • Java8后新特性介绍
  • Arthas monitor(方法执行监控)
  • 语言的副作用
  • centos磁盘逻辑卷LVM创建
  • BUUCTF蜘蛛侠呀
  • 大数据新视界 --大数据大厂之基于 MapReduce 的大数据并行计算实践
  • win自带录屏怎么用?让视频制作更简单!
  • 修改Kali Linux的镜像网站