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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
注册配置 MSE Nacos/ZooKeeper,118元/月
云原生网关 MSE Higress,422元/月
简介: 微服务学习笔记三 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完成,但它提供给注册中心后,其他模块能相互调用。




目录
相关文章
|
22天前
|
Cloud Native Java Nacos
微服务时代的新宠儿!Spring Cloud Nacos实战指南,带你玩转服务发现与配置管理,拥抱云原生潮流!
【8月更文挑战第29天】Spring Cloud Nacos作为微服务架构中的新兴之星,凭借其轻量、高效的特点,迅速成为服务发现、配置管理和治理的首选方案。Nacos(命名和配置服务)由阿里巴巴开源,为云原生应用提供了动态服务发现及配置管理等功能,简化了服务间的调用与依赖管理。本文将指导你通过五个步骤在Spring Boot项目中集成Nacos,实现服务注册、发现及配置动态管理,从而轻松搭建出高效的微服务环境。
93 0
|
7天前
|
消息中间件 存储 Java
SpringCloud基础9——服务异步通信-高级篇
消息可靠性、死信交换机、惰性队列、MQ集群
SpringCloud基础9——服务异步通信-高级篇
|
21天前
|
小程序 前端开发 Java
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
JavaDog Chat v1.0.0 是一款基于 SpringBoot、MybatisPlus 和 uniapp 的简易聊天软件,兼容 H5、小程序和 APP,提供丰富的注释和简洁代码,适合初学者。主要功能包括登录注册、消息发送、好友管理及群组交流。
45 0
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
|
7天前
|
监控 Java Nacos
SpringCloud基础5——微服务保护、Sentinel
sentinel、雪崩问题、流量控制、隔离和降级、授权规则、规则持久化
SpringCloud基础5——微服务保护、Sentinel
|
12天前
|
前端开发 Java UED
"揭秘!如何以戏剧性姿态,利用SpringCloud铸就无懈可击的异常处理铁壁,让你的微服务架构稳如泰山,震撼业界!"
【9月更文挑战第8天】随着微服务架构的普及,Spring Cloud作为一套完整的微服务解决方案被广泛应用。在微服务架构中,服务间调用频繁且复杂,异常处理成为保障系统稳定性和用户体验的关键。传统的异常处理方式导致代码冗余,降低系统可维护性和一致性。因此,基于Spring Cloud封装统一的异常处理机制至关重要。这样不仅可以减少代码冗余、提升一致性,还增强了系统的可维护性,并通过统一的错误响应格式优化了用户体验。具体实现包括定义全局异常处理器、自定义业务异常以及在服务中抛出这些异常。这种方式体现了微服务架构中的“服务治理”和“契约先行”原则,有助于构建健壮、可扩展的系统。
31 2
|
20天前
|
Java 微服务 Spring
驾驭复杂性:Spring Cloud在微服务构建中的决胜法则
【8月更文挑战第31天】Spring Cloud是在Spring Framework基础上打造的微服务解决方案,提供服务发现、配置管理、消息路由等功能,适用于构建复杂的微服务架构。本文介绍如何利用Spring Cloud搭建微服务,包括Eureka服务发现、Config Server配置管理和Zuul API网关等组件的配置与使用。通过Spring Cloud,可实现快速开发、自动化配置,并提升系统的伸缩性和容错性,尽管仍需面对分布式事务等挑战,但其强大的社区支持有助于解决问题。
31 0
|
23天前
|
Kubernetes Cloud Native Docker
云原生之旅:从容器到微服务的架构演变
【8月更文挑战第29天】在数字化时代的浪潮下,云原生技术以其灵活性、可扩展性和弹性管理成为企业数字化转型的关键。本文将通过浅显易懂的语言和生动的比喻,带领读者了解云原生的基本概念,探索容器化技术的奥秘,并深入微服务架构的世界。我们将一起见证代码如何转化为现实中的服务,实现快速迭代和高效部署。无论你是初学者还是有经验的开发者,这篇文章都会为你打开一扇通往云原生世界的大门。
|
23天前
|
负载均衡 应用服务中间件 持续交付
微服务架构下的Web服务器部署
【8月更文第28天】随着互联网应用的不断发展,传统的单体应用架构逐渐显露出其局限性,特别是在可扩展性和维护性方面。为了解决这些问题,微服务架构应运而生。微服务架构通过将应用程序分解成一系列小型、独立的服务来提高系统的灵活性和可维护性。本文将探讨如何在微服务架构中有效部署和管理Web服务器实例,并提供一些实际的代码示例。
56 0
|
7天前
|
运维 Cloud Native Devops
云原生架构的崛起与实践云原生架构是一种通过容器化、微服务和DevOps等技术手段,帮助应用系统实现敏捷部署、弹性扩展和高效运维的技术理念。本文将探讨云原生的概念、核心技术以及其在企业中的应用实践,揭示云原生如何成为现代软件开发和运营的主流方式。##
云原生架构是现代IT领域的一场革命,它依托于容器化、微服务和DevOps等核心技术,旨在解决传统架构在应对复杂业务需求时的不足。通过采用云原生方法,企业可以实现敏捷部署、弹性扩展和高效运维,从而大幅提升开发效率和系统可靠性。本文详细阐述了云原生的核心概念、主要技术和实际应用案例,并探讨了企业在实施云原生过程中的挑战与解决方案。无论是正在转型的传统企业,还是寻求创新的互联网企业,云原生都提供了一条实现高效能、高灵活性和高可靠性的技术路径。 ##
18 3
|
15天前
|
存储 Java Maven
从零到微服务专家:用Micronaut框架轻松构建未来架构
【9月更文挑战第5天】在现代软件开发中,微服务架构因提升应用的可伸缩性和灵活性而广受欢迎。Micronaut 是一个轻量级的 Java 框架,适合构建微服务。本文介绍如何从零开始使用 Micronaut 搭建微服务架构,包括设置开发环境、创建 Maven 项目并添加 Micronaut 依赖,编写主类启动应用,以及添加控制器处理 HTTP 请求。通过示例代码展示如何实现简单的 “Hello, World!” 功能,并介绍如何通过添加更多依赖来扩展应用功能,如数据访问、验证和安全性等。Micronaut 的强大和灵活性使你能够快速构建复杂的微服务系统。
39 5