本博客为学习使用,学习教程翟永超 spring cloud 微服务实战

搭建eureka server注册中心

spring initialize构建spring boot项目

构建网址:https://start.spring.io/

版本与spring boot版本匹配



此处选择Brixton.SR5版本,spring boot选择1.3.7版本

maven dependencies

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

启动eureka服务注册中心

//启动eureka 服务
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

eureka配置文件配置

spring.application.name=eureka-server
server.port=1111 eureka.instance.hostname=localhost
#禁用eureka的client功能,为false表示不向注册中心注册自己
eureka.client.register-with-eureka=false
#不检索服务
eureka.client.fetch-registry=false
#服务地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

通过访问http://localhost:1111/可以查看Eureka的信息面板

注册服务提供者

maven dependencies

<dependency>
<groupId>org.springf ramework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

启动项

//激活erueka中的DiscoveryClient实现
//自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

提供的服务接口

@RestController
public class HelloController
{
private final Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired
private DiscoveryClient discoveryClient; @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index()
{
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
logger.info("/hello, host: {}, service_id : {}.", serviceInstance.getHost(), serviceInstance.getServiceId());
return "hello world!";
}
}

配置文件

spring.application.name=eureka-client
server.port=2001 eureka.instance.hostname=localhost
#eureka的client功能默认为true
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
#注册服务的注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

启动eureka-client,发现eureka-server注册中心页面出现了一个服务实例,说明注册成功

然后访问eureka-client服务接口http://localhost:2001/hello,打印出hello world!,说明服务调用成功

服务高可用

注册中心服务高可用是建立在注册中心即使注册中心,本身也是服务,也可以注册到其他的注册中心

修改eureka-server服务配置文件

  • 复制application.properties两份,命名为application-peer1.properties,application-peer2.properties。清空application.peroperties
  • 修改applicaton-peer1.properties内容为
spring.application.name=eureka-server
server.port=1111 eureka.instance.hostname=peer1
#禁用eureka的client功能
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/
  • 修改application-peer2.properties内容为
spring.application.name=eureka-server
server.port=1112 eureka.instance.hostname=peer2
#禁用eureka的client功能
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://peer2:1111/eureka/
  • 在/etc/hosts文件下加入127.0.0.1 peer1 127.0.0.1 peer2。

    如果不在此处用peer1和peer2,而是在两个properties中都使用localhost,则会出问题

  • 修改eureka-client的application.properties配置文件为

spring.application.name=eureka-client
server.port=2001 eureka.instance.hostname=localhost
#eureka的client功能默认为true
#eureka.client.register-with-eureka=false
#获取服务清单,默认为true
#eureka.client.fetch-registry=false
#修改缓存清单的更新时间
eureka.client.registry-fetch-interval-seconds=30 #服务续约
#服务续约调用的时间间隔,默认是30秒
eureka.instance.lease-renewal-interval-in-seconds=30
#服务失效时间
eureka.instance.lease-expiration-duration-in-seconds=90 #注册服务的注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/

启动两个eureka-server注册中心和eureka-client

java -jar eureka-server-001.jar --spring.profiles.active=peer1

java -jar eureka-server-001.jar --spring.profiles.active=peer2

在peer1的服务页面会看到的内容为



在peer2的服务页看到的内容为

当关闭peer1注册中心,调用eureka-client服务接口,仍然能够获得hello world!,说明注册中心高可用配置成功

ribbon服务消费者

在eureka-client基础上加入ribbonmaven依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

配置application.properties

spring.application.name=ribbon-consumer
server.port=9000 eureka.instance.hostname=localhost
#eureka的client功能默认为true
#eureka.client.register-with-eureka=false
#获取服务清单,默认为true
#eureka.client.fetch-registry=false
#修改缓存清单的更新时间
eureka.client.registry-fetch-interval-seconds=30 #服务续约
#服务续约调用的时间间隔,默认是30秒
eureka.instance.lease-renewal-interval-in-seconds=30
#服务失效时间
eureka.instance.lease-expiration-duration-in-seconds=90 #注册服务的注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

主类上增加代码

//激活erueka中的DiscoveryClient实现
//自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication
{
//注入RestTemplate
@Bean
//开启客户端负载均衡
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}

写一个消费类

