首先在idea中新建一个工程demo,作为父工程。
![](http://img.e-com-net.com/image/info10/6c2ba0ad8a8b43b7bd78a68639699441.jpg)
创建完成后的pom配置文件如下
com.example
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.13.RELEASE
UTF-8
UTF-8
1.8
Finchley.RELEASE
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Edgware.SR4
pom
import
org.springframework.boot
spring-boot-maven-plugin
server1
client
ribbon
zuul
然后创建子工程eureka server1服务注册中心,用于提供服务的注册与发现。
![](http://img.e-com-net.com/image/info10/1fc98f60a380409084f7c00c61f8dfa1.jpg)
server1 pom配置
server1
Demo project for Spring Boot
com.example
demo
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
修改server1 pom
![](http://img.e-com-net.com/image/info10/544291305ef34530aa53357f9baa811a.jpg)
pom添加相应依赖,配置完成后在启动类中@EnableEurekaServer。
之后进行application.yml相关配置。
server:
port: 1111
eureka:
instance:
hostname: qlocal
prefer-ip-address: true
lease-expiration-duration-in-seconds: 20
lease-renewal-interval-in-seconds: 10
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/
spring:
application:
name: SERVERSEV
server.port 声明端口号为 1111
eureka.instance.hostname 设置当前实例主机名为 qlocal(在本地hosts中添加 127.0.0.1 qlocal)
eureka.instance.prefer-ip-address 不使用主机名定义注册中心地址,而使用IP地址的形式。Spring会自动为我们获取第一个非回环IP地址(除环路IP外的第一个IP地址)。
eureka.instance.lease-expiration-duration-in-seconds 定义服务失效时间为20 秒。表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
默认为90秒
如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
该值至少应该大于lease-renewal-interval-in-seconds的值。
eureka.instance.lease-renewal-interval-in-seconds 定义服务续约任务(心跳)的调用间隔10秒。 表示eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量。
默认30秒
eureka.client.fetch-registry 为true 进行服务检查
eureka.client.register-with-eureka 为true 向注册中心注册自己
eureka.client.service-url. defaultZone=http://qlocal:1110/eureka/,http://qlocal:1111/eureka/ 指定服务注册中心的位置为http://qlocal:1110/eureka/,http://qlocal:1111/eureka/ 两个地址。
spring.application.name=SERVERSEV 指定应用名称SERVERSEV
配置完成后访问 qlocal:1111
![](http://img.e-com-net.com/image/info10/396288850010452c8dfd55f18bf6d411.jpg)
接着创建eureka client 服务提供者
![](http://img.e-com-net.com/image/info10/26fabfe4f2934bf8bb0fab0992613ae4.jpg)
pom配置
然后在启动类中添加@EnableEurekaClient
启动类
@RestController
@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/my")
public String home(@RequestParam String name){
return "my name" +name +" 端口" +port;
}
}
application.yml配置
server:
port: 1112
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1110/eureka/,http://localhost:1111/eureka/
prefer-same-zone-eureka: true
instance:
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
spring:
application:
name: CLIENTSEV
eureka.client.prefer-same-zone-eureka 实例是否使用同一zone里的eureka服务器,默认为true,理想状态下,eureka客户端与服务端是在同一zone下
访问 qlocal:1112/my?name=1
![](http://img.e-com-net.com/image/info10/6e37ded11c1e464699a306ceff4311f9.jpg)
![](http://img.e-com-net.com/image/info10/a1cc74c3ea37412c8f72a9bb914e8421.jpg)
eureka ribbon+hystrix 消费者+断路器
pom配置
com.example
ribbon
0.0.1-SNAPSHOT
jar
ribbon
Demo project for Spring Boot
com.example
demo
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.retry
spring-retry
org.springframework.boot
spring-boot-maven-plugin
想要自动重试必须添加retry依赖
想要自动重试必须添加retry依赖
想要自动重试必须添加retry依赖
application.yml配置
eureka:
client:
service-url:
defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/
server:
port: 1114
spring:
application:
name: RIBBONSERV
cloud:
loadbalancer:
retry:
enabled: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
RIBBONSERV:
ribbon:
ConnectTimeout: 250
ReadTimeout: 1000
OkToRetryOnAllOperations: true
MaxAutoRetriesNextServer: 2
MaxAutoRetries: 1
spring.cloud. loadbalancer.retry.enabled=true 开启重连机制
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000 断路器的超时时间,断路器的超时时间需要大于ribbon的超时时间,不然不会触发重试。
RIBBONSERV: #服务名(spring.application.name=RIBBONSERV)
ribbon:
ConnectTimeout: 250 #ribbon请求连接的超时时间
ReadTimeout: 1000 #ribbon 请求处理的超时时间
OkToRetryOnAllOperations: true #对所有操作请求都进行重试
MaxAutoRetriesNextServer: 2 #对下个实例的重试次数
MaxAutoRetries: 1 #对当前实例的重试次数
启动类配置
添加@EnableEurekaClient 向服务中心进行注册
@Bean //向程序ioc注入bean restTemplate
@LoadBalanced //表明 restTemplate开启负载均衡功能
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
新建一个Service类,通过访问之前Clientsev的/my接口进行测试。这里使用了程序名替代了具体的url地址,ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名
service
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "error")
public String helservice(String name){
return restTemplate.getForObject("http://CLIENTSEV/my?name="+name,String.class);
}
public String error(String name){
return "my name" + name +" 断路" ;
}
}
新建controller 类 调用service方法
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/my")
public String con(@RequestParam String name){
return helloService.helservice(name);
}
}
启动 一次开启 server 1110 1111 client 1112 1113 ribbon 1114(qlocal:1114/my?name=1)
![](http://img.e-com-net.com/image/info10/137e6c4008ff4ad2b7d6a091583b1db6.jpg)
zuul 网关
pom
com.example
zuul
0.0.1-SNAPSHOT
jar
zuul
Demo project for Spring Boot
com.example
demo
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.retry
spring-retry
org.springframework.retry
spring-retry
org.springframework.boot
spring-boot-maven-plugin
application.yml
eureka:
client:
service-url:
defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/
server:
port: 1115
spring:
application:
name: ZUULSERV
cloud:
loadbalancer:
retry:
enabled: true
zuul:
routes:
ribbon:
path: /ribbon/**
serviceId: RIBBONSERV
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
ZUULSERV:
ribbon:
ConnectTimeout: 250
ReadTimeout: 1000
OkToRetryOnAllOperations: true
MaxAutoRetriesNextServer: 2
MaxAutoRetries: 1
启动类
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
--------------------------------------------------------------------------------------------------------------------访问地址 qlocal:1115/ribbon/my?name=1
源码地址:链接:https://pan.baidu.com/s/1rdNfniIgEV_9yQjHFCeibw 密码:v2bd