第六章构建服务消费者Ribbon

第一步,搭建springboot项目,这个不多说

修改pom.xml文件:

引入:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

具体pom.xml如下:

<project 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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>pers.cc</groupId>
		<artifactId>springCloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>ribbon</artifactId>
	<name>ribbon</name>
	<description>ribbon消费者</description>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>
</project>

第二步,修改配置文件application.properties,如下图,这里就是一个普通的服务消费者,定义服务名称ribbon-consumer,端口号3001和eureka服务的地址http://localhost:1001/eureka/

#配置服务及端口
spring.application.name=ribbon-consumer
server.port=3001
#注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

#从eureka服务器注册表中获取注册信息的时间间隔(s),默认为30秒
eureka.client.registry-fetch-interval-seconds=5

第三步,找到我们的主类,RibbonApplication.java,基本上和前面的服务提供者配置差不多,此处我们定义了一个RestTemplate的模板的bean,这个bean帮助我们调用远程的服务。

@LoadBalanced是一个本地负载均衡的实现,具体的源码探究可以参考下面的链接

https://blog.csdn.net/u011063112/article/details/81295376

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	public static void main(String[] args) {
		SpringApplication.run(RibbonApplication.class, args);
	}
}

第四步,建设我们的controller,来调用其他服务提供者的服务。

package pers.cc.springCloud.ribbon.test.controller;

import java.util.HashMap;
import java.util.Map;

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

import org.springframework.http.ResponseEntity;

@RestController
public class TestCoontroller {
	@Autowired
	RestTemplate restTemplate;

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test() {
		return restTemplate.getForEntity("http://EUREKA-CLIENT/dc", String.class).getBody();
	}


	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add() {
		
		Map<String, Object> urlVariables = new HashMap<String, Object>();
		urlVariables.put("a",new Integer(200));
		urlVariables.put("b",new Integer(210));
		 
		 
		return restTemplate.getForEntity("http://EUREKA-CLIENT/add?a={1}&b={2}", String.class,new Integer(200),new Integer(300)).getBody();
	}
	
	@RequestMapping(value = "/stop", method = RequestMethod.GET)
	public String stop() {
		return restTemplate.getForEntity("http://EUREKA-CLIENT/stop", String.class).getBody();
	}
	
	

	
	
}

 

下面我们看一下效果:

http://localhost:3001/add/

http://localhost:3001/test/

http://localhost:3001/stop/         因为服务提供方里面有一个睡眠5秒钟,所以我们调用下面这个链接的时候,5秒钟返回stop,这里我们只是模拟了关机的一个状态而已

 

好了,ribbon是不是很简单呢,我们是利用

import org.springframework.web.client.RestTemplate;

@Autowired
 RestTemplate restTemplate;

 

   @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

这个注解来获取了一个rest调用的工具类,来帮助我们实现远程的http调用。

 

本章ribbon就讲到这里。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知乎关注八戒来了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值