一 eureka 带来的优势
1. eureka提供的服务本身既是服务提供者,也是服务消费者,eureka服务端通过相互注册的方式组成服务注册中心,多个服务端实例通过相互复制的方式,实现服务注册中心的数据同步
2. eureka 客户端会缓存服务注册表中的信息,微服务无需每次从服务注册中心获取服务提供者信息,降低了服务注册中心的压力,同时即使服务注册中心所有节点都宕掉,服务调用端仍可通过客户端缓存的服务注册表信息找到服务提供者并完成调用
3. 服务提供者会周期性(默认30s)向服务注册中心发送心跳以续约自己周期,服务注册中心在一定时间(默认90s)没有接收到服务提供者的心跳,则注销该实例
4. 当eureka因为网络故障等问题而丢失大量心跳连接的时候,则Eureka节点会进入"自我保护模式",并且保留那些"心跳死亡"的服务注册信息不过期,并且对新服务仍然提供注册服务,当网络恢复正常后退出"自我保护模式"
eureka通过心跳检测,客户端缓存提高了系统的可用性,灵活性,以及可伸缩性
二 erueka服务注册中心实例-单节点
1 pom配置
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
com.kklixin
microservice-discovery-eureka
1.0
1.8
Greenwich.SR2
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2. application.yml 配置
server:
port: 8081
eureka:
client:
register-with-eureka: false #是否将自己注册到服务注册中心
fetch-registry: false #是否从服务注册中心获取注册信息
service-url:
defaultZone: http://localhost:8081/eureka/
3 启动类代码
package com.kklixin.discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MicroserviceDiscoveryEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
}
}
三 服务注册中心 高可用
application-peer1.yml
server:
port: 1111
eureka:
instance:
hostname: peer1
prefer-ip-address: false #通过此属性值(true)可设置服务注册中心通过ip地址注册,默认为false
client:
service-url:
defaultZone: http://peer2:1112/eureka/
spring:
application:
name: eureka-service
application-peer1.yml
server:
port: 1112
eureka:
instance:
hostname: peer2
prefer-ip-address: false #通过此属性值(true)可设置服务注册中心通过ip地址注册,默认为false
client:
service-url:
defaultZone: http://peer1:1111/eureka/
spring:
application:
name: eureka-service
其他代码和单节点一致,最重要的是服务注册中心节点互相注册形成高可用注册中心
四 服务提供者
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
com.kklixin
hello-service
1.0
hello-service
1.8
Greenwich.SR2
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.yml
spring:
application:
name: hello-service
server:
port: 8080
eureka:
client:
service-url:
defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
启动类HelloServiceApplication.java
package com.kklixin.helloservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //标注服务为客户端行为
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
}
提供服务的controller
package com.kklixin.helloservice.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private final static Logger log = LoggerFactory.getLogger(HelloController.class);
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
五 服务发现与消费
pom.xml
启动类代码
RibbonConsumerApplication.java
package com.kklixin.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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {
/**
* 注册bean并且开启客户端负载均衡
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
消费服务的controller
ConsumerController.java
package com.kklixin.ribbonconsumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value="consumer",method = RequestMethod.GET)
public String helloConsumer(){
return restTemplate.getForEntity("http://hello-service/hello",String.class).getBody();
}
}
application.yml
server:
port: 8081
spring:
application:
name: consumer-service
eureka:
client:
service-url:
defaultZone: http://peer1:1111/eureka,http://peer2:1112/eureka
高可用eureka服务注册中心的eureka面板信息