Eureka注册中心
服务名:cloud-eureka-server-7001
注册中心的jar包是:spring-cloud-starter-netflix-eureka-server 注册中心端口号:7001
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父工程-->
<parent>
<groupId>com.mxd</groupId>
<artifactId>cloud-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mxd</groupId>
<artifactId>cloud-eureka-server-7001</artifactId>
<version>1.0-SNAPSHOT</version>
<name>cloud-eureka-server-7001</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--my commons-->
<dependency>
<groupId>com.mxd</groupId>
<artifactId>my-cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--boot actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--devtools-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false # 不向注册中心注册自己
fetch-registry: false # 自己就是注册中心,不需要检索服务
service-url:
defaultZone: hhtp://localhost:7001/eureka/ #注册地址
server:
enable-self-preservation: false #关闭自我保护机制
eviction-interval-timer-in-ms: 2000 #2000ms
启动类
@EnableEurekaServer 开启eureka,使其作为注册中心
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerMain7001.class, args);
}
}
服务提供者1
服务名:cloud-eureka-client-provider-8001
eureka客户端jar包:spring-cloud-starter-netflix-eureka-client,这个包本身包含了Ribbon依赖
端口:8001
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父工程-->
<parent>
<groupId>com.mxd</groupId>
<artifactId>cloud-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mxd</groupId>
<artifactId>cloud-eureka-client-provider-8001</artifactId>
<version>1.0-SNAPSHOT</version>
<name>cloud-eureka-client-provider-8001</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8001
spring:
application:
name: eureka-provider-service
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ #注册地址
启动类
@EnableEurekaClient 开启eureka-client
@SpringBootApplication
@EnableEurekaClient
public class ProviderMain8001 {
public static void main(String[] args) {
SpringApplication.run(ProviderMain8001.class, args);
}
}
Controller
模拟业务层,作为消费者
@RestController
public class ProviderController {
@Value("${server.port}")
private String serverPort;
// 我们这里简单打印此消费者的端口号
@GetMapping("/provider/get/{id}")
public String provider(@PathVariable("id") int id){
return "服务提供者-----port:" + serverPort + " 被调用了";
}
}
服务提供者2
服务名:服务名:cloud-eureka-client-provider-8002
端口号:8002
其他配置与 服务提供者1没有什么区别
消费者
服务名:cloud-eureka-consumer-ribbon-9001
主要jar包:spring-cloud-starter-netflix-eureka-client、spring-cloud-starter-netflix-ribbon
端口号:9001
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父工程-->
<parent>
<groupId>com.mxd</groupId>
<artifactId>cloud-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mxd</groupId>
<artifactId>cloud-eureka-consumer-ribbon-9001</artifactId>
<version>1.0-SNAPSHOT</version>
<name>cloud-eureka-consumer-ribbon-9001</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 9001
spring:
application:
name: eureka-consumer-service-ribbon
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ #注册地址
启动类
开启EurekaClient、同时注入用于服务调用的restTemplate
@SpringBootApplication
@EnableEurekaClient
public class ConsumerMain9001 {
public static void main(String[] args) {
SpringApplication.run(ConsumerMain9001.class, args);
}
@Bean
@LoadBalanced // 赋予RestTemplate负载均衡的能力
RestTemplate restTemplate(){
return new RestTemplate();
}
}
配置轮询规则
@Configuration
public class MySelfRule {
@Bean
public IRule rule(){
// 轮询改为随机
return new RandomRule();
}
}
Controller
@RestController
public class ConsumerController {
// 调用的服务
private String PROVIDER_SERVICE_NAME = "eureka-provider-service";
@Autowired
RestTemplate restTemplate;
@GetMapping("/consumer/provider/get/{id}")
public String consumer(@PathVariable("id") int id){
return restTemplate.getForObject("http://" + PROVIDER_SERVICE_NAME + "/provider/get/"+id , String.class);
}
}
项目结构
测试
开启这四个微服务
查看注册中心 http://localhost:7001/ ,可以看到两个服务:
EUREKA-CONSUMER-SERVICE-RIBBON :该消费者服务只有9001
EUREKA-PROVIDER-SERVICE: 8001、8002
多次请求 http://localhost:9001/consumer/provider/get/1 我们发现端口号在变化,所以我们达到了“负载均衡”的效果
补充:
-
RandomRule:随机策略
-
RoundRobinRule:轮询策略(默认策略)
-
RetryRule:重试策略
-
BestAvailableRule:最低并发策略
-
AvailabilityFilteringRule:可用过滤策略,过滤掉那些因为一直连接失败的被标记为circuittripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值)性能仅次于最低并发策略。
-
WeightedResponseTimeRule:响应时间加权策略,每隔30秒计算一次服务器响应时间,以响应时间作为权重,响应时间越短的服务器被选中的概率越大。
-
ZoneAvoidanceRule:区域权衡策略,Ribbon的负载均衡策略使用建议,一般情况下,推荐使用最低并发策略,这个性能比默认的轮询策略高很多。