SpringCloud学习心得——Eureka注册中心

示范代码链接

定义

SpringCloud EurekaSpringCloud Netflix微服务套件的一部分,基于 REST 的服务,并且提供了基于 Java 的客户端组件,主要负责实现微服务架构中的服务治理功能。

与Zookeeper区别与联系

  1. CAP 定理:即 C 为数据一致性;A 为服务可用性;P 为服务对网络分区故障的容错性。这三个特性在任何分布式系统中都不能同时满足,最多同时满足两个。
  2. Eureka 是基于 AP 原则构建的,而 ZooKeeper 是基于 CP 原则构建的。
    1. Zookeeper 有一个 Leader,而且在这个 Leader 无法使用的时候通过 Paxos(ZAB)算法选举出一个新的 Leader。这个 Leader 的任务就是保证写数据的时候只向这个 Leader 写入,Leader 会同步信息到其他节点。通过这个操作就可以保证数据的一致性。
    2. 去 Eureka 中去拉取服务列表,查看你调用的服务在不在其中,在的话就拿到服务地址、端口等信息,然后调用。

搭建Eureka中心

创建maven项目

添加启动类

<!-- Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- Spring Cloud -->
<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>

添加配置文件

spring:
application:
name: myEureka-Server server:
port: 8761 eureka:
client:
# 因为自己是主机,所以不向自己注册
register-with-eureka: false
# 自己就是注册中心,所以不用去检索服务
fetch-registry: false

运行,并访问,http://localhost:8761

创建服务提供者

创建项目注册到Eureka

新建maven项目,引入依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<depenpency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies> <!-- Spring Cloud -->
<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>

创建启动类,注意,此处在启动类上的注解是@EnableDiscoveryClient

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

添加测试接口

@RestController
@RequestMapping("/user")
public class UserController { @GetMapping("/hello")
public String sayHello(){
return "hello";
}
}

增加配置文件

server:
port: 8081 spring:
application:
name: myEureka-Service eureka:
client:
#这次要注册到eureka上
register-with-eureka: true
serviceUrl:
defaulZone: "http://localhost:8761/eureka/"
instance:
#采用ip注册的方式
prefer-ip-address: true
#id的格式
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

启动成功,去eureka看一下,发现了8762的服务

创建消费者

创建maven项目,eureka-consumer,添加相同依赖与启动类,但要更改配置文件

spring.application.name=eureka-client-article-service
server.port=8082

使用RestTemplate获取Rest服务端调用接口

@Configuration
public class BeanConfiguration {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}

创建接口

@RestController
public class ArticleController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/article /callHello")
public String callHello() {
return restTemplate.getForObject("http://localhost:8081/user/hello", String.class);
}
}

调用可以看到成功
我们的目的是通过服务调用,而不要关心端口
改造 RestTemplate的配置,添加一个 @LoadBalanced 注解,这个注解会自动构造LoadBalancerClient 接口的实现类并注册到 Spring容器中

@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}

接下来就是改造调用代码,我们不再直接写固定地址,而是写成服务的名称,这个名称就是我们注册到 Eureka 中的名称,是属性文件中的 spring.application.name

@GetMapping("/callHello")
public String callHello() {
return restTemplate.getForObject("http://myEureka-Service/user/hello", String.class);
}

调用成功,舒服了

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

 

SpringCloud学习心得之Eureka注册中心的基本使用的更多相关文章

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

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

  2. Spring-Cloud(三)Eureka注册中心实现高可用

    前言: spring-cloud为基础的微服务架构,所有的微服务都需要注册到注册中心,如果这个注册中心阻塞或者崩了,那么整个系统都无法继续正常提供服务,所以,这里就需要对注册中心进行集群,换言之,高可 ...

  3. SpringCloud集成Security安全(Eureka注册中心)

    1.说明 为了保护注册中心的服务安全, 避免恶意服务注册到Eureka, 需要对Eureka Server进行安全保护, 本文基于Spring Security方案, 为Eureka Server增加 ...

  4. SpringCloud实战之初级入门(一)— eureka注册中心

    目录 写在前面 1.资料目录 2.环境介绍 3.eureka注册中心 3.1 创建工程 3.2 启动工程 5.eureka注册中心集群高可用 6.结语 7.一点点重要的事情 写在前面 我在软件行业浸泡 ...

  5. SpringCloud学习心得—1.2—Eureka注册中心的密码认证、高可用的设置

      SpringCloud学习心得—1.2—Eureka注册中心的密码认证.高可用的设置 这是相关代码 链接 Eureka开启密码配置 添加依赖 <dependency> <grou ...

  6. SpringCloud学习笔记(1):Eureka注册中心

    简介 Eureka是Netflix开源的基于rest的服务治理方案,分为Server端和Client端,Server端为注册中心,其他微服务通过Client端连接Server端进行服务的注册和发现. ...

  7. SpringCloud的入门学习之深入理解Eureka注册中心

    1.Eureka 注册中心三种角色. 答:a.Eureka Server,注册中心,通过 Register.Get.Renew 等接口提供服务的注册和发现. b.Application Service ...

  8. SpringCloud学习心得—1.3—Eureka与REST API

      SpringCloud学习心得—1.3—Eureka与REST API Eureka的REST API接口 API的基本访问 Eureka REST APIEureka 作为注册中心,其本质是存储 ...

  9. SpringCloud之eureka注册中心入门

    eureka注册中心 一.基本概念 SpringCloud封装 了Netflix公司的eureka作为自己微服务的注册中心.这个注册中心和dubbo中的zookeeper很相似,简单来说,只要你可以将 ...

随机推荐

  1. Appium 退出和启动

    # 退出驱动driver.quit() # 退出当前应用driver.close_app() # 启动当前应用driver.launch_app() # 置于后台XX秒后恢复driver.backgr ...

  2. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  4. [LeetCode] 489. Robot Room Cleaner 扫地机器人

    Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. Th ...

  5. [LeetCode] 507. Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  6. 【神经网络与深度学习】【计算机视觉】图解YOLO

    图解YOLO 晓雷 3 个月前 YOLO核心思想:从R-CNN到Fast R-CNN一直采用的思路是proposal+分类 (proposal 提供位置信息, 分类提供类别信息)精度已经很高,但是速度 ...

  7. TypeError: Cannot read property 'compilation' of undefined

    Vue build失败 TypeError: Cannot read property 'compilation' of undefined 1.   使用npm run build 失败 使用npm ...

  8. centos 如何修改docker镜像和容器的默认存放路径

    原因:通过df -h查看磁盘利用的时候,目前挂载的太小了,所以尝试挂载到其他地方 1 先看看默认存放的路径在哪儿 方法1:docker info 方法2:sudo docker info | grep ...

  9. LeetCode | 152. 乘积最大子序列

    原题(Medium): 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 思路: 遍历数组时且逐元素相乘时,如果遇到了0,在求乘积最大值的情况下,0左边的元素 ...

  10. Django组件之cookie、session

    一.cookie 1.1 产生背景 HTTP协议是无状态的,对服务器来说,每次的请求都是独立的.状态可以理解为客户端和服务器在某次会话中产生的数据,那无状态的就以为这些数据不会被保留.会话中产生的数据 ...