首先在idea中新建一个工程demo,作为父工程。
创建完成后的pom配置文件如下
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<properties>
<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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<modules>
<module> server1</module>
<module>client</module>
<module>ribbon</module>
<module>zuul</module>
</modules>
</project>
<modules> 声明多个模块:server,client,ribbon,zuul。
<dependencyManagement> 统一依赖的版本号。只是声明依赖,并未被继承,只有子工程中写了该依赖项,且未注明版本号,才会从父项目中继承此项。(当子工程指定版本时,使用子工程中指定的版本)。
<dependencies> 所有在 <dependencies>里的依赖都会自动注入,默认被所有的子工程继承。即使子工程中不写依赖,在父工程中<dependencies>里的依赖也都会被子工程继承。
然后创建子工程eureka server1服务注册中心,用于提供服务的注册与发现。
server1 pom配置
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>server1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改server1 pom <parent>继承父工程
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<parent>相关配置取自父工程
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
接着创建eureka client 服务提供者
pom配置
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后在启动类中添加@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
eureka ribbon+hystrix 消费者+断路器
pom配置
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbon</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
想要自动重试必须添加retry依赖
想要自动重试必须添加retry依赖
想要自动重试必须添加retry依赖
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
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)
zuul 网关
pom
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zuul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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