1,什么叫做服务的注册与发现

服务的注册与发现基于注册中心,注册中心本身是一个服务,也相当于一个载体,其他服务的注册需要注册到这个注册中心上。

注册:当服务器启动的时候,会将自己的服务器信息,通过别名的形式注册到之前已经启动的注册中心上面

发现:在注册中心上面注册的服务,由注册中心共同管理,以该别名的方式去注册中心上获取到实际的服务通讯地址,让后在实现本地rpc调用远程

2,搭建注册中心

eureka注册中心:maven依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

配置文件:127.0.0.1:8100 启动路径

server:
port: 8100
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: false
###因为自己是为注册中心,不需要检索服务
fetch-registry: false

启动类:需要加上@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(final String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}

这样访问127.0.0.1:8100就可以进入eureka注册中心的web页面了,目前还没有服务注册上去。。。

3,实现服务注册

开始注册会员服务和订单服务:

配置都一样:就是配置文件上的地址不同

member,order 的maven依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

member的配置文件:name就是注册的别名,member的端口是8000,注册到注册中心上

###服务启动端口号
server:
port: 8000
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-aiyuesheng-member
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###需要注册到eureka
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true

orderr的配置文件:name就是注册的别名,member的端口是8001,注册到注册中心上

###服务启动端口号
server:
port: 8001
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-aiyuesheng-order
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###需要注册到eureka
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true

添加启动类:

@SpringBootApplication
@EnableEurekaClient
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

启动之后,两个服务都会注册到注册中心上了。。。

4,实现服务发现(实现rpc远程调用)

order 添加映射地址:

@RestController
public class IndexOrderController { @RequestMapping("/getOrder")
public String getOrder(){
return "获得订单成功";
} }

member 添加映射地址,去调用order 服务:

@RestController
public class IndexMemberController { // 封装了http方法,显得更加的优雅
@Autowired
private RestTemplate restTemplate; @RequestMapping("/getOrder")
public void getOrder() {
// url会在eureka注册中心上通过别名进行查找,restTemplate通过别名来找服务,是依赖ribbon,所以@LoadBalanced要在RestTemplate初始化要加上
String orderUrl = "http://app-aiyuesheng-order/getOrder";
String res = restTemplate.getForObject(orderUrl, String.class);
System.out.println("rpc远程调用服务成功");
System.out.println(res);
}
}

通过RestTemplate 去实现httpClient 的功能,即rpc 远程调用,因为RestTemplate是依赖客户端负载均衡器Ribbon的,所以在将RestTemplate注入到Spring容器中的时候,需要加上注解@LoadBalanced

将RestTemplate注入到Spring容器中:

@SpringBootApplication
@EnableEurekaClient
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
} // 注册到spring容器中
@Bean
// 开启负载均衡
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} }

访问:127.0.0.1:8000/getOrder 这是会员的入口,能够进入到order 服务的接口。。。。。

