【Spring Cloud】三、Eureka Consumer 服务注册中心消费者调用服务

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。

消费者和服务提供者使用同样的eureka服务端注册中心地址,调用注册中心的服务

maven结构如下:

application相关配置如下:

 

spring.application.name=consumer-demo
eureka.client.service-url.defaultZone=http://localhost:8060/eureka

server.port=8080


代码如下:

 

 

package com.chiwei.eureka;

import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Type Bootstrap.java
 * @Desc 
 * @author chiwei
 * @date 2017年11月9日 下午5:24:53
 * @version 
 */
/**
 * @author chiwei
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class Consumer {

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

    /**
     * 主函数入口
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Consumer.class);
        // 不启动web服务
        // app.setWebEnvironment(false);
        app.run(args);
    }

}

@RestController
class HelloController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return restTemplate.getForEntity("http://service-demo/hello", String.class).getBody();
    }
}

/**
 * Revision history
 * -------------------------------------------------------------------------
 * 
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2017年11月9日 chiwei create
 */

 

 

 

启动消费者,浏览器调用服务如下:

调用服务如下:

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值