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

 更新时间:2021年06月16日 14:43:54   作者:全栈阿文  
这篇文章主要介绍了Spring Cloud入门系列之服务提供者总结,服务提供者使用Eureka Client组件创建 ,创建完成以后修改某文件,具体操作方法及实例代码跟随小编一起看看吧

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>com.wenqi.springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>EurekaClient</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

编写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<Student> 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<Long,Student> studentMap;
    static {
        studentMap = new HashMap<Long, Student>();
        studentMap.put(1L,new Student(1L,"张三",22));
        studentMap.put(2L,new Student(2L,"王五",20));
        studentMap.put(3L,new Student(3L,"李四",21));
    }


    public Collection<Student> 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<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("/delete/{id}")
    public void deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
    }
}

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

在这里插入图片描述
在这里插入图片描述

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

以上就是Spring Cloud入门服务提供者总结的详细内容,更多关于Spring Cloud服务提供者的资料请关注脚本之家其它相关文章!

相关文章

  • Java数组(Array)最全汇总(上篇)

    Java数组(Array)最全汇总(上篇)

    这篇文章主要介绍了Java数组(Array)最全汇总(上篇),本文章内容详细,通过案例可以更好的理解数组的相关知识,本模块分为了三部分,本次为上篇,需要的朋友可以参考下
    2023-01-01
  • java中常用的阻塞队列与非阻塞队列详解

    java中常用的阻塞队列与非阻塞队列详解

    这篇文章主要介绍了java中常用的阻塞队列与非阻塞队列用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • java批量导入Excel数据超详细实例

    java批量导入Excel数据超详细实例

    这篇文章主要给大家介绍了关于java批量导入Excel数据的相关资料,EXCEL导入就是文件导入,操作代码是一样的,文中给出了详细的代码示例,需要的朋友可以参考下
    2023-08-08
  • Swing拆分窗格控件JSplitPane使用详解

    Swing拆分窗格控件JSplitPane使用详解

    这篇文章主要为大家详细介绍了Swing拆分窗格控件JSplitPane的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • 使用Postman自动生成Cookie并转换为Java代码的实现

    使用Postman自动生成Cookie并转换为Java代码的实现

    在接口测试中,有时候需要在请求中携带Cookie信息,为了方便测试,我们可以使用Postman来自动生成Cookie,并将其转换为Java代码,以便在自动化测试中使用,下面将介绍如何实现这一功能,需要的朋友可以参考下
    2024-11-11
  • SpringBoot开启异步调用方法

    SpringBoot开启异步调用方法

    这篇文章主要为大家详细介绍了SpringBoot开启异步调用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • 浅谈JavaIO之try with底层原理

    浅谈JavaIO之try with底层原理

    众所周知,所有被打开的系统资源,比如流、文件或者Socket连接等,都需要被开发者手动关闭,否则随着程序的不断运行,资源泄露将会累积成重大的生产事故。本文将介绍JavaIO之try with底层原理。
    2021-06-06
  • java -jar指定端口或配置文件启动jar问题

    java -jar指定端口或配置文件启动jar问题

    这篇文章主要介绍了java -jar指定端口或配置文件启动jar问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Spring实现定时任务的几种方式总结

    Spring实现定时任务的几种方式总结

    Spring Task 是 Spring 框架提供的一种任务调度和异步处理的解决方案,可以按照约定的时间自动执行某个代码逻辑它可以帮助开发者在 Spring 应用中轻松地实现定时任务、异步任务等功能,提高应用的效率和可维护性,需要的朋友可以参考下本文
    2024-07-07
  • Java基础教程之String深度分析

    Java基础教程之String深度分析

    这篇文章主要给大家介绍了关于Java基础教程之String的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06

最新评论