spring cloud ribbon 消费

2018-03-01 11:25:25来源:oschina作者:Germmy人点击

分享

步骤:


1、新建maven项目


2、pom如下:


<?xml version="1.0" encoding="UTF-8"?>
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">
4.0.0
com.mzjf.ribbonconsumer
ribbonconsumer
1.0-SNAPSHOT

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




org.springframework.cloud
spring-cloud-starter-eureka


org.springframework.boot
spring-boot-starter-web


org.springframework.cloud
spring-cloud-starter-ribbon




org.springframework.cloud
spring-cloud-dependencies
Dalston.SR1
pom
import



3、新建spring boot启动类ConsumerApplicaiton


package com.mzjf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
/**
* Created by germmy on 18/2/27.
*/
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
returnnew RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}

4、新建ConsumerController


package com.mzjf;
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;
/**
* Created by germmy on 18/2/27.
*/
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;@RequestMapping(value="ribbon-consumer",method= RequestMethod.GET)
publicString helloConsumer(){
returnrestTemplate.getForEntity("http://EUREKA-CLIENT-GERMMY/dc",String.class).getBody();
}}

5、新建application.yml文件


server:
port: 9000
spring:
application:
name: ribbon-consumer
eureka:
instance:
hostname: localhost
port: 8081
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/

6、启动后,在控制台看到如下图


最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台