SpringCloud服务的注册发现--------Eureka的更多相关文章

  1. SpringCloud服务的注册发现--------Eureka自我保护机制

    1,Eureka 自我保护机制 Eureka注册中心,一些服务会注册到Eureka 服务器上,例如之前的member服务,order服务. 在网络不通的情况下,如果一个bmember 挂了,但是Eur ...

  2. SpringCloud服务的注册发现--------Eureka实现高可用

    1,Eureka作为注册中心,掌管者服务治理的功能,十分重要,如果注册中心的服务一旦宕机,所有的服务就会挂了,为此,实现注册中心的集群(高可用)就显得十分必要了 2,Eureka 搭建集群 实现原理就 ...

  3. SpringCloud服务的注册发现--------zookeeper实现服务与发现 + Ribbon实现客户端负载均衡

    1,Eureka 闭源了,但是我们可以通过zookeeper实现注册中心的功能. zookeeper 是一个分布式协调工具,可以实现服务的注册和发现,配置中心,注册中心,消息中间件的功能 2,工具准备 ...

  4. SpringCloud服务的注册发现--------consul实现服务与发现

    1,consul也可以替代Eureka实现注册和发现的功能,即注册中心. 之前在linux环境通过consul + upsync + nginx 实现nginx 的动态负载均衡 https://www ...

  5. 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)

    在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一 ...

  6. 服务注册发现Eureka

    一 Eureka相关概念 1 Peer   2 Zone   3 Region 地理区域   3 CAP理论   4 在线扩容   5     二 注册发现 Eureka 1 搭建Server服务端 ...

  7. 服务注册发现Eureka之二:高可用服务注册中心

    前言 在Spring Cloud系列文章的开始,我们就介绍了服务注册与发现,其中,主要演示了如何构建和启动服务注册中心Eureka Server,以及如何将服务注册到Eureka Server中,但是 ...

  8. 服务注册发现Eureka之一:Spring Cloud Eureka的服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  9. springcloud使用之服务的注册发现与消费

    随着spring的发展我们发现spring提供了越来越多的项目来帮我们简化框架的搭建,使我们站在巨人的肩膀行走,让我们更关注于开发我们的逻辑.随着技术的更替,我们的新项目也逐渐使用了springboo ...

随机推荐

  1. git指令-添加远程仓库

    git指令-添加远程仓库 首先在GitHub上创建属于你自己的远程仓库:例如我创建的远程仓库mybatis用于我最近保存的mybatis代码 目前,在GitHub上的这个learngit仓库还是空的, ...

  2. Python爬虫开发教程

     正文   现在Python语言大火,在网络爬虫.人工智能.大数据等领域都有很好的应用.今天我向大家介绍一下Python爬虫的一些知识和常用类库的用法,希望能对大家有所帮助.其实爬虫这个概念很简单,基 ...

  3. 攻防世界Mobile6 app1 XCTF详解

    XCTF_app1 先安装看看 点击芝麻开门之后会弹出“年轻人不要耍小聪明噢” 这大概就能看懂是点击之后进行判断,那就直接去看JEB,看看判断条件是什么 V1是输入的字符串,V2获取包信息(百度的), ...

  4. 在高德地图上用svg.js绘制简单图形

    这段时间做的一个项目,需要在地图上绘制简单的图形.在学习高德地图JS API的过程中,发现高德地图提供的点.线等API并不能满足我的需求,还好它开放了自定义图层CustomLayer,官方说自定义图层 ...

  5. Typescript 01 安装与使用

    ---恢复内容开始--- 一. 介绍 1. TypeScript 是由微软开发的一款开源的编程语言. 2. TypeScript 是 Javascript 的超级,遵循最新的 ES6.Es5 规范.T ...

  6. node.js-web服务器

    node.js--web服务器 目前最主流的三个Web服务器是Apache.Nginx.IIS. 使用 Node 创建 Web 服务器 以下是演示一个最基本的 HTTP 服务器架构(使用8081端口) ...

  7. Python基础知识(day1)

    day1 1.编码 ASCII码 1字节8位 2^8 = 256 位 万国码 unicode 4字节32位 #浪费空间 UTF-8 对unicode进行压缩 2.注释 单行注释 score = inp ...

  8. js小数计算引起的精度误差问题

    我记得刚开始学js的时候学到浮点有举例0.1+0.2 它的计算结果是: 0.1+0.20.30000000000000004 很神奇的一个计算,js是弱语言,在精度上没做处理: 我就自己定义了加减乘除 ...

  9. Spark入门(六)--Spark的combineByKey、sortBykey

    spark的combineByKey combineByKey的特点 combineByKey的强大之处,在于提供了三个函数操作来操作一个函数.第一个函数,是对元数据处理,从而获得一个键值对.第二个函 ...

  10. 在Linux环境安装redis步骤,且设置开机自动启动redis

    最近在linux环境安装了redis学习,目前已经安装成功且设置开机即启动状态,我把步骤流程记录了下来,分享给需要的小伙伴. 1.我在/usr/local/localsoftware/目录下创建了一个 ...