Spring Cloud : 是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
本文主要讲的是整合方面的东西,所以springcloud讲的不深
1.创建一个空项目,里面加入下面的子模块:
- eureka-server,只需要勾选下面的场景:
然后创建一个application.yml,写下面的配置内容:
server:
port: 8761
eureka:
instance:
hostname: eureka-server # eureka实例的主机名
client:
register-with-eureka: false #不将自己注册到eureka上
fetch-registry: false #不从eureka上来获取服务的注册信息
service-url:
defaultZone: http://localhost:8761/eureka/
然后在springBoot的启动类上标明下面的注解:
然后启动springBoot,然后浏览器地址栏上访问http://localhost:8761/
即可看到下面的可是化的注册中心(由于还没注册服务,所以下面红色框框那个地方自然是空的)
- provider-ticket 和 consumer-user 都需要下面的依赖:
先说provider-ticket:
在application.yml中写下面的配置:
server:
port: 8001
spring:
application:
name: provider-ticket
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka/
书写下面的service:
package org.lzl.providerticket.service;
import org.springframework.stereotype.Service;
@Service
public class TicketService {
public String getTicket(){
System.out.println("8001");
return "卢泽龙最帅!";
}
}
再书写下面的controller:
package org.lzl.providerticket.controller;
import org.lzl.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TicketController {
@Autowired
TicketService ticketService;
@GetMapping("/ticket")
public String getTicket(){
return ticketService.getTicket();
}
}
然后启动这个springboot模块,这这个服务就被注册进去了。
consumer-user:
application.yml的配置信息如下:
spring:
application:
name: consumer-user
server:
port: 8200
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka/
启动类加入下面的注解和bean:
controller如下:
package org.lzl.consumeruser.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/buy")
public String buyTicket(String name){
String s = restTemplate.getForObject("http://PROVIDER-TICKET/ticket",String.class); //第一参数写注册的服务名 第二个参数指定放返回的参数类型
return name+"告诉我:"+s;
}
}
然后也启动这个springboot的模块。
注上面三个模块都启动了,再看http://localhost:8761/ 页面
已经可以看到两个服务的信息了。
测试远程调用:访问http://localhost:8200/buy?name=zs
看到如下的结果则说明成功:
Ribbon介绍
Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现。通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的服务调用。
由下面的图片可以得知:Eureka已经帮我们整合了Ribbon,所以我们不需要引入Ribbon的jar包。
上面使用的 @LoadBalance 会默认用Ribbon作为负载均衡,且默认是以轮循的方式,如果想要修改负载均衡策略则可以参照下面的方法:
- 把包建在主启动类所在包的外面:
package org.lzl.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MySelfRule {
@Bean
public IRule myRule(){
return new RandomRule();//定义随机
}
}
然后主配置类上添加下面的注解:
即可把负载均衡的策略改为随机模式。
自己手写负载均衡算法
第一步:去掉项目中的@LoadBalanced
第二步:写接口
package org.lzl.springcloud.LB;
import org.springframework.cloud.client.ServiceInstance;
import java.util.List;
public interface LoadBalancer {
ServiceInstance instances(List<ServiceInstance> serviceInstances);
}
第三步:写接口的实现
package org.lzl.springcloud.LB;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@Component// 自己手写 Ribbon 的负载均衡算法 轮循
public class MyLB implements LoadBalancer{
private AtomicInteger atomicInteger = new AtomicInteger(0);
public final int getAndIncrement(){
int current;
int next;
do{
current = this.atomicInteger.get();
next = current >= 2147483647 ? 0: current+1;
}while (!this.atomicInteger.compareAndSet(current,next));
System.out.println("******第几次访问,次数next:"+next);
return next;
}
@Override
public ServiceInstance instances(List<ServiceInstance> serviceInstances) {
int index = getAndIncrement() % serviceInstances.size();
return serviceInstances.get(index);
}
}
第四步:使用自己手写的负载均衡
@Autowired
private LoadBalancer loadBalancer;
@GetMapping(value="/consumer/payment/lb")
public String getPaymentLB(){
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
if(instances == null || instances.size() <= 0){
return null;
}
ServiceInstance serviceInstance = loadBalancer.instances(instances);
URI uri = serviceInstance.getUri();
log.info("*******"+uri);
return restTemplate.getForObject(uri+"/payment/lb",String.class);
}
如此就完成了!!!!
注:自己手写的算法不是通过@LoadBalanced注解使用的