SpringCloud------Ribbon特性脱离Eureka做负载均衡

本文探讨了在微服务架构中不使用Eureka服务发现的情况,介绍了如何通过直接配置服务实例列表和自定义负载均衡策略来实现服务间的调用。文中详细展示了在Spring Cloud Ribbon中关闭Eureka功能的具体配置,以及如何使用RestTemplate和LoadBalancerClient进行服务调用。
摘要由CSDN通过智能技术生成

1、什么情况不需要Eureka服务端,脱离它,直接访问微服务端集群?

???

2、application.yml中关闭Eureka功能

配置文件中没有eureka的zoneDefaultUrl,关闭eureka功能

server:
  port: 80

ribbon:
  eureka:
    enabled: false
  
dept-8001:
  ribbon:
    listOfServers: http://dept-8001.com:8001,http://dept-8002.com:8002,http://dept-8003.com:8003

3、RestConfig中的注解@LoadBalanced去掉

package com.zemel.consumer.config;

import java.nio.charset.Charset;
import java.util.Base64;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {
	
	@Bean
	public HttpHeaders getHeaders(){
		// 进行一个Http头信息配置
		HttpHeaders headers = new HttpHeaders();
		String auth = "wendy:wendy";
		byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));
		// 加密字符串要有空格
		String authHeader = "Basic " + new String(encodedAuth);
		
		headers.set("Authorization", authHeader);
		return headers;
	}

	@Bean
//	@LoadBalanced
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}

}

4、MyBalanceConfig.java

package com.zemel.common;

import org.springframework.context.annotation.Bean;

import com.netflix.loadbalancer.IRule;

// 该类无法被主main扫描到
public class MyLoadBalanceConfig {

	@Bean
	public IRule ribbonRule(){
		
		return new com.netflix.loadbalancer.RandomRule();
	}
}

5、Main启动类

package com.zemel.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class ConsumerRibbonApp {
	public static void main(String[] args) {
		SpringApplication.run(ConsumerRibbonApp.class, args);
	}
	
	
}

6、控制器

通过LoadBalancerClient类获取到服务的主机名和端口信息

package com.zemel.consumer.controller;

import java.net.URI;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.zemel.common.MyLoadBalanceConfig;
import com.zemel.vo.Dept;

@RestController
@RequestMapping("/consumer/dept")
@RibbonClient(name="dept-8001", configuration=MyLoadBalanceConfig.class)
public class DeptController {
	
	static final String DEPT_REST_TOPIC = "dept-8001";
	
	@Resource
	private RestTemplate restTemplate;
	
	@Autowired
	private LoadBalancerClient client;
	
	@Resource
	private HttpHeaders headers;

	@GetMapping("/get")
	public Object getDept(long id){
		
		ServiceInstance instance = client.choose(DEPT_REST_TOPIC);
		
		System.out.println("host="+instance.getHost()+"--port:"+instance.getPort() + "--serviceId:"+instance.getServiceId()
		+ "  --uri:" + instance.getUri());
		
		Dept dept = null;
		
		URI deptUri = URI.create(String.format("http://%s:%s/dept/get/"+id, instance.getHost(),instance.getPort()));
		
//		return dept; 
		return this.restTemplate.exchange(deptUri, HttpMethod.GET, new HttpEntity(headers), Dept.class);
		
//		return this.restTemplate.exchange(DEPT_GET_URL+id, HttpMethod.GET, new HttpEntity(headers), Dept.class);
//		return this.restTemplate.getForEntity(DEPT_GET_URL+id, Dept.class);
	}
	
	/*@GetMapping("/list")
	public Object list(){
		return this.restTemplate.exchange(DEPT_LIST_URL, HttpMethod.GET, new HttpEntity(headers), List.class);
//		return this.restTemplate.getForObject(DEPT_LIST_URL, List.class);
	}
	
	@GetMapping("/add")
	public Object add(Dept dept){
		return this.restTemplate.exchange(DEPT_ADD_URL, HttpMethod.POST, new HttpEntity(dept,headers), Boolean.class);
//		return this.restTemplate.postForObject(DEPT_ADD_URL, dept, Boolean.class);
	}*/
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值