(1-1)SpringCloud-Eureka:服务的注册与发现
SpringCloud Eureka是SpringCloud Netflix服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。下面来做一个示例:
一、搭建服务注册中心:
1.构建一个maven项目
2.添加maven依赖
<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.hope</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-server</name> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <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>Dalston.RC1</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> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
3.写启动类
package org.hope; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4.springboot的配置文件application.yml
server:
port: 8761 eureka:
instance:
hostname: localhost
client:
#由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
registerWithEureka: false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
5.运行EurekaServerApplication启动服务
在浏览器中输入http://localhost:8761,就可以看到Eureka的管理界面

二、服务注册与服务发现---注册服务提供者
1.新建maven项目
2.添加pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3.写启动类ServiceApplication
@EnableEurekaClient注解是基于spring-cloud-netflix依赖,只能eureka使用
@EnableDiscoveryClient注解是基于spring-cloud-commons依赖,并且在classpath中实现
它们的作用是一样的
package org.hope; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@EnableEurekaClient
//@EnableDiscoveryClient
@RestController
public class ServiceApplication { public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
} }
4.写一个Controller测试用
package org.hope; 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.RestController; @RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass()); @Autowired
private DiscoveryClient client; @RequestMapping(value = "hello", method = RequestMethod.GET)
public String index() {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
return "Hello World";
}
}
5.配置文件application.yml
eureka:
client:
serviceUrl:
#注册中心的地址
defaultZone: http://localhost:8761/eureka/
server:
#当前服务端口号
port: 8762
spring:
application:
#当前应用名称
name: service-hi
6.运行应用
我们会在Eureka的控制台上看到应用名,证明服务提供者已经成功注册在Eureka上了

7.在浏览器中输入http://localhost:8762/hello

https://gitee.com/huayicompany/springcloud-learn/tree/master/lesson1
参考:
[1] 博客,http://blog.csdn.net/forezp/article/details/69696915
[2] 博客,http://www.cnblogs.com/skyblog/p/5133752.html
[3] 《SpringCloud微服务实战》,电子工业出版社,翟永超
(1-1)SpringCloud-Eureka:服务的注册与发现的更多相关文章
- springCloud学习-服务的注册与发现(Eureka)
1.小记 这段时间有空,把springcloud的知识整理一下,好记性不如烂笔头,也让自己对springcloud有个清晰的认识.此次的整理记录主要借鉴了这位大佬的博客 https://blog.cs ...
- (一)Eureka 服务的注册与发现
(一)服务的注册于发现(eureka); Eureka Server: 服务注册中心,负责服务列表的注册.维护和查询等功能 在Idea里,新建项目,选择Spring initializer. 下面的p ...
- SpringCloud初体验:一、Eureka 服务的注册与发现
Eureka :云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. Eureka 可以大致理解为 房产中介 和 房东 的关系,房东想让租客租房子,首先要把房子 ...
- Eureka 服务的注册和发现
二.Eureka 服务端 1.新建一个 maven module 子项目 microservicecloud-eureka-server 2.pom.xml <project xmlns=&qu ...
- spring cloud 系列第1篇 —— eureka 服务的注册与发现 (F版本)
源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.eureka 简介 Spring Cloud Eureka使用Netflix ...
- SpringCloud Eureka服务注册及发现——服务端/客户端/消费者搭建
Eureka 是 Netflix 出品的用于实现服务注册和发现的工具. Spring Cloud 集成了 Eureka,并提供了开箱即用的支持.其中, Eureka 又可细分为 Eureka Serv ...
- 玩转SpringCloud(F版本) 一.服务的注册与发现(Eureka)
一.服务的注册与发现(Eureka) spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等 ...
- (转) 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...
- SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(转载)
SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本) 转载请标明出处:http://blog.csdn.net/forezp/article/details ...
- 【微服务】之二:从零开始,轻松搞定SpringCloud微服务系列--注册中心(一)
微服务体系,有效解决项目庞大.互相依赖的问题.目前SpringCloud体系有强大的一整套针对微服务的解决方案.本文中,重点对微服务体系中的服务发现注册中心进行详细说明.本篇中的注册中心,采用Netf ...
随机推荐
- C++\virtual 虚函数、纯虚函数
前提摘要: 虚函数联系到多态,多态联系到继承.所以本文中都是在继承层次上做文章.没了继承,什么都没得谈. 虚函数定义: 指向基类的指针或引用在操作它的多态类(子类/派生类)对象时,会根据不同的类对象, ...
- this语句
this语句 this语句调用构造器 原 因: 代码功能重复,重复会导致代码维护性低. 如何使用:this([实参]); 注意事项:构造器重载的调用,this(参数)必须写在构造方法第一行,因此 ...
- (๑•̀ㅂ•́)و✧随笔总目录ヾ(≧▽≦*)o
SSM整合进阶篇 日常手记 开源博客My Blog系列 短信接口攻击事件 读书笔记 SSM整合优化篇 SSM整合基础篇 SSM整合进阶篇 Spring+SpringMVC+MyBatis+easyUI ...
- java_web学习(八) jdbc连接mysql
首先我们来看一下主机与数据库的关系图 实际上是两台服务器 一:下载数据库驱动jar包存放WebContent—WEB-INF—lib目录下 1.2步骤 1. 2. 3 4. 1.3 将jar包导入到W ...
- LoadRunner 中实现MD5加密
最近在用loadrunner做一个压力测试,在编写脚本的时候发现传递参数的时候需要一个sign值,这个值是将参数进行MD5加密生成的,所以下面就说一说怎么对参数进行MD5加密. 1.首先我们需要一个加 ...
- Phabricator API Go 创建task/提交文件到Phabricator
Go Phabricator API 代码/程序创建task/提交文件到Phabricator Creat Task or upload file to phabricator with code i ...
- List集合及新特性引用
ArrayList就是动态数组,也是一个对象. 创建一个ArrayList对象,该对象存放在堆内存中,且是一个内存连续的内存区域. 1.ArrayList是用数组实现的,这个数组的内存是连续的,不存在 ...
- BZOJ 3668: [Noi2014]起床困难综合症【贪心】
3668: [Noi2014]起床困难综合症 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1305[Submit][St ...
- 城乡联谊胡策会糊厕R3
因为时间关系这把没设计题面,而且居然还出了锅……T_T 信 原题是leetcode WeeklyContest52 的T1(懒得去找url了 随便搞,但是无解输-1 数字统计 原题PE603 记前n个 ...
- [51nod1462]树据结构
题面: 给一颗以1为根的树. 每个点有两个权值:vi, ti,一开始全部是零. Q次操作: 读入o, u, d o = 1 对u到根上所有点的vi += d o = 2 对u到根上所有点的ti += ...