您当前的位置: 首页 >  ribbon

程序员一灯

暂无认证

  • 0浏览

    0关注

    152博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

二、服务消费者(rest+ribbon)

程序员一灯 发布时间:2021-12-27 14:54:27 ,浏览量:0

🚌一个人可以走的很快,一群人可以走的很远 🎉点赞➕评论➕收藏 ➕关注== 养成习惯(一键四连)📝 🎉欢迎关注💗一起学习👍一起讨论⭐️一起进步📝 🙏作者水平有限,欢迎各位大佬指点,相互学习进步!😆

在上一篇文章,讲了服务的注册和发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。

一、ribbon简介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies. -----摘自官网 https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html

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

二、创建服务注册中心 创建服务端服务端,端口是8762,其他都一样

创建nacos-provider项目,添加SpringCloud nacos相关依赖


        1.8
        2020.0.4
    
    
        
            org.springframework.boot
            spring-boot-starter
        

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

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2021.1
        
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

创建application.yml配置文件

server.port=8762
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

NacosServerApplication增加@EnableDiscoveryClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosServerApplication {

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

}

启动:nacos-provider,在nacos服务中心查看是否启动成功 在这里插入图片描述

创建客户端

创建nacos-consumer为客户端,增加SpringCloud、nacos相关配置 我们使用ribbon+rest,所以还需要加入spring-cloud-loadbalancer


        1.8
        2020.0.4
    
    
        
            org.springframework.boot
            spring-boot-starter
        

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

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2021.1
        

        
            org.springframework.cloud
            spring-cloud-loadbalancer
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

创建application.yml文件,增加如下内容

server.port=8882
spring.application.name=nacos-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

在NacosClientApplication上增加@EnableDiscoveryClient,并且增加负载均衡@LoadBalanced、RestTemplate

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;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }
    
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
三、服务端创建一个服务提供者

在nacos-provider中,创建ProviderController,通过注解@RestController表明自己是一个服务端接口.


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

    @Value("${server.port}")
    String port;

    @GetMapping("/hi")
    public String hi(@RequestParam(value = "name",defaultValue = "Nacos服务端消息",required = false)String name){
        return "服务端【"+port+"】提供消息,客户端传过来的name= "+name;
    }

}

在nacos-provider跟目录下执行:mvn clean package进行打包编译 进入target目录,执行:

java -jar nacos-provider-0.0.1-SNAPSHOT.jar --server.port=8761 java -jar nacos-provider-0.0.1-SNAPSHOT.jar --server.port=8762

测试两个服务

http://localhost:8761/hi?name=小明

服务端【8761】提供消息,客户端传过来的name= 小明

http://localhost:8762/hi?name=小黑

服务端【8762】提供消息,客户端传过来的name= 小黑

两个服务端接口启动成功

四、建一个服务消费者

写一个ConsumerController,并且在controller中通过RestTemplate调用服务端的服务接口hi

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/hi-rest")
    public String hi(@RequestParam String name) {
        return restTemplate.getForObject("http://nacos-provider/hi?name="+name,String.class);
    }
}

在浏览器上多次访问http://localhost:8763/hi-rest?name=小火锅,浏览器交替显示:

服务端【8761】提供消息,客户端传过来的name= 小火锅 服务端【8762】提供消息,客户端传过来的name= 小火锅 在这里插入图片描述 在这里插入图片描述

五、此时的架构

在这里插入图片描述 从这里可以看出来,Nacos主要是管理服务的地址、转发请求

关注
打赏
1645367472
查看更多评论
立即登录/注册

微信扫码登录

0.0426s