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);
}*/
}