为什么要使用Eureka,因为在一个完整的系统架构中,任何单点的服务都不能保证不会中断,因此我们需要服务发现机制,在某个节点中断后,其它的节点能够继续提供服务,从而保证整个系统是高可用的。
服务发现有两种模式:一种是客户端发现模式,一种是服务端发现模式。Erueka采用的是客户端发现模式。
feign:Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便。 它具有可插拔注释支持,包括Feign注解和JAX-RS注解、Feign还支持可插拔编码器和解码器、Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载平衡的http客户端。
方式一:使用eureka实现服务治理
一、eureka-service 服务搭建
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
com.rainbow.eureka
sikl-road-eureka
0.0.1-SNAPSHOT
jar
sikl-road-eureka
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
org.springframework.cloud
spring-cloud-starter-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、启动类加注解:@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaDomeServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaDomeServiceApplication.class, args);
}
}
3、application.yml配置:
spring:
application:
name: eureka-service
server:
port: 1111 # 注册服务的端口号
eureka:
instance:
hostname: localhost # 指定主机名 (域名) 值=spring。profiles
server:
enable-self-preservation: false #关闭保护机制 可以将不可用的服务剔除
client:
register-with-eureka: false #不向注册中心注册资金
fetch-registry: false #关闭服务检索
serviceUrl:
defaultZone: http://eureka.instance.hostname:{eureka.instance.hostname}:eureka.instan