SpringCloud入门教学|第二篇:服务消费者(restTempleate+ribbon)

上一篇我们讲解了项目的构建和服务的注册和发现。现在我们再增加一个model。来了解ribbon+restTemplate

ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。

注册两个实例

上一篇我们的service-client 启动后,我们需要改变其端口为8763 再启动一次,然后我们发现我们的Eureka的页面中如下,出现了一个伪集群
这里写图片描述

建立一个消费者

首先我们需要创建我们的model。并将项目起名为service-ribbon.

这里写图片描述

接下来我们需要在pom声明我们的父pom,同时声明spring-cloud-starter-ribbon、spring-boot-starter-web依赖

<?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.example</groupId>
    <artifactId>service-ribbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-ribbon</name>
    <description>Demo project for Spring Boot</description>

    <!-- 指定父pom ,此时的父POM中有spring-cloud-starter-eureka的依赖-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR2</spring-cloud.version>
    </properties>

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


    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
server.port= 8764
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.application.name=service-ribbon
  • 在工程的入口类中,我们需要声明@EnableDiscoveryClient向服务中心注册,并且向程序IOC 注入一个restTemplate的Bean。 @LoadBalanced注解表明这个restRemplate开启负载均衡功能。
@SpringBootApplication
@EnableDiscoveryClient  // @EnableDiscoveryClient向服务中心注册
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced // @LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

}
  • 接下来我们写一个测试类HelloService,通过IOC容器的restTemplate来消费service-hi服务的”/hello”接口,在这里我们用程序名替代具体的url地址,在ribbon中会根据服务名来选择具体的服务实例。
@Service
class HelloService {
    @Autowired
    private lateinit var restTemplate: RestTemplate

    fun helloService(name: String): String {
        return restTemplate.getForObject("http://SERVICE-HI/hello?name=$name",String::class.java)
    }

}
  • 写一个contoller来调用HelloService方法。
@RestController
class HelloController {
    @Autowired
    private lateinit var helloService: HelloService
    @RequestMapping("/hi")
    fun hello(@RequestParam name: String): String {
        return helloService.helloService(name)
    }

}
hi houshuai,I am from port:8763

hi houshuai,I am from port:8762

此项目架构

这里写图片描述

  • 8761端口为一个服务器注册中心
  • 8762 和8763 分别向服务中心注册的 service-hi的两个实例
  • 8764为向服务中心注册的service-ribbon
  • 当service-ribbon 通过restTemplate调用service-hi的hi接口时,因为用了ribbon进行了负载均衡,会轮流焦勇8762和8763这两个端口的hello接口

源码地址
https://github.com/houshuai0816/SpringCloudDemo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值