Spring Cloud笔记(14) Spring Cloud Eureka Client 注册微服务

本文紧接上一篇 Spring Cloud笔记(13) 服务治理组件 Eureka ,Eureka Server 是一个注册中心,用来治理管理微服务的,但是想要注册微服务,还是得通过 Eureka Client 。不同业务需求下的所有微服务统一使用 Eureka Client 组件进行注册,这个不同业务的微服务包括服务提供者和服务消费者 。所谓的提供者和消费者,本质都是微服务,这只是一个我们用来业务区分的叫法而已。所以不管是提供者还是消费者,都是通过 Eureka Client 连接到 Eureka Server 完成注册。

注册微服务

1、父工程下创建 Module ,这里我将它当作服务提供者

2、在该 module 下添加 Eureka Client 依赖。

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

3、在 resources 路径下创建配置文件 application.yml,添加 Eureka Client 相关配置。

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

4、创建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

5、依次启动 注册中心也就是 Eureka Server ,接着是服务提供者 Student Provider。

然后访问注册中心监控中心,可以看到,刚才的服务已经注册进来

提供服务

因为我将这个微服务确定为一个 服务器提供者,所以这里我需要写上一些增删改查的业务,供给其他服务调用。

这里举例写一个对 Student 的增删改查。先依赖Lombok,因为其他服务也会用到这个依赖,所以我将它放到父 pom.xml 中,这样所有子 module 都能生效。

1、创建 Student 实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private long id;
    private String name;
    private char gender;
}

2、这里不查数据库,直接创建有关它的服务方法

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

3、创建它的实现类

@Repository
public class StudentServiceImpl implements StudentService {

    private Map<Long,Student> studentMap;

    public StudentServiceImpl(){
        studentMap = new HashMap<>();
        studentMap.put(1L,new Student(1L,"张三",'男'));
        studentMap.put(2L,new Student(2L,"李四",'女'));
        studentMap.put(3L,new Student(3L,"王五",'男'));
    }

    @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);
    }
}

4、创建 Controller

@RequestMapping("/student")
@RestController
public class StudentController {

    @Autowired
    private StudentServiceImpl 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);
    }
}

在重启该服务提供者,用 postman 试下

其他接口应该也没问题,我都不截图显示了。

现在说明该服务业务接口都准备好,等待其他服务来调用,这应该是下篇文章的内容了。本文大部分内容来自 Gitchat ,我按照自己的理解和风格改写成如上。

以上代码笔记内容来自付费专栏:案例上手 Spring 全家桶

PS:如果侵犯版权,请联系我。

本文由老郭种树原创,转载请注明:https://guozh.net/spring-cloud-eureka-client/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注