一、创建一个子工程并引入配置如下:

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

二、创建application.yml,添加相关配置如下:

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。

三、创建启动类,代码如下

package com.frr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(EurekaClientApplication.class, args);
} }

四、启动注册中心,然后再启动刚刚写的服务提供者,访问注册中心的地址可看到以下界面,多出了刚刚注册成功的provider服务

五、实战!模拟学生的增删改查

1.创建student实体类如下:

package com.frr.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data //生成Getter,Setter,equals,canEqual,hasCode,toString等方法
@AllArgsConstructor //添加一个构造函数,该构造函数含有所有已声明字段属性参数
@NoArgsConstructor //创建一个无参构造函数
public class Student {
private long id;
private String name;
private int age;
}

注解说明

*@Data 生成Getter,Setter,equals,canEqual,hasCode,toString等方法

*@AllArgsConstructor 添加一个构造函数,该构造函数含有所有已声明字段属性参数

*@NoArgsConstructor 创建一个无参构造函数

2.创建接口,代码如下:

package com.frr.repository;

import java.util.Collection;

import com.frr.entity.Student;

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

3.创建实现类,采用静态代码块实现数据初始化,代码如下:

package com.frr.repository.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.frr.entity.Student;
import com.frr.repository.StudentRepository; @Repository
public class StudentRepositoryImpl implements StudentRepository{ private static Map<Long,Student> studentMap; static {
studentMap = new HashMap<>();
studentMap.put(1L, new Student(1L, "张三", 11));
studentMap.put(2L, new Student(1L, "李四", 11));
studentMap.put(3L, new Student(1L, "王五", 11));
} @Override
public Collection<Student> findAll() {
return studentMap.values();
} @Override
public Student findById(long id) {
return studentMap.get(id);
} @Override
public void saveOrUpdate(Student s) {
studentMap.put(s.getId(), s);
} @Override
public void deleteById(long id) {
studentMap.remove(id);
} }

4.创建controller,代码如下:

package com.frr.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.frr.entity.Student;
import com.frr.repository.StudentRepository; @RestController //在Spring中@RestController的作用等同于@Controller + @ResponseBody。
@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 s) {
studentRepository.saveOrUpdate(s);
} @PutMapping("/Update")
public void Update(@RequestBody Student s) {
studentRepository.saveOrUpdate(s);
} @DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") long id) {
studentRepository.deleteById(id);
}
}

5.重新启动上文创建的启动类

6.使用Postman工具测试各个方法,数据一致表示成功!

Spring Cloud03: Eureka Client 服务提供者的更多相关文章

  1. spring cloud 2.x版本 Eureka Client服务提供者教程

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1 创建eureka client 1.1 新建Srping boot工程:eureka-c ...

  2. spring cloud Eureka client配置(consumer通过Eureka发起对provider的调用)

    参考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html springboot版本:2.0.3 ...

  3. 2.spring cloud eureka client配置

    红色加粗内容表示修改部分 1.把server项目打成jar包并启动 在项目根目录cmd执行  mvn clean package -Dmaven.test.skip=true mavne仓库地址建议 ...

  4. spring boot eureka client

    eureka client @EnableDiscoveryClient @SpringBootApplication public class DemoApplication { public st ...

  5. 【转载】一起来学Spring Cloud | Eureka Client注册到Eureka Server的秘密

    LZ看到这篇文章感觉写得比较详细,理解以后,便转载到自己博客中,留作以后回顾学习用,喝水不忘挖井人,内容来自于李刚的博客:http://www.spring4all.com/article/180 一 ...

  6. Spring Cloud Eureka(三):认识Eureka Server 与 Eureka Client

    Spring Cloud Netflix 是什么 This project provides Netflix OSS integrations for Spring Boot apps through ...

  7. SpringCloud2.0 Eureka Client 服务注册 基础教程(三)

    1.创建[服务提供者],即 Eureka Client 1.1.新建 Spring Boot 工程,工程名称:springcloud-eureka-client 1.2.工程 pom.xml 文件添加 ...

  8. Spring Cloud Eureka 3 (Eureka client注册服务提供者)

    在完成服务注册中心的搭建后我们来尝试下将一个既有的spring boot应用加入eureka的服务治理体系中 新建一个spring boot项目加入eureka client依赖 这里加入的eurek ...

  9. Spring cloud Eureka 服务治理(注册服务提供者)

    搭建完成服务注册中心,下一步可以创建服务提供者并向注册中心注册服务. 接下来我们创建Spring Boot 应用将其加入Eureka服务治理体系中去. 直接使用签名章节创建hello服务项目改造: 1 ...

随机推荐

  1. vue中v-if与v-show的区别以及使用场景

    区别 1.手段:v-if是通过控制dom节点的存在与否来控制元素的显隐:v-show是通过设置DOM元素的display样式,block为显示,none为隐藏: 2.编译过程:v-if切换有一个局部编 ...

  2. python常识系列07-->python利用xlwt写入excel文件

    前言 读书之法,在循序而渐进,熟读而精思.--朱熹 抽空又来写一篇,毕竟知识在于分享! 一.xlwt模块是什么 python第三方工具包,用于往excel中写入数据:(ps:只能创建新表格,不能修改表 ...

  3. js--吐血总结最近遇到的变态表单校验---element+原生+jq+easyUI(前端职业生涯见过的最烦的校验)

    最近写了无数各种形式的表单,记录下奇奇怪怪的校验规则~ 一:首先是element自带的rules校验规则: element作为常用框架,自带rules属性简单易懂,官方文档一目了然,不再赘述,应付常用 ...

  4. .NET之WebAPI

    介绍 通过一个简单的项目,总结一下常用的几种WebApi编写方式以及请求方式. 本文示例代码环境:vs2019.net5.MySQL 正文前准备 新创建了一个.Net5 WebAPI程序,安装组件 & ...

  5. Redis 存储对象信息是用 Hash 还是 String

    Redis 内部使用一个 RedisObject 对象来表示所有的 key 和 value,RedisObject 中的 type,则是代表一个 value 对象具体是何种数据类型,它包含字符串(St ...

  6. UVa OJ 455 Periodic Strings

     Periodic Strings  A character string is said to have period k if it can be formed by concatenating ...

  7. SE_Work0_回顾与展望

    项目 内容 课程:北航-2020-春-软件工程 博客园班级博客 要求:阅读推荐博客并回答问题 热身作业阅读部分要求 我在这个课程的目标是 提升团队管理及合作能力,开发一项满意的工程项目 这个作业在哪个 ...

  8. 动态内存:delete作用于空指针

    在学习<C++primer 第五版>(中文版)中第12章动态内存与智能指针的时候遇到了一个习题,练习12.13: 练习 12.13:如果执行下面的代码,会发生什么? auto sp=mak ...

  9. slickgrid ( nsunleo-slickgrid ) 8 区域选择与复制粘贴

    区域选择 区域选择是通过插件CellRangeSelector实现的,默认不支持跨冻结列进行选择,修正了选择,支持跨冻结列,代码如下,通过判断选择的起点和终点所落在的冻结范围进行计算,如从左往右进行复 ...

  10. [Java] 数据分析 -- 大数据

    单词计数 需求:输入小说文本,输出每个单词出现的次数 实现:分map.combine.reduce三个阶段实现 1 /* Data Analysis with Java 2 * John R. Hub ...