Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。

Eureka的基本架构:

1、Eureka Server

  • 提供服务注册和发现

2、Service Provider

  • 服务提供方
  • 将自身服务注册到Eureka,从而使服务消费方能够找到

3、Service Consumer

  • 服务消费方
  • 从Eureka获取注册服务列表,从而能够消费服务

一、创建服务注册中心

  1、创建spring boot项目(利用idea的Spring Initializr快速创建项目)

  2、引入eureka-server依赖(也可以在创建项目时勾选eureka-server服务,自动生成依赖) 注意版本号,版本不对可能启动会报错,本次使用Dalston.SR1版本

<?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>com.daqsoft</groupId>
<artifactId>cloud_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>cloud_demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
</properties> <dependencies>
<!--添加依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

  3、启动类中开启服务注册中心功能

package com.daqsoft;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
//开启服务注册中心
@EnableEurekaServer
public class CloudDemoApplication {
public static void main(String[] args) {
SpringApplication.run(CloudDemoApplication.class, args);
}
}

  4、在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties中问增加如下配置:

server.port=1111

#是否将自己注册到Eureka Server,默认为TRUE
eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册信息,默认为TRUE
eureka.client.fetch-registry=false
#设置与Eureka Server的交互地址,查询服务和注册服务都需要依赖这个地址
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

  5、浏览器访问   localhost:1111  (此时没有任务服务注册)

  

二、创建服务提供方

  1、创建spring boot项目如上

  2、添加eureka依赖

<?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>com.daqsoft</groupId>
<artifactId>server_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>server_demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
</properties> <dependencies>
<!--添加依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

  3、启动类中添加注解,激活DiscoveryClient

package com.daqsoft;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
//激活DiscoveryClient的使用
@EnableDiscoveryClient
public class ServerDemoApplication { public static void main(String[] args) {
SpringApplication.run(ServerDemoApplication.class, args);
}
}

  4、创建controller提供简单的添加任务

package com.daqsoft;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @Description Created by liaoxx on 2017-6-12.
*/
@RestController
public class ComputerController {
private final Logger logger = Logger.getLogger(getClass()); @Autowired
private DiscoveryClient client; @RequestMapping(value = "/add",method = RequestMethod.GET)
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
ServiceInstance instance = client.getLocalServiceInstance();
Integer c = a + b;
logger.info("/add , post:"+instance.getPort()+" service_id:"+instance.getServiceId()+" result:"+c);
return c;
}
}

  5、修改配置文件如下

#指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问
spring.application.name=compute-service
#方便测试,设置为不同的端口
server.port=2222
#对应服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

   6、启动服务,再次访问  localhost:1111

  

  

  


  

spring cloud(服务注册中心及服务提供者——初学一)的更多相关文章

  1. spring cloud 服务注册中心eureka高可用集群搭建

    spring cloud 服务注册中心eureka高可用集群搭建 一,准备工作 eureka可以类比zookeeper,本文用三台机器搭建集群,也就是说要启动三个eureka注册中心 1 本文三台eu ...

  2. spring Cloud服务注册中心Eureka集群

    spring Cloud服务注册中心Eureka集群配置: 在application.yml文件加以下配置: server: port: 8761 tomcat: uri-encoding: UTF- ...

  3. Spring Cloud服务注册中心交付至kubernetes

    前言 服务发现原则: 各个微服务在启动时,会将自己的网络地址等信息注册到服务发现组件中,服务发现组件会存储这些信息 服务消费者可以从服务发现组件中查询到服务提供者的网络地址,并使用该地址来远程调用服务 ...

  4. 孰能巧用 Spring Cloud 服务注册中心Eureka

    Eureka介绍 在Spring Cloud Netflix 整合技术栈中,Eureka既可以作为服务注册中心也可以用于服务发现对整个微服务架构起着最核心的整合作用. Eureka是基于REST(Re ...

  5. spring Cloud服务注册中心eureka

    Eureka是什么? 1.Eureka是SpringCloud Netflix的核心子模块. 2.Eureka包含Eureka Server和Eureka Client. 3.Server提供注册服务 ...

  6. 二、Spring Cloud之注册中心 Eureka

    前言 算是正式开始学习 spring cloud 的项目知识了,大概的知道Springcloud 是由众多的微服务组成的,所以我们现在一个一个的来学习吧. 注册中心,在微服务中算是核心了.所有的服务都 ...

  7. Spring Cloud Eureka 注册中心集群搭建,Greenwich 最新版!

    Spring Cloud 的注册中心可以由 Eureka.Consul.Zookeeper.ETCD 等来实现,这里推荐使用 Spring Cloud Eureka 来实现注册中心,它基于 Netfl ...

  8. IDEA 创建Spring cloud Eureka 注册中心

    IDEA 创建Spring cloud Eureka 注册中心 一. 首先创建一个maven project Next之后填好groupId与artifactId,Next之后填好项目名与路径,点击F ...

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

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

随机推荐

  1. java.io.IOException: Can't read [\jre\lib\rt.jar]

    [proguard] java.io.IOException: Can't read [F:\e\java\jdk1.8.0_101\jre\lib\rt.jar] (Can't process cl ...

  2. Spring的下载与安装

    Spring是一个独立的框架,不依赖于任何Web服务器或容器.它既可在独立的JavaSE项目中使用,也可以在Java Web项目中使用. 下载和安装Spring框架可按如下步骤进行: 1.登录http ...

  3. CentOS 5.8下快速搭建FTP服务器

    学习安装和配置vsftpd: 实验环境:CentOS 5.8 x86_64 测试环境关掉防火墙和selinux. service iptables stop setenforce 0 1.安装vsft ...

  4. noip第10课资料

  5. Hdu4185 Oil Skimming

    Oil Skimming Problem Description Thanks to a certain "green" resources company, there is a ...

  6. hdu 4983 线段树+斐波那契数

    http://acm.hdu.edu.cn/showproblem.php?pid=4893 三种操作: 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 从l到r区间 ...

  7. verilog中defparam的用法 (verilog调用底层模块(只改变)参数的传递)

    当一个模块引用另外一个模块时,高层模块可以改变低层模块用parameter定义的参数值,改变低层模块的参数值可采用以下两种方式: 1)defparam 重定义参数 语法:defparam path_n ...

  8. EBS中查看其他用户或所有用户的请求和输出文件

      R12: How To Configure Access To Request Output Of The Same Responsibility (Doc ID 804296.1) To Bot ...

  9. [leet code 190]reverse bits

    1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...

  10. MarkdownPad 2 for Windows 10 预览问题解决方案。

    知乎问题: Windows 10 下 MarkdownPad2 预览无法显示是怎么回事? 文章提供的解决方法可以奏效.具体如下: 在我的 Win10 上测试了一次,确实会遇到这个问题,官方的说法是从 ...