springcloud学习一 Eureka
来源:互联网 发布:奖金计算软件 编辑:程序博客网 时间:2024/06/08 07:53
Eureka分为3个部分,服务注册中心,服务提供者,服务消费者
搭建高可用服务注册中心:
创建springboot项目——eureka-server
启动类EurekaServerApplication中:
package com.example.eurekaserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer //让该项目变为一个注册中心@SpringBootApplicationpublic class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}
配置文件:
application.properties没有内容
application-peer1.properties:
server.port = 1111spring.application.name=eureka-server eureka.instance.hostname = peer1#禁止注册自己,单机版注册中心需要#eureka.client.register-with-eureka = false#注册中心职责是维护实例,不需要检索服务,单机版需要#Seureka.client.fetch-registry = false#注册地址eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} #关闭自我保护(解决服务注册不能及时更新)eureka.server.enable-self-preservation=false #默认时间 60*1000eureka.server.evictionIntervalTimerInMs: 4000
application-peer2.properties:
server.port = 1112spring.application.name=eureka-server eureka.instance.hostname = peer2#禁止注册自己#eureka.client.register-with-eureka = false#注册中心职责是维护实例,不需要检索服务#eureka.client.fetch-registry = false#注册地址eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/#指定该服务实例名(要保证唯一)eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} #关闭自我保护eureka.server.enable-self-preservation=false #默认时间 60*1000eureka.server.evictionIntervalTimerInMs:4000
访问1112端口也是一样的,,可以看到,两个注册中心相互注册了。所谓高可用注册中心就是多个注册中心相互注册,形成闭合,,
这样就能共享注册到任一注册中心的服务了。
服务提供者:
HelloApplication主类;
package com.example.hello;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;@EnableDiscoveryClient //该注解可以去发现并注册到注册中心@SpringBootApplicationpublic class HelloApplication {public static void main(String[] args) {SpringApplication.run(HelloApplication.class, args);}}
HelloWorld业务类:
package com.example.hello.web;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.RestController;@RestControllerpublic class HelloWorld {private final Logger logger = Logger.getLogger(getClass());@Autowiredprivate DiscoveryClient client;@RequestMapping("/hello")public String index() {ServiceInstance instance = client.getLocalServiceInstance();logger.info("/hello,host:"+ instance.getHost() + ",service_id:"+instance.getServiceId());String str = "Hello World";return str;}}
application.properties:
server.port=8011spring.application.name = hello-serviceeureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/#解决Eureka注册中心不能及时更新服务实例的问题#健康检查eureka.client.healthcheck.enabled: true#Eureka客户端像服务端发送心跳间隔时间,默认30seureka.instance.leaseRenewalIntervalInSeconds=1#Eureka服务端收到最后一次心跳后的等待时间上限,超过该时间后,会提出对应的服务实例eureka.instance.leaseExpirationDurationInSeconds=2
分别以8011和8022端口启动以上的hello项目,然后去注册中心查看,发现注册进来一个服务是咧,分别在8011和8022上:
服务消费者:
创建消费实例,并使用简单ribbon实现客户端负载均衡,下节详细介绍ribbon
创建ribbon-consumer (config包暂时不管,下节用的)
RibbonCousumerApplication主类:
package com.example.ribbonconsumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.ribbon.RibbonClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;import com.example.config.MyConfig;@EnableDiscoveryClient@SpringBootApplicationpublic class RibbonConsumerApplication {@Bean@LoadBalanced //开启客户端负载均衡,使用该注解后,RestTemplate就有了负载均衡的能力RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(RibbonConsumerApplication.class, args);}}
ConsumerController业务类:
package com.example.ribbonconsumer.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class ConsumerController {@AutowiredRestTemplate restTemplate;@Autowired private LoadBalancerClient loadBalancerClient;@RequestMapping(value="/ribbon-consumer",method=RequestMethod.GET)public String helloConsumer() {return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody(); //访问之前注册的HELLO服务}
application.properties:
spring.application.name=ribbon-consumerserver.port=9000eureka.client.healthcheck.enabled: trueeureka.instance.leaseRenewalIntervalInSeconds=1eureka.instance.leaseExpirationDurationInSeconds=2eureka.client.serviceUrl.defaultZone=http://peer1:1111/eur
启动服务,访问localhost:9000/ribbon-consumer,,在之前启动的两个hello服务的控制台,会看到交替打印信息,这就实现了负载均衡。
Eureka的基本使用就是这些,下节具体介绍一下ribbon负载均衡。
阅读全文
0 0
- springcloud学习一 Eureka
- 【微服务】Springcloud学习笔记(一) —— Eureka
- SpringCloud 学习记录(一):服务注册与发现(eureka+feign)
- springcloud学习笔记一:eureka服务注册与发现
- SpringCloud学习笔记-Eureka集群
- SpringCloud Eureka
- SpringCloud.Eureka
- SpringCloud Eureka
- SpringCloud学习:Eureka、Ribbon和Feign
- SpringCloud教程一:服务注册与发现(Eureka)
- springcloud架构搭建(一) Eureka服务器搭建及配置
- Springcloud学习(一)
- springcloud学习(一)
- SpringCloud学习:Eureka、Feign(含示例代码)
- eclipse springcloud服务发现服务注册(Eureka) 学习日志
- SpringCloud 服务发现组件 Eureka的初步学习
- SpringCloud 之 Eureka体验
- SpringCloud Eureka实战
- javaSpring使用maven初始搭建步骤java项目
- andorid:百度地图定位
- Oracle存储过程总结
- cmath 函数库常用函数
- MyBatis传入参数为list、数组、map写法
- springcloud学习一 Eureka
- 解决IDEA启动时报错:Java 1.8 or later is required.
- Java移位操作符及其对char、byte、short类型数值的处理
- JSTL标签
- 运维与自动化概述
- Mysql权限,用户添加,远程连接问题汇总
- 剑指Offer 面试题25:合并两个排序的链表(递归+非递归) Java代码实现
- 神经网络之cost函数损失函数,
- Vue 中 v-cloak的解读