实际项目中不可能只用到一个注册中心,不然注册中心出现宕机等情况,整个项目就有可能崩掉
所以这里来模拟搭建两台eureka作为集群
首先修改yml文件
server:
port: 8888
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8889/eureka
以8888去监听8889的eureka
启动会发现报错,这是因为8889根本就不存在,当然会报错
2020-03-05 22:36:59.669 WARN 5864 --- [nfoReplicator-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: java.net.ConnectException: Connection refused: connect
2020-03-05 22:36:59.669 WARN 5864 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKA-SERVER/192.168.0.103:eureka-server:8888 - registration failed Cannot execute request on any known server
接着咱们来创建8889
点击进去
点击+号选择springboot
修改新建启动项的名称为EurekApp2并将Main class填入与EurekApp一样保存
修改yml文件
server:
port: 8889
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8888/eureka
然后启动刚才配置的第二台注册中心
第一台便不会再报错,注册成功
2020-03-05 22:39:01.096 INFO 5864 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKA-SERVER/192.168.0.103:eureka-server:8888 - Re-registering apps/EUREKA-SERVER
2020-03-05 22:39:01.096 INFO 5864 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKA-SERVER/192.168.0.103:eureka-server:8888: registering service...
查看页面
这样就达到了相互监听的效果达到相互监听的效果
下面来整合rbbion已达到负载均衡的效果
再创建一个provider02,yml文件的端口修改一下
并且消费者与服务者的注册列表都要改为
defaultZone: http://127.0.0.1:8888/eureka,http://127.0.0.1:8889/eureka
为了能区分服务的具体提供者,将二的接口返回代码改为
return bookInfoMapper.selectByPrimaryKey(id+1);
用以返回不同的数据,加以区分
启动两台服务的提供者
下面来修改服务的消费者首先是pom文件
加入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
加上@LoadBalanced注解
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
接口修改
package com.flash.service.impl;
import com.flash.pojo.BookInfo;
import com.flash.service.BookInfoService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
@Service
public class BookInfoServiceImpl implements BookInfoService {
private static final String PROVIDER_NAME="EUREKA-CLIENT-PROVIDER";
private static final String URL_PROVIDER="http://EUREKA-CLIENT-PROVIDER/provider/findById/";
@Autowired
private RestTemplate restTemplate;
@Resource
RibbonLoadBalancerClient loadBalancerClient;
@Override
public BookInfo findById(int id) {
BookInfo bookInfo =
restTemplate.getForObject(URL_PROVIDER+id,BookInfo.class);
return bookInfo;
}
}
启动
然后进行访问
刷新
每次刷新访问的提供者是不同的,这样就达到了负载均衡
还可以通过修改
配置文件
EUREKA-CLIENT-PROVIDER:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
并实现轮询策略来达到随机访问的效果
@Bean
public IRule ribbonRule() {
return new RandomRule(); //这里配置策略,和配置文件对应
}