- 生成eurekaServer端服务注册中心
- 建module
- 改POM
<!--加入Eureka server依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
-
写YML
server: port: 7001 eureka: instance: hostname: localhost client: # 表示是否在注册中心注册eureka本身 register-with-eureka: false # 表示是否从Eureka Server获取注册的服务信息 fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}
-
主启动
@SpringBootApplication @EnableEurekaServer public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class,args); } }
- 将EurekaClient端cloud-provider-payment8001注册进EurekaServer成为服务提供者provider
- 改POM
<!--Eureka client依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
写YML
eureka: client: register-with-eureka: true # 获取已经注册的信息,默认是true,单节点的时候无所谓,但是如果是集群,必须设置true才能配合ribbon使用负载均衡 fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka/
-
主启动
@SpringBootApplication @EnableEurekaClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
-
注册成功
-
将EurekaClient端cloud-consumer-order80注册进EurekaServer成为消费者consumer
-
改POM
<!--Eureka client依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
写YML
eureka: client: register-with-eureka: true # 获取已经注册的信息,默认是true,单节点的时候无所谓,但是如果是集群,必须设置true才能配合ribbon使用负载均衡 fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka/
-
主启动
@SpringBootApplication @EnableEurekaClient public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class,args); } }
-
注册成功
- 为了实现高可用,搭建注册中心集群,实现负载均衡、故障容错:
- 各个注册中心之间,互相注册,相互守望;
- 先建第二个注册中心:
- 建module(第二个注册中心)
- 改POM(和eureka7001相同)
- 修改映射文件:找到C:\Windows\System32\drivers\etc 路径下的hosts文件;修改映射配置添加进hosts文件127.0.0.1 eureka7001.com、127.0.0.1 eureka7002.com
- 写YML(关键)
server: port: 7001 eureka: instance: hostname: eureka7001 client: # 表示是否在注册中心注册eureka本身 register-with-eureka: true # 表示是否从Eureka Server获取注册的服务信息 fetch-registry: true service-url: defaultZone: http://eureka7002:7002/eureka/ server: port: 7002 eureka: instance: hostname: eureka7002 client: # 表示是否在注册中心注册eureka本身 register-with-eureka: true # 表示是否从Eureka Server获取注册的服务信息 fetch-registry: true service-url: defaultZone: http://eureka7001:7001/eureka/ ####注意:如果这两个register-with-eureka 和 fetch-registry不改为true的话 会导致注册中心变成unavailable-replicas
-
搭建成功
- 将两个微服务注册进Eureka集群
- 修改YML文件即可:
service-url: defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka
- 配置服务集群:
- 建第二个服务module
- 修改YML文件中的端口号
server: port: 8002
-
如何做到轮询这两个服务呢?将消费者端的请求地址改为通过服务名称去获取:
public static final String PAYMENT_SRV = "http://CLOUD-PAYMENT-SERVICE";
并且将RestTemplate的配置类的方法上加上负载均衡的注解@LoadBalanced
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
这就是Ribbon的负载均衡的功能