spring cloud学习--eureka 01
本博客为学习使用,学习教程翟永超 spring cloud 微服务实战
搭建eureka server注册中心
spring initialize构建spring boot项目
版本与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的更多相关文章
- spring cloud学习--eureka 02
开启eureka client的注解@EnableDiscoveryClient的功能类DiscoveryClient梳理图 获取server url位于类EndpointUtils的getServi ...
- spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法
turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...
- Spring Cloud学习(一):Eureka服务注册与发现
1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...
- Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...
- Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇
Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...
- spring cloud之eureka简介
最近线上的接口出了一些问题,有一些可能不是代码的问题,但是由于是测试和其他方面的同事爆出来的,所以感觉对接口的监控应该提上日程. 经过搜索发现,spring cloud的eureka就是专门做这方面工 ...
- spring cloud 学习资料
spring cloud 学习资料 网址 拜托!面试请不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA
- Spring Boot和Spring Cloud学习资源推荐
Spring Boot和Spring Cloud学习资源推荐 比较好的学习资源,分享一下. 1.Spring Boot官方文档:http://projects.spring.io/spring-b ...
- Spring Cloud Security&Eureka安全认证(Greenwich版本)
Spring Cloud Security&Eureka安全认证(Greenwich版本) 一·安全 Spring Cloud支持多种安全认证方式,比如OAuth等.而默认是可以直接添加spr ...
随机推荐
- topic模式下的收发
生产者: import pika import sys connection = pika.BlockingConnection(pika.ConnectionParameters( host='lo ...
- go web编程——自定义路由设计
本文主要讲解go语言web编程中自定义路由器的设计.在此之前需要先了解一下go语言web编程中路由与http服务的基本原理,可以参考笔者另一篇博文:go web编程——路由与http服务 . 我们已经 ...
- JVM(18)之 Class文件
开发十年,就只剩下这套架构体系了! >>> 关于类加载机制的相关知识在前面的博文中暂时先讲那么多.中间留下了很多问题,从本篇博文开始,我们来一一解决. 从我们最陌生而又最熟 ...
- js本地时间格式化
var myDate = new Date(); //获取当前时间及日期 var year=myDate.getYear(); // 获取当前年份(当前年份-1900) var fyear=myDat ...
- [置顶]unity常用开发套件
维京人骨骼动画制作(Unity 2D Animation资源包使用指南) 维京人骨骼动画制作2(Unity 2D IK资源包使用指南) -------------------------------- ...
- windows安装 阿里云的Fun工具
由于项目使用到了 函数计算,特此了解到了需要安装 阿里云的Fun工具 Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算.API 网关.日志服务等资源.它通过一个 ...
- python列表解析和生成器表达式
列表解析作为动态创建列表的强大工具,值得学习. 列表解析技术之前的状况--函数式编程. lambda.filter(), map() enumerate, sorted, any, all, zip ...
- Vue--按键修饰符(逐个学习按键修饰符)
在监听键盘事件时,我们经常需要检查常见的键值.Vue 允许为 v-on 在监听键盘事件时添加按键修饰符: <!-- 只有在 `keyCode` 是 13 时调用 `vm.submit()` -- ...
- matlab中求解线性方程组的rref函数
摘自:http://www.maybe520.net/blog/987/ matlab中怎么求解线性方程组呢? matlab中求解线性方程组可应用克拉默法则(Cramer's Rule)即通过det( ...
- 剑指Offer-51.构建乘积数组(C++/Java)
题目: 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不能 ...