微服务学习笔记三 Spring Cloud Eureka Client 服务提供者

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
云原生网关 MSE Higress,422元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 微服务学习笔记三 Spring Cloud Eureka Client 服务提供者

Eureka Client 代码实现

创建Module,pom.xml


```yaml

<dependencies>

   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```


创建配置文件 application.yml,添加Eureka Client相关配置


```yaml

server:

 port: 8010

spring:

 application:

   name: provider

eureka:

 client:

   service-url:

     defaultZone: http://localhost:8761/eureka/

 instance:

   prefer.ip-address: true

```


属性说明:

spring.application.name:当前服务注册在Eureka Server上的名称。

eureka.client.service-url.defaultZone:注册中心的访问地址。

eureka.instance.prefer.ip.address:是否将当前服务的ip注册到Eureka Server。


创建启动类 ProviderApplication


```java

package com.shuang;


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

   }

}

```


创建实体  Student


```java

package com.shuang.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;

}

```



创建学生接口


```java

package com.shuang.repository;


import com.shuang.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);

}

```


创建实现类


```java

package com.shuang.repository.impl;


import com.shuang.entity.Student;

import com.shuang.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 {

   public 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


```java

package com.shuang.controller;



import com.shuang.entity.Student;

import com.shuang.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);

   }

}

```


服务提供者可以单独使用springboot完成,但它提供给注册中心后,其他模块能相互调用。




目录
相关文章
|
2天前
|
消息中间件 Java API
解密微服务架构:如何在Java中实现高效的服务通信
微服务架构作为一种现代软件开发模式,通过将应用拆分成多个独立的服务,提升了系统的灵活性和扩展性。然而,实现微服务之间的高效通信仍然是许多开发者面临的挑战。本文将探讨在Java环境中实现微服务架构时,如何使用不同的通信机制来优化服务之间的交互,包括同步和异步通信的方法,以及相关的最佳实践。
|
1天前
|
Java 微服务 Spring
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
文章介绍了如何利用Spring Cloud Alibaba快速构建大型电商系统的分布式微服务,包括服务限流降级等主要功能的实现,并通过注解和配置简化了Spring Cloud应用的接入和搭建过程。
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
|
4天前
|
负载均衡 Java Nacos
EureKa详解:微服务发现与注册的利器
EureKa详解:微服务发现与注册的利器
|
4天前
|
存储 缓存 Java
Eureka原理与实践:深入探索微服务架构的核心组件
在微服务架构日益盛行的今天,服务之间的注册与发现成为了保证系统高可用性和灵活性的关键。Eureka,作为Netflix开源的服务注册与发现框架,凭借其简单、健壮的特性,在微服务领域占据了举足轻重的地位。本文将深入剖析Eureka的原理,并通过实践案例展示其在实际项目中的应用,以期为开发者提供一个高端、深入的视角。
12 0
|
5天前
|
Kubernetes Nacos 微服务
【技术难题破解】Nacos v2.2.3 + K8s 微服务注册:强制删除 Pod 却不消失?!7步排查法+实战代码,手把手教你解决Nacos Pod僵死问题,让服务瞬间满血复活!
【8月更文挑战第15天】Nacos作为微服务注册与配置中心受到欢迎,但有时会遇到“v2.2.3 k8s 微服务注册nacos强制删除 pod不消失”的问题。本文介绍此现象及其解决方法,帮助开发者确保服务稳定运行。首先需检查Pod状态与事件、配置文件及Nacos配置,确认无误后可调整Pod生命周期管理,并检查Kubernetes版本兼容性。若问题持续,考虑使用Finalizers、审查Nacos日志或借助Kubernetes诊断工具。必要时,可尝试手动强制删除Pod。通过系统排查,通常能有效解决此问题。
11 0
|
12天前
|
API Go 数据安全/隐私保护
go-zero微服务框架的静态文件服务
【8月更文挑战第7天】`go-zero` 微服务框架支持多种静态文件服务实现方式。常用方法是利用 `Go` 标准库 `http.FileServer`。通过设置静态文件根目录并使用 `http.StripPrefix` 去除路径前缀,能确保 `/static/` 开头的请求正确返回文件。此外,结合 `go-zero` 的路由机制可更灵活地控制静态文件服务,例如仅在特定 API 路径 `/api/static` 下提供服务,从而实现精细化访问控制。
|
13天前
|
监控 供应链 安全
构建高效微服务架构:API网关与服务熔断策略
【7月更文挑战第38天】随着现代应用程序向微服务架构的转型,系统的稳定性和效率成为了开发团队关注的焦点。本文将探讨在微服务环境中实现系统可靠性的关键组件——API网关,以及如何在服务间通讯时采用熔断机制来防止故障蔓延。通过分析API网关的核心功能和设计原则,并结合熔断策略的最佳实践,我们旨在提供一套提高分布式系统弹性的策略。
|
3天前
|
监控 负载均衡 API
从单体到微服务:架构转型之道
【8月更文挑战第17天】从单体架构到微服务架构的转型是一项复杂而系统的工程,需要综合考虑技术、团队、文化等多个方面的因素。通过合理的规划和实施策略,可以克服转型过程中的挑战,实现系统架构的升级和优化。微服务架构以其高度的模块化、可扩展性和灵活性,为业务的持续发展和创新提供了坚实的技术保障。
|
13天前
|
缓存 监控 API
【微服务战场上的神秘守门人】:揭秘API网关的超能力 —— 探索微服务架构中的终极守护者与它的神奇魔法!
【8月更文挑战第7天】随着微服务架构的流行,企业应用被拆分为围绕特定业务功能构建的小型服务。API网关作为微服务间的通信管理核心,对请求进行路由、认证、限流等处理,简化客户端集成并提升用户体验。以电商应用为例,通过Kong部署API网关,配置产品目录等服务的API及JWT认证插件,确保安全高效的数据交互。这种方式不仅增强了系统的可维护性和扩展性,还提供了额外的安全保障。
31 2

热门文章

最新文章