iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring Cloud入门系列服务提供者总结
  • 435
分享到

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

2024-04-02 19:04:59 435人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

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

服务提供者使用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服务提供者的资料请关注编程网其它相关文章!

--结束END--

本文标题: Spring Cloud入门系列服务提供者总结

本文链接: https://www.lsjlt.com/news/128325.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作