1. 程式人生 > >SpringCloud服務發現與負載均衡ribbon(三)

SpringCloud服務發現與負載均衡ribbon(三)

SpringCloud學習總結

3、服務發現與負載均衡ribbon

一、服務發現

修改provider8001主啟動類,增加註解@EnableDiscoveryClient

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.
EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient //本服務啟動後會自動註冊進eureka服務中 @EnableDiscoveryClient //服務發現 public class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.
class, args); } }

修改provider8001中的Controller

package com.atguigu.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.atguigu.springcloud.entities.Dept; import com.atguigu.springcloud.service.DeptService; @RestController public class DeptController { @Autowired private DeptService service; @Autowired private DiscoveryClient client; @RequestMapping(value = "/dept/add", method = RequestMethod.POST) public boolean add(@RequestBody Dept dept) { return service.add(dept); } @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET) public Dept get(@PathVariable("id") Long id) { return service.get(id); } @RequestMapping(value = "/dept/list", method = RequestMethod.GET) public List<Dept> list() { return service.list(); } // @Autowired // private DiscoveryClient client; @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET) public Object discovery() { List<String> list = client.getServices(); System.out.println("**********" + list); List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT"); for (ServiceInstance element : srvList) { System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t" + element.getUri()); } return this.client; } }

修改服務消費consumer80的服務處理類

package com.atguigu.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.atguigu.springcloud.entities.Dept;

@RestController
public class DeptController_Consumer
{

	//private static final String REST_URL_PREFIX = "http://localhost:8001";
	private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";

	/**
	 * 使用 使用restTemplate訪問restful介面非常的簡單粗暴無腦。 (url, requestMap,
	 * ResponseBean.class)這三個引數分別代表 REST請求地址、請求引數、HTTP響應轉換被轉換成的物件型別。
	 */
	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping(value = "/consumer/dept/add")
	public boolean add(Dept dept)
	{
		return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
	}

