什么是Eureka组件

spring cloud Eureka ,提供服务注册和服务发现的功能。

一:spring cloud Eureka

Eureka Server 服务端

Eureka Client 客户端

服务注册

代码实现:

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>springCloud01</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>EurekaServer</module>
</modules>
<!-- 父工程-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JDk9-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

2.父工程下实现一个子模块Module实现Eureka Server

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springCloud01</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>EurekaServer</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies> </project>

3.配置文件

server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/

4.创建启动类:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class EuiekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EuiekaServerApplication.class,args);
}
}

5.检查打开

http://localhost:8761

注册第一个微服务

服务提供者

服务消费者和服务提供者都是通过Eureka Client 连接到Eureka Server完成注册。

1.创建一个模块,实现Eureka Client

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springCloud01</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies> </project>

2.配置文件:

server:
port: 8010
spring:
application:
name: provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true

3.创建启动类:

package com.southwind;

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

4.数据持久化:

接口:

package com.southwind.repository;

import com.southwind.entity.Student;

import java.util.Collection;

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

实现类:

package com.southwind.repository.impl;

import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository; import java.util.Collection;
import java.util.HashMap;
import java.util.Map; public class StudentRepositoryImpl implements StudentRepository {
private static Map<Integer,Student> map;
static {
map=new HashMap<>();
map.put(1,new Student(1,"张三"));
map.put(1,new Student(2,"李四"));
map.put(1,new Student(3,"王五"));
}
@Override
public Collection<Student> findAll() {
return map.values();
} @Override
public Student findById(Integer id) {
return map.get(id);
} @Override
public void saveOrUpdate(Student student) {
map.put(student.getId(),student);
} @Override
public void deleteById(Integer id) {
map.remove(id);
}
}

Handler:

package com.southwind.Handler;

import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.Collection; @RestController
@RequestMapping("/provider")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findall")
public Collection<Student> findall(){
return studentRepository.findAll();
}
@GetMapping("/findbyid/{id}")
public Student findById(@PathVariable("id") Integer id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping("/delete")
public void deletebyid(Integer id){
studentRepository.deleteById(id);
} }

Eureka 注册中心和服务提供者的更多相关文章

  1. Spring Cloud Eureka 注册中心 服务消费者 服务提供者之间的关系以及高可用之间的联系

    注册中心:提供服务的注册与查询(发现) 服务提供者:服务的提供方,提供服务的一方. 服务消费者:服务的消费方,使用服务的一方. 我们没有注册中心,服务提供者与服务消费者同样可以调用,通过spring中 ...

  2. Spring-cloud (一):Eureka注册中心搭建

    前提 系统安装jdk1.8及以上,配置好maven的ide(这里用idea进行演示,maven版本3.5,配置阿里云源) 项目搭建 新建一个maven项目,创建最简单的那种就好,项目名这里为Eurek ...

  3. 微服务---Eureka注册中心(服务治理)

    在上一篇的初识SpringCloud微服务中,我们简单讲解到服务的提供者与消费者,当服务多了之后,会存在依赖与管理之间混乱的问题,以及需要对外暴露自己的地址,为了解决此等问题,我们学习Eureka注册 ...

  4. idea搭建Eureka注册中心

    服务的注册与发现 关系调用说明: 服务生产者启动时,向服务注册中心注册自己提供的服务 服务消费者启动时,在服务注册中心订阅自己所需要的服务 注册中心返回服务提供者的地址信息个消费者 消费者从提供者中调 ...

  5. Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用-服务提供和消费

    由于 Eureka 注册中心只是在内存中保存服务注册实例,并且没有将服务注册实例进行同步,因此我们需要对服务提供和消费进行调整,需要指定服务提供和消费的注册.服务发现的具体Eureka 注册中心配置, ...

  6. SpringCloud (一)Eureka注册中心搭建

    前提 系统安装jdk1.8及以上,配置好maven的ide(这里用idea进行演示,maven版本3.5,配置阿里云源) 项目搭建 新建一个maven项目,创建最简单的那种就好,项目名这里为Eurek ...

  7. 《springcloud 一》搭建注册中心,服务提供者,服务消费者

    注册中心环境搭建 Maven依赖信息 <parent> <groupId>org.springframework.boot</groupId> <artifa ...

  8. 这个注册的 IP 网络都不通了,Eureka 注册中心竟然无法踢掉它!

    本文导读: 微服务技术架构选型介绍 k8s 容器化部署架构方案 Eureka 注册中心问题场景 问题解决手段及原理剖析 阅读本文建议先了解: 注册中心基本原理 K8s(Kuberneters)基本概念 ...

  9. Spring Cloud第二篇 | 使用并认识Eureka注册中心

    ​ 本文是Spring Cloud专栏的第二篇文章,了解前一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 ​​ 一.Sprin ...

  10. Spring Cloud第三篇 | 搭建高可用Eureka注册中心

    ​ ​本文是Spring Cloud专栏的第三篇文章,了解前两篇文章内容有助于更好的理解后面文章: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...

