前文
服务注册
- 图中的 7001 和 7002 代表的是 Eureka 的 Server 端
- 80 和 8001 代表的是 Eureka 的 Client 端
- 其中 8001 代表的是提供者,80 代表的是消费者(图中的数字表示的是项目的端口号)
主要是修改 application.yml 文件即可
修改 80 项目的 yml 文件
server:
port: 80
spring:
application:
# 服务名称
name: demo-order-service
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true。单节点无所谓,集群必须设置为 true 才能配合 Ribbon 使用负载均衡
fetch-registry: true
service-url:
# 注册地址
# defaultZone: http://localhost:7001/eureka
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
这里注册地址不再是一台 Eureka ,而是两台
修改 8001 项目的 yml 文件
server:
port: 8001
spring:
application:
name: demo-provider-payment
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true,单节点无所谓,集群必须设置为 true 才能配合 Ribbon 使用负载均衡
fetchRegistry: true
service-url:
# 注册地址
# defaultZone: http://localhost:7001/eureka
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
跟 80 的一样
启动 EurekaServer
先后启动 7001 ,7002,然后再启动 80,8001
访问 http://eureka7001.com:7001/
访问 http://eureka7002.com:7002/
多个服务注册
也就是说要弄一个跟 8001 一样的服务,然后修改端口为 8002,如下:
server:
port: 8002 # 端口号
spring:
application:
name: demo-provider-payment
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true,单节点无所谓,集群必须设置为 true 才能配合 Ribbon 使用负载均衡
fetchRegistry: true
service-url:
# 注册地址
# defaultZone: http://localhost:7001/eureka
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
现在有两个提供者 8001 和 8002,但是 80 服务怎么知道调的是哪个呢?
可以在 8001 和 8002 添加以下代码,当去浏览器查询的时候,就可以知道是谁提供的服务
启动服务
先后启动 7001 和 7002 服务,然后再启动 8001 、8002 和 80 服务
访问 http://eureka7001.com:7001/ 和 http://eureka7002.com:7002/ 均能访问到 Eureka 的主页
访问 http://localhost:8001/payment/get/1和 http://localhost:8002/payment/get/1 均能访问到数据
使用 http://localhost/consumer/payment/get/1 也可以访问
但是访问的只有 8001 的服务,并没有达到下面这张图的效果
原因是因为在 80 的控制器中固定了访问的地址,
因为上面定义了 8001 和 8002 的服务名称都是一样的
那么可以使用服务名称替换掉原来固定的 URL,如下:
public static final String PAYMENT_URL = "http://DEMO-PROVIDER-PAYMENT";
重启 80 服务,重新访问 http://localhost/consumer/payment/get/1 的时候报错了,说不知道主机异常
因为设置了使用服务名称来调用服务,但是由于 8001 和 8002 的服务名称是一样的,80 服务不知道要找哪个服务,所以需要在 80 的配置类中加一个 @LoadBalance 注解
package com.java.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author Woo_home
* @create 2020/3/24 14:16
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced // 使用 @LoadBalance 注解赋予 RestTemplate 负载均衡的能力
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
重启 80 服务,重新访问 http://localhost/consumer/payment/get/1,访问成功
多刷新几次会发现访问的端口不一样
这样就完成了一个默认的轮询的负载机制,简单地说就是 8001 和 8002 轮流地返回结果给 80 服务
主机名称修改
eureka:
instance:
instance-id: payment8002 # 在 Eureka 页面显示的主机名称
perfer-ip-address: true # 访问路径可以显示 IP 地址
代码已上传至码云,感兴趣的朋友可以去下载来玩下 码云地址 https://gitee.com/WooGitee/SpringCloud