	@RequestMapping(value = "/consumer/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id)
	{
		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/consumer/dept/list")
	public List<Dept> list()
	{
		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
	}

	// 測試@EnableDiscoveryClient,消費端可以呼叫服務發現
	@RequestMapping(value = "/consumer/dept/discovery")
	public Object discovery()
	{
		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
	}

}

傳送請求檢視服務發現的情況 http://localhost:80/consumer/dept/discovery

二、負載均衡ribbon

新建服務提供者provider8001模組

拷貝provider8001的pom檔案以及yml檔案

server:
 port: 8002

mybatis:
 config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置檔案所在路徑
 type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity別名類所在包
 mapper-locations:
 - classpath:mybatis/mapper/**/*.xml                       # mapper對映檔案

spring:
  application:
   name: microservicecloud-dept
  datasource:
   type: com.alibaba.druid.pool.DruidDataSource            # 當前資料來源操作型別
   driver-class-name: com.mysql.jdbc.Driver              # mysql驅動包
   url: jdbc:mysql://localhost:3306/cloudDB02              # 資料庫名稱
   username: root
   password: 123321
   dbcp2:
     min-idle: 5                                           # 資料庫連線池的最小維持連線數
     initial-size: 5                                       # 初始化連線數
     max-total: 5                                          # 最大連線數
     max-wait-millis: 200                                  # 等待連接獲取的最大超時時間

eureka:
 client: #客戶端註冊進eureka服務列表內
   service-url:
     defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
 instance:
   instance-id: microservicecloud-dept8002 #自定義服務名稱資訊
   prefer-ip-address: true     #訪問路徑可以顯示IP地址
#
info:
 app.name: atguigu-microservicecloud
 company.name: www.atguigu.com
 build.artifactId: $project.artifactId$
 build.version: $project.version$

注意:不同的微服務提供者可以連線不同的資料庫!!! 8001連線cloudDB01 8002連線cloudDB02

修改consumer80中的配置類

在注入的RestTemplate 上標註註解@LoadBalanced則可實現負載均衡

package com.atguigu.springcloud.cfgbeans;

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;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RetryRule;

@Configuration
public class ConfigBean 
{ 
	@Bean
	@LoadBalanced //Spring Cloud Ribbon是基於Netflix Ribbon實現的一套客戶端       負載均衡的工具。
	public RestTemplate getRestTemplate()
	{
		return new RestTemplate();
	}
}

啟動三個eureka,啟動兩個provider,啟動consumer,檢視結果
http://localhost:80/consumer/dept/list

三、選擇載均衡演算法與自定義演算法

在consumer80建立一個配置類

在容器中注入相應的演算法類

package com.atguigu.myrule;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;

@Configuration
public class MySelfRule
{
	@Bean
	public IRule myRule()
	{
		//return new RandomRule();// Ribbon預設是輪詢,我自定義為隨機
		//return new RoundRobinRule();// Ribbon預設是輪詢,我自定義為隨機
		
		return new RandomRule_ZY();// 我自定義為每臺機器5次
	}
}

自定義負載均衡演算法

注意:要實現自定義演算法,配置類與演算法類不能與主啟動類在同一個包下!!!,自定義演算法類,繼承AbstractLoadBalancerRule

//迴圈呼叫每個微服務,並且每個呼叫5次
package com.atguigu.myrule;

import java.util.List;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

public class RandomRule_ZY extends AbstractLoadBalancerRule
{
	// total = 0 // 當total==5以後,我們指標才能往下走,
	// index = 0 // 當前對外提供服務的伺服器地址,
	// total需要重新置為零,但是已經達到過一個5次,我們的index = 1
	// 分析:我們5次,但是微服務只有8001 8002 8003 三臺,OK?
	private int total = 0; 			// 總共被呼叫的次數,目前要求每臺被呼叫5次
	private int currentIndex = 0;	// 當前提供服務的機器號

	public Server choose(ILoadBalancer lb, Object key)
	{
		if (lb == null) {
			return null;
		}
		Server server = null;

		while (server == null) {
			if (Thread.interrupted()) {
				return null;
			}
			List<Server> upList = lb.getReachableServers();
			List<Server> allList = lb.getAllServers();

			int serverCount = allList.size();
			if (serverCount == 0) {
				/*
				 * No servers. End regardless of pass, because subsequent passes only get more
				 * restrictive.
				 */
				return null;
			}

//			int index = rand.nextInt(serverCount);// java.util.Random().nextInt(3);
//			server = upList.get(index);

			
//			private int total = 0; 			// 總共被呼叫的次數,目前要求每臺被呼叫5次
//			private int currentIndex = 0;	// 當前提供服務的機器號
            if(total < 5)
            {
	            server = upList.get(currentIndex);
	            total++;
            }else {
	            total = 0;
	            currentIndex++;
	            if(currentIndex >= upList.size())
	            {
	              currentIndex = 0;
	            }
            }			
			if (server == null) {
				/*
				 * The only time this should happen is if the server list were somehow trimmed.
				 * This is a transient condition. Retry after yielding.
				 */
				Thread.yield();
				continue;
			}
			if (server.isAlive()) {
				return (server);
			}
			// Shouldn't actually happen.. but must be transient or a bug.
			server = null;
			Thread.yield();
		}
		return server;
	}
	@Override
	public Server choose(Object key)
	{
		return choose(getLoadBalancer(), key);
	}
	@Override
	public void initWithNiwsConfig(IClientConfig clientConfig)
	{
		// TODO Auto-generated method stub
	}
}

有點模糊,看結構圖
在這裡插入圖片描述

僅僅選擇不同的負載均衡演算法,則直接在ConfigBean裡面 注入相應的類
自定義演算法,則在myrule包下新建配置類與演算法類。
主啟動類在springcloud包下

啟動三個eureka,啟動兩個provider,啟動consumer,再次檢視結果
http://localhost:80/consumer/dept/list

後續:負載均衡feign與Hystrix斷路器…