springCloud - 第3篇 - 消费者调用服务 ( RestTemplate + Ribbon )

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

一、新建 ribbon 工程:

1. file - new - module 

2. spring Initializr - module SDK 选择自己的 JDK ,其余的可以不用填写,next。

3. 填写工程相关信息:包名、工程名等,next。

4. spring cloud discovery - 勾选 eureka discover client,next。

或  spring cloud routing - 勾选 ribbon,next。(此步这 2 种都可)

5. 工程名,代码存放位置等,finish 。

6. 工程结构如下:

7. pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>com.ribbon</groupId>
    <artifactId>service-ribbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>service-ribbon</name>
    <description>服务消费 ribbon 方式</description>

    <parent>
        <groupId>com.base</groupId>
        <artifactId>base-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-ribbon</artifactId>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>


8. 在工程启动类上加注解:@EnableDiscoveryClient  。

关于 2 个注解的区别见文章:springcloud 注解 @EnableDiscoveryClient 与 @EnableEurekaClient 的区别

package com.ribbon.serviceribbon;

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;

// 标明自已为服务
@EnableDiscoveryClient

@SpringBootApplication
public class ServiceRibbonApplication {


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

    /**
     * 向 ioc 注入 bean : restTemplate;
     * 注解 @LoadBalanced :此 bean 开启负载均衡。
     * @return
     */
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

实现对于生产者服务的调用:

SeeParamService

package com.ribbon.serviceribbon;

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

/**
 * @author yujiang
 * @description
 * @date 2019/7/22 11:45
 */
@Service
public class SeeParamService {

    @Autowired
    RestTemplate restTemplate;

    public String seeService(String param) {
        return restTemplate.getForObject("http://see-param/seeParam?param=" + param, String.class);
    }

}

 SeeParamController:

package com.ribbon.serviceribbon;

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

/**
 * /**
 *
 * @author yujiang
 * @description
 * @date 2019/7/22 13:27
 */
@RestController
public class SeeParamController {


    @Autowired
    SeeParamService seeParamService;

    @RequestMapping(value = "/seeParam")
    public String see(@RequestParam String param) {
        return seeParamService.seeService(param);
    }
}

9. 配置文件相关设置:



# 注册中心 - 端口: 1234、工程名: eureka (见 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/

# 端口
server.port= 8701

# 工程名
spring.application.name= ribbon

10.启动工程:

 

11. 浏览器访问:  http://localhost:8701/seeParam?param=参数啊 。刷新多次请求,得到不同端口服返回的结果 。

 

12. 从注册中心可知,当前注册了4 个服务,访问注册中心:http://localhost:1234/

 

13. 不断刷新 ribbon 工程访问地址,可见 8801、8802、8803 都有请求到。图见第 11 点。

说明 负载均衡 已实现,消费者(服务请求方应用)请求到了不同的生产者(服务提供方应用)。

14.总结:

此时 整个工程体系为:

1个注册中心:eureka 工程,端口:1234 。

3个生产者 see-param ,分别占用端口:8801、8802、8803 ,三者都向 eureka  注册,暴露自已提供的服务。

1个消费者 ribbon 工程,端口:8701 。向 eureka 注册 ,订阅自已所需要的服务。

ribbon 有作负载均衡,故 在调用生产者服务时,可轮流请求到不同的生产者服务。

------------------------------------------------------------------------------

遇到 问题1:

解决方法见文章:解决:There was an unexpected error (type=Internal Server Error,..). No instances available for XXX

 

遇到 问题2 :

解决方法见文章:解决:Whitelabel Error Page This application has no explicit mapping for /error...UnknownHostException

------------------------------------------------------------------------

下一篇:springCloud - 第4篇 - 消费者调用服务 ( Feign )

源码见:https://gitee.com/FJ_WoMenDeShiJie/springcloud-ribbon

--------------------------------------------------------------

PS:这个系列不定时更新,只是个人的学习分享,

内容全程参考书目:

《Spring Cloud 与 Docker 微服务架构空实战 》、

《Spring Cloud 微服务实战》及此书作者博客:http://blog.didispace.com/spring-cloud-learning/

《深入理解 Spring Cloud 与微服务构建》及此书作者博客:https://blog.csdn.net/forezp/article/details/70148833
--------------------------------------------------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值