Spring Cloud (一) 关于 Eureka 的学习(Eureka 服务提供者的搭建)

上篇内容讲解了如何搭建 Eureka 的注册中心https://blog.csdn.net/assiduous_me/article/details/97233073

本篇内容是讲解如何搭建 Eureka 服务提供者,也就是提供 REST api 的一个基于

Spring Boot 的一个 web项目,好的,废话不多说,现在开始!

1. 创建 spring-provider 项目

点击 next ?

填写 ? 的信息,然后点击 next ?

 

选中 ? 的对应可选项,然后点击 next ,再点击 Finish 完成项目的创建 ?

2. 环境配置

将 application.properties 通过 alt + shift + r 改写为 application.yml

扫描二维码关注公众号,回复: 8519286 查看本文章

填写 ? 的内容在 application.yml 中

server:
  port: 8010
spring:
  application:
    name: provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

3. 代码编写

  • 实体类的编写 

在 mr.s.provider 包下新建 entity 包,并创建 Student 类

package mr.s.provider.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;
}

  • dao 接口编写

在 mr.s.provider 下创建 repository 包,并在该包下面创建 StudentRepository 接口,编写内容 ?

package mr.s.provider.repository;

import mr.s.provider.entity.Student;

import java.util.Collection;

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

 

  • 编写 StudentRepository 接口实现 类

在 mr.s.provider.repository 下创建 impl 包,并在该包下面创建 StudentRepositoryImpl 实现类,编写内容 ?

package mr.s.provider.repository.impl;

import lombok.Data;
import mr.s.provider.entity.Student;
import mr.s.provider.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, "李四", 23));
        studentMap.put(3L, new Student(3L, "王五", 24));
    }

    @Override
    public Collection<Student> findAll() {
        return studentMap.values();
    }

    @Override
    public Student findById(long id) {
        return studentMap.get(id);
    }

    @Override
    public void saveOrUpdate(Student student) {
        studentMap.put(student.getId(), student);
    }

    @Override
    public void deleteById(long id) {
        studentMap.remove(id);
    }
}

  • 编写 StudentHandler

在 mr.s.provider 下创建 controller 包,并在该包下面创建 StudentHandler 类,编写内容 ?

package mr.s.provider.controller;

import mr.s.provider.entity.Student;
import mr.s.provider.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 {
    @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("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id) {
        studentRepository.deleteById(id);
    }
}

到现在提供者的 REST api 已经编写好了,现在先启动 Eureka 的注册中心,再启用

本提供者项目

现在两个项目皆已启动成功,先先访问 http:localhost:8761/ 

从 ? 可以看到,该提供者已经成功在服务中心进行了注册,现在访问一下提供者的 api 接口

浏览器输入 http://localhost:8010/student/findAll

可以拿到正确的返回信息,说明已经成功搭建好了!

为了不想大篇幅,消费者的项目将在另一篇博客中给出!

https://blog.csdn.net/assiduous_me/article/details/97256542

发布了92 篇原创文章 · 获赞 23 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/assiduous_me/article/details/97234996
今日推荐