@RestController
public class ConsumerController
{
@Autowired
RestTemplate restTemplate; @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer()
{
return restTemplate.getForEntity("http://EUREKA-CLIENT/hello", String.class).getBody();
}
}

spring cloud学习--eureka 01的更多相关文章

  1. spring cloud学习--eureka 02

    开启eureka client的注解@EnableDiscoveryClient的功能类DiscoveryClient梳理图 获取server url位于类EndpointUtils的getServi ...

  2. spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法

    turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...

  3. Spring Cloud学习(一):Eureka服务注册与发现

    1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...

  4. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  5. Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇

    Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...

  6. spring cloud之eureka简介

    最近线上的接口出了一些问题,有一些可能不是代码的问题,但是由于是测试和其他方面的同事爆出来的,所以感觉对接口的监控应该提上日程. 经过搜索发现,spring cloud的eureka就是专门做这方面工 ...

  7. spring cloud 学习资料

    spring cloud 学习资料 网址 拜托!面试请不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA

  8. Spring Boot和Spring Cloud学习资源推荐

    Spring Boot和Spring Cloud学习资源推荐   比较好的学习资源,分享一下. 1.Spring Boot官方文档:http://projects.spring.io/spring-b ...

  9. Spring Cloud Security&Eureka安全认证(Greenwich版本)

    Spring Cloud Security&Eureka安全认证(Greenwich版本) 一·安全 Spring Cloud支持多种安全认证方式,比如OAuth等.而默认是可以直接添加spr ...

随机推荐

  1. TP50、TP90、TP99、TP999

    TP=Top Percentile,Top百分数,是一个统计学里的术语,与平均数.中位数都是一类.TP50.TP90和TP99等指标常用于系统性能监控场景,指高于50%.90%.99%等百分线的情况. ...

  2. vue-cli3脚手架的安装

    如果之前有安装过其他的版本的话,要先卸载 卸载:npm uninstall vue-cli-g  或  yarn global remove vue-cli 安装:npm i @vue/cli -g ...

  3. vue不是内部或外部命令的解决方法

    1.在nodejs的安装目录下,找到vue.cmd,将此路径加到环境变量中,我是通过nvm管理node版本的,路径是C:\Users\hy\AppData\Roaming\nvm\v6.10.0,关闭 ...

  4. Java面向对象的特征与含义

    面向对象的主要特征包括抽象.继承.封装和多态. 抽象 把一个类对象的共同特征总结出来,构造新类的过程. 继承 从已有类中得到继承信息,创建新类的过程. 封装 把数据和对数据的操作绑定起来,对数据的访问 ...

  5. Swoole 简单学习(2)

    Swoole 简单学习(2) swoole之tcp服务器: //创建tcp服务器new swoole_server(string $host,int $port,int $mode=SWOOLE_PR ...

  6. 同步与异步,阻塞与非阻塞 bio,nio,aio

    BIO.NIO和AIO的区别(简明版) 同步异步,阻塞非阻塞: https://www.zhihu.com/question/19732473   转载请注明原文地址:http://www.cnblo ...

  7. 十分钟理解Redux核心思想,过目不忘。

    白话Redux工作原理.浅显易懂. 如有纰漏或疑问,欢迎交流. Redux 约法三章 唯一数据源(state) 虽然redux中的state与react没有联系,但可以简单理解为react组件中的th ...

  8. AI比医生更好地发现皮肤癌,未来计算机技术可渗透医院

    未来机器人将取代医生?这可能是事实.为什么这么多年轻人选择计算机行业,因为这是一个趋势.据法新社报道,研究人员周二称,一项计算机技术比人类皮肤科医生在检测皮肤癌方面的表现要好得多,因为这项研究是为了寻 ...

  9. cnpm 安装和 command not found

    安装cnpm出错 > $ npm install -g cnpm --registry=https://registry.npm.taobao.org 按照淘宝 NPM 镜像安装,cnpm -v ...

  10. [Java复习] 微服务

    1. 怎么样定义一个微服务,或划分服务比较合理?业务导向的共性? 对应服务拆分,先设计高内聚低耦合的领域模型(DD),再实现相应的分布式系统是一种比较合理的方式. 微服务是手段,不是目的.目的是为了让 ...