SpringCloud系列课程第十五课-Ribbon负载均衡Ribbon基础配置

现在所有的服务已经通过了Eureka进行了注册,那么使用Eureka注册的目的是希望所有的服务都统一归属到Eureka之中进行处理,但是现在的问题是所有的微服务都汇集到了Eureka之中,而客户端的调用也应该通过Eureka完成。而这种调用就可以用Ribbon来实现。

Ribb0n是一个服务调用的组件,并且是一个客户端实现负载均衡处理的组件。

【microcloud-consumer-80】修改pom.xml配置文件,追加Ribb0n相关依赖支持包。

spring-cloud-starter-ribbon

【microcloud-consumer-80】修改RestConfig配置类,在获取RestTemplate对象的时候加入Ribb0n的配置。

@Configuration

public class RestConfig {

//要进行一个Http头信息配置

@Bean

public HttpHeaders getHeader() {

HttpHeaders httpHeaders = new HttpHeaders();

String autho = "gwolf:gwolf";

byte[] encodedAuth = Base64.getEncoder().encode(

autho.getBytes(Charset.forName(一种编码)));//进行一个加密处理

//在进行授权的头信息内容配置的时候加密的信息一定要与"Basic"之间有一个空格

String authHeader = "Basic " + new String(encodedAuth);//认证的原始信息

httpHeaders.set("Authorization",authHeader);

return httpHeaders;

}

@Bean

@LoadBalanced

public RestTemplate getRestTemplate() {

return new RestTemplate();

}

}

【microcloud-consumer-80】修改pom.xml配置文件,追加Eureka相关依赖包:

xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

microcloud

com.gwolf

1.0

4.0.0

com.gwolf

microcloud-consumer-80

jar

microcloud-consumer-80

junit

junit

test

com.gwolf

microcloud-api

logback-core

spring-boot-starter-jetty

spring-boot-starter-web

spring-boot-starter-test

test

org.springframework

springloaded

spring-boot-devtools

spring-cloud-starter-ribbon

spring-cloud-starter-eureka

spring-cloud-starter-config

【microcloud-consumer-80】修改application.yml配置文件,追加Eureka的服务注册地址配置:

server:

port: 80

eureka:

client: #客户端进行Eureka注册的配置

service-url:

defaultZone: http://eureka:eureka@eureka-7001.com:7001/eureka,http://eureka:eureka@eureka-7002.com:7002/eureka,http://eureka:eureka@eureka-7003.com:7003/eureka

register-with-eureka: false

【microcloud-consumer-80】修改项目的启动类,追加Eureka客户端的配置注解:

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@EnableEurekaClient

public class Consumer_80_StartSpringCloudApplication {

public static void main(String[] args) {

SpringApplication.run(Consumer_80_StartSpringCloudApplication.class,args);

}

}

【microcloud-consumer-80】修改控制器调用类。

1、现在在Eureka之中注册的所有服务名称都是大写字母。

import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import java.util.List;

@RestController

public class ConsumerDeptController {

public static final String DEPT_GET_URL = "http://MICROCLOUD-PROVIDER-DEPT/dept/get/";

public static final String DEPT_LIST_URL = "http://MICROCLOUD-PROVIDER-DEPT/dept/list/";

public static final String DEPT_ADD_URL = "http://MICROCLOUD-PROVIDER-DEPT/dept/add";

@Resource

private RestTemplate restTemplate;

@Resource

private HttpHeaders httpHeaders;

@RequestMapping(value = "/consumer/dept/get",method = RequestMethod.GET)

public Object getDept(long id) {

//Dept dept = this.restTemplate.getForObject(DEPT_GET_URL + id,Dept.class);

Dept dept = this.restTemplate.exchange(DEPT_GET_URL + id, HttpMethod.GET,

new HttpEntity(this.httpHeaders),Dept.class).getBody();

return dept;

}

@RequestMapping(value = "/consumer/dept/list")

public Object listDept() {

//List allDepts = this.restTemplate.getForObject(DEPT_LIST_URL,List.class);

List allDepts = this.restTemplate.exchange(DEPT_LIST_URL,HttpMethod.GET,

new HttpEntity(this.httpHeaders),List.class).getBody();

return allDepts;

}

@RequestMapping(value = "/consumer/dept/add")

public Object addDept(Dept dept) {

return this.restTemplate.exchange(DEPT_ADD_URL,HttpMethod.POST,

new HttpEntity(dept,this.httpHeaders),Boolean.class).getBody();

}

}

客户端进行访问:http://client.com/consumer/dept/list,这个时候有了Ribb0n和Eureka整合之后用户不再去关注Rest服务的地址与端口号了,所有的信息获取都通过Eureka完成。

  • 发表于:
  • 原文链接:http://kuaibao.qq.com/s/20180124A09V6400?refer=cp_1026

同媒体快讯

相关快讯

扫码关注云+社区