• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Spring Cloud入门系列服务提供者总结

java 搞代码 4年前 (2022-01-05) 38次浏览 已收录 0个评论

这篇文章主要介绍了Spring Cloud入门系列之服务提供者总结,服务提供者使用Eureka Client组件创建 ,创建完成以后修改某文件,具体操作方法及实例代码跟随小编一起看看吧

服务提供者使用Eureka Client组件创建
首先在父项目中创建Eureka Client Server项目(Maven)
创建完成以后,修改pom.xml文件

    com.wenqi.springcloudorg.example1.0-SNAPSHOT4.0.0EurekaClient  org.springframework.cloudspring-cloud-starter-netflix-eureka-client2.0.2.RELEASE

编写application.yml文件

 server: port: 8010 # 当前服务注册在eureka server上的名称 spring: application: name: provider # 当前注册中心的访问地址 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ # 是否将当前服务的ip注册到Eureka Server instance: prefer-ip-address: true

创建启动类

 @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class,args); } }

启动启动类,这时候访问http://localhost:8761/ 可以看到服务注册进来了。
接下来进行增删改查的简单业务服务操作

创建实体类

 @Data @AllArgsConstructor @NoArgsConstructor public class Student { private long id; private String name; private int age; }

创建repository

 public interface StudentRepository { public Collection findAll(); public Student findById(long id); public void saveOrUpdate(Student student); public void deleteById(long id); }

创建repository实现类

 @Repository public class StudentRepositoryimpl implements StudentRepository { private static Map studentMap; static { studentMap = new HashMap(); studentMap.put(1L,new Student(1L,"张三",22)); studentMap.put(2L,new Student(2L,"王五",20)); studentMap.put(3L,new Student(3L,"李四",21)); } public Collection findAll() { return studentMap.values(); } public Student findById(long id) { return studentMap.get(id); } public void saveOrUpdate(Student student) { studentMap.put(student.getId(),student); } public void deleteById(long id) { studentMap.remove(id); } }

实现增删改查操作 controller

 @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentRepository studentRepository; @GetMapping("/findAll") public Collection findAll(){ return studentRepository.findAll(); } @GetMapping("/findById/{id}") public Student findById(@PathVariable("id") long id){ return studentRep<strong style="color:transparent">来源gaodaima#com搞(代@码网</strong>ository.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("/delete/{id}") public void deleteById(@PathVariable("id") long id){ studentRepository.deleteById(id); } }

启动Eureka Client,并启动Eureka Server。
使用postman工具进行测试

postman的使用自己下去进行了练习,不得不说测试还是很有必要的。

以上就是Spring Cloud入门服务提供者总结的详细内容,更多关于Spring Cloud服务提供者的资料请关注gaodaima搞代码网其它相关文章!

以上就是Spring Cloud入门系列服务提供者总结的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring Cloud入门系列服务提供者总结

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址