随机推荐

  1. Typora基本使用

    Typora主要功能介绍 1.语言环境 文件>>>偏好设置>>>系统语言 2.创建另一个编辑页面 ctrl+N 几乎所有软件的新建页面的快捷键都是它 3.保存文件 ...

  2. node学习01

    1.前言 Node.js 是一个开源和跨平台的 JavaScript 运行时环境 Node.js 在浏览器之外运行 V8 JavaScript 引擎(Google Chrome 的内核). 这使得 N ...

  3. Jmeter——结合Allure展示测试报告

    在平时用jmeter做测试时,生成报告的模板,不是特别好.大家应该也知道allure报告,页面美观. 先来看效果图,报告首页,如下所示: 报告详情信息,如下所示: 运行run.py文件,运行成功,如下 ...

  4. 求10以内所有偶数和-Java

    public class Demo{ //求10以内所有偶数和 public static void main (String[] args){ int sum = 0; for(int i = 0; ...

  5. 第二十五节:scrapy爬虫识别验证码(四)手绘验证码识别

    一.介绍 今天主要介绍的是微博客户端在登录时出现的四宫格手绘验证码,不多说直接看看验证码长成什么样.        二.思路 1.由于微博上的手绘验证码只有四个宫格,且每个宫格之间都有有向线段连接,所 ...

  6. Zabbix6.0使用教程 (一)—zabbix新增功能介绍1

    使用zabbix的小伙伴应该都有关注到目前zabbix的大版本已经更新到了6.0,后面乐乐将会对如何使用zabbix6.0做一个使用教程的系列,大家可以持续关注,这篇我们主要聊聊zabbix6.0新增 ...

  7. Javascript | 分别用async await异步方法和Promise来实现一个简易的求职程序

      关注公众号,一起交流,微信搜一搜: LiOnTalKING   JavaScript Promise Promise 是一个 ECMAScript 6 提供的类,目的是更加优雅地书写复杂的异步任务 ...

  8. 前缀树(Tire)—Python

    核心思想 空间换时间,是一种用于快速减速的多叉树结构,利用字符串的公共前缀来降低时间 优缺点: 优点:查询效率高,减少字符比较 缺点:内存消耗较大 每次都会从头向下一直到字符串结尾 前缀树 1 单个字 ...

  9. 安装node.js与webpack创建vue2项目

    本文为博主原创,转载请注明出处: 1.安装node.js 下载地址:http://nodejs.cn/download/ (可查看历史版本) node.js 中文网:http://nodejs.cn/ ...

  10. 什么是Rabbitmq消息队列? (安装Rabbitmq,通过Rabbitmq实现RPC全面了解,从入门到精通)

    目录 Rabbitmq 一: 消息队列介绍 1.介绍 2.MQ解决了什么问题 1.应用的解耦 2.流量削峰 3.消息分发(发布订阅: 观察者模式) 4.异步消息(celery就是对消息队列的封装) 3 ...