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

本文涉及的产品
服务治理 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完成,但它提供给注册中心后,其他模块能相互调用。




目录
相关文章
|
6天前
|
负载均衡 Java API
构建高效微服务架构:API网关与服务熔断策略
【5月更文挑战第2天】 在微服务架构中,确保系统的高可用性与灵活性是至关重要的。本文将深入探讨如何通过实施有效的API网关和设计合理的服务熔断机制来提升分布式系统的鲁棒性。我们将分析API网关的核心职责,包括请求路由、负载均衡、认证授权以及限流控制,并讨论如何利用熔断器模式防止故障传播,维护系统的整体稳定性。文章还将介绍一些实用的技术和工具,如Netflix Zuul、Spring Cloud Gateway以及Hystrix,以帮助开发者构建一个可靠且高效的微服务环境。
|
9天前
|
Java Docker 微服务
|
10天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
27 1
|
10天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
14 2
|
13天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
2月前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
49 0
|
3月前
|
缓存 Java Maven
Spring Boot自动配置原理
Spring Boot自动配置原理
50 0
|
2月前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
134 0
|
1月前
|
存储 JSON Java
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
39 2
|
2月前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革