(二)spring-cloud入门学习:服务消费rest+ribbon

前面讲了如何将服务注册到Eureka服务端,为了保证服务的高可用,同一个服务都会采用集群形式注册多个节点,那如何去消费这些服务?通过IP+端口的形式明显不合理,那就需要通过服务应用名来进行访问,同时需要能够将请求均衡转发到各节点。而Ribbon就很好地解决了这个问题,Ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。

1. 创建一个服务消费者工程eureka_ribbon, pom.xml文件如下:


  4.0.0

  com.kevin
  eureka_ribbon
  0.0.1-SNAPSHOT
  jar

  eureka_ribbon
  http://maven.apache.org

  
    UTF-8
  

  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.4.RELEASE
  

  
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-ribbon
    
  
  
  
    
        
           org.springframework.cloud
           spring-cloud-dependencies
           Greenwich.SR1
           pom
           import
        
    
  

2. application.properties

spring.application.name=eurekaRibbon
server.port=9201

#注册到eureka
eureka.client.serviceUrl.defaultZone=http://localhost:9101/eureka/

3. Application.java
使用@EnableDiscoveryClient开启服务发现,同时需要申明RestTemplate。

package com.kevin.eureka_ribbon;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
	
    public static void main( String[] args ) {
    	new SpringApplicationBuilder(Application.class)
    		.web(WebApplicationType.SERVLET).run(args);
    }
    
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

4. 创建一个service,HelloService.java
使用RestTemplate进行服务调用

package com.kevin.eureka_ribbon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
	
	@Autowired
	private RestTemplate restTemplate;

	public String sayHello() {
		return restTemplate.getForObject("http://eurekaClient/sayHello", String.class);
	}
}

备注:通过服务注册的name来访问api,该name不能包含下划线,否则报错Request URI does not contain a valid hostname.

5. 创建一个api对外访问,HelloController.java

package com.kevin.eureka_ribbon.controller;

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

import com.kevin.eureka_ribbon.service.HelloService;

@RestController
public class HelloController {
	
	@Autowired
	private HelloService helloService;
	
	@RequestMapping("sayHello")
	public String sayHello() {
		return helloService.sayHello();
	}
}

6. 启动项目并访问http://127.0.0.1:9201/sayHello
在这里插入图片描述

能够正常访问结果,同时查看两个服务client的后台,请求7次,eureka_client9102收到4次请求,eureka_client9103收到3次请求,说明ribbon会将请求负载均衡到各服务端。

你可能感兴趣的:(spring-cloud)