记录一下Ribbon+Eureka实现负载均衡初次练习
首先是service
主配置文件
server.port=9100
#设置该服务注册中心的hostname,也是域名,可以自己去设置
eureka.instance.hostname=localhost
#由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种自己向自己注册的默认行为
eureka.client.register-with-eureka=false
#表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它不需要去检索其他服务
eureka.client.fetch-registry=false
#指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
记得在启动类上加上@EnableEurekaServer注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
直接在浏览器地址栏访问 localhost:9100 就可以进入注册中心
实现负载均衡的轮询策略需要多个Provider
Provider01
#指定服务名字 这个名称将在服务消费者时被调用
spring.application.name=010-springcloud-eureka-client-provider
#指定eureka的访问地址
eureka.client.service-url.defaultZone=http://localhost:9100/eureka
#指定Tomcat的端口号需要避免和其他的Tomcat端口冲突
server.port=8081
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class providerController {
@RequestMapping(value = "/test")
public String test(){
return "8081提供者01的ribbon测试";
}
}
Provider02
#指定服务名字 这个名称将在服务消费者时被调用
spring.application.name=010-springcloud-eureka-client-provider
#指定eureka的访问地址
eureka.client.service-url.defaultZone=http://localhost:9100/eureka
#指定Tomcat的端口号需要避免和其他的Tomcat端口冲突
server.port=8082
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class providerController {
@RequestMapping(value = "/test")
public String test(){
return "8082提供者02的ribbon测试";
}
}
两个提供者的主配置文件除了端口号完全一样。spring.application.name=010-springcloud-eureka-client-provider
这个name后面的值是随意的,需要一个唯一的名字。两个提供者的name一样
最后需要一个消费者访问
Consumer
#将自己注册到注册中心
spring.application.name=010-springcloud-eureka-ribbon-consumer
#将服务同时注册到多个注册中心,其实当Eureka注册中心实现集群以后,
# 那么将服务注册到任意一个Eureka的注册中心后,数据服务都会同步到其他的Eureka注册中心,
# 但是实际应用时还是建议将服务分别注册到全部的Eureka集群的所有服务中,
# 这个防止某个以Eureka出现故障后服务仍然可以注册成功
eureka.client.service-url.defaultZone=http://localhost:9100/eureka
#指定Tomcat的端口号需要避免和其他的Tomcat端口冲突
server.port=8080
#每间隔2s,向服务端发送一次心跳,证明自己依然"存活"
eureka.instance.lease-renewal-interval-in-seconds=2
#告诉服务端,如果我10s之内没有给你发心跳,就代表我故障了,将我踢出掉
eureka.instance.lease-expiration-duration-in-seconds=10
需要一个配置类
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;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class providerController {
@Resource
private RestTemplate restTemplate;
@RequestMapping(value = "/test")
public String test(){
ResponseEntity<String>result=restTemplate.getForEntity("http://010-SPRINGCLOUD-EUREKA-CLIENT-PROVIDER/test",String.class);
String body=result.getBody();
return "8080消费者的ribbon测试,使用了负载均衡"+body;
}
}
最后将所有的服务一一启动。先启动服务,再启动提供者,最后消费者
这里是两个提供者。
然后访问消费者的地址,他会一次的去访问提供者1和提供者2
再次访问这个地址
依次访问两个提供者。