Spring Cloud03: Eureka Client 服务提供者
一、创建一个子工程并引入配置如下:
<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 服务提供者的更多相关文章
- spring cloud 2.x版本 Eureka Client服务提供者教程
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1 创建eureka client 1.1 新建Srping boot工程:eureka-c ...
- spring cloud Eureka client配置(consumer通过Eureka发起对provider的调用)
参考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html springboot版本:2.0.3 ...
- 2.spring cloud eureka client配置
红色加粗内容表示修改部分 1.把server项目打成jar包并启动 在项目根目录cmd执行 mvn clean package -Dmaven.test.skip=true mavne仓库地址建议 ...
- spring boot eureka client
eureka client @EnableDiscoveryClient @SpringBootApplication public class DemoApplication { public st ...
- 【转载】一起来学Spring Cloud | Eureka Client注册到Eureka Server的秘密
LZ看到这篇文章感觉写得比较详细,理解以后,便转载到自己博客中,留作以后回顾学习用,喝水不忘挖井人,内容来自于李刚的博客:http://www.spring4all.com/article/180 一 ...
- Spring Cloud Eureka(三):认识Eureka Server 与 Eureka Client
Spring Cloud Netflix 是什么 This project provides Netflix OSS integrations for Spring Boot apps through ...
- SpringCloud2.0 Eureka Client 服务注册 基础教程(三)
1.创建[服务提供者],即 Eureka Client 1.1.新建 Spring Boot 工程,工程名称:springcloud-eureka-client 1.2.工程 pom.xml 文件添加 ...
- Spring Cloud Eureka 3 (Eureka client注册服务提供者)
在完成服务注册中心的搭建后我们来尝试下将一个既有的spring boot应用加入eureka的服务治理体系中 新建一个spring boot项目加入eureka client依赖 这里加入的eurek ...
- Spring cloud Eureka 服务治理(注册服务提供者)
搭建完成服务注册中心,下一步可以创建服务提供者并向注册中心注册服务. 接下来我们创建Spring Boot 应用将其加入Eureka服务治理体系中去. 直接使用签名章节创建hello服务项目改造: 1 ...
随机推荐
- poj1190深搜 生日蛋糕
题意: 让你制作一个蛋糕,这个蛋糕有m层,而且每层都是圆柱形,并且每一层都必须满足 ri>ri+1 && hi > hi+1,然后给出蛋糕的总体积是n*PI,还有 ...
- 堆栈上的舞蹈之释放重引用(UAF) 漏洞原理实验分析
0x01 前言 释放重引用的英文名名称是 Use After Free,也就是著名的 UAF 漏洞的全称.从字面意思可以看出 After Free 就是释放后的内存空间,Use 就是使用的意思,使用释 ...
- Windows核心编程 第十四章 虚拟内存
第1 4章 虚 拟 内 存 <这一章没啥,是说的几个内存相关的函数 > 14.1 系统信息 许多操作系统的值是根据主机而定的,比如页面的大小,分配粒度的大小等.这些值决不应该用硬编码的形式 ...
- Day009 类和对象的创建
类和对象的关系 类是一种抽象的数据结构,它是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物 动物.植物.手机.电脑 Person类.Pet类.Car类等,这些都是用来描述/定义某一类具体的 ...
- Linux yum 报错:One of the configured repositories failed (Unknown), and yum doesn't have.
1. 请先确定你是无法联网还是配置问题. ping www.baidu.com 如果是正常ping那可以看这个帖子完成配置 https://blog.csdn.net/weicuidi/articl ...
- python三元(三目)运算
传统的if,else写法 三元运算 name="alex" if 1==1 else "SB"
- 使用C#进行数据库增删改查ADO.NET(一)
这节讲一下如何使用C#进行数据库的增删改查操作,本节以SQL Server数据库为例. .NET 平台,使用ADO.NET 作为与数据库服务器的桥梁,我们通过ADO.NET就可以使用C#语言操作数据库 ...
- JAVA8 lambda表达式权威教程!
Java 8新特性----Stream流 jdk8是Java 语言开发的一个主要版本,它支持函数式编程,新的 JavaScript 引擎,新的日期 API,新的Stream API 等等.今天就重点介 ...
- Java_常用类API之一
Math类 Math类中包含一些对数据进行数学运算的方法,而该类中的方法全都是静态的.像这样的类称之为工具类. 1 public static int abs(int a) 2 对一个数据求绝对值 3 ...
- 黄衫女子,黄衫好.png
正想着团队项目中数据该如何解析,就收到了来自软工课程组的一件小黄衫,真是意外之喜.详问其来源,竟是因结对项目做的"较好"而来,顿感受之有愧. 结对项目是两人对文件系统的一个小模拟, ...