SpringCloud 服务消费者-rest+ribbon

阅读数:72 评论数:0

跳转到新版页面

分类

python/Java

正文

一、概述

spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign.

二、ribbon

ribbon是一个负载均衡客户端,主要功能是负载均衡,负责从多个Feign默认集成了ribbon。ribbon已经默认实现了如下配置bean:

(1) IClientConfig ribbonClientConfig: DefaultClientConfigImpl

(2)IRule ribbonRule: ZoneAvoidanceRule

(3)IPing ribbonPing: NoOpPing

(4)ServerList ribbonServerList: ConfigurationBasedServerList

(5)ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

(6)ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

三、实战

基于前上篇文章,启动eureka-server工程,启动server-hi工程,它的端口为8762,将server-hi的配置文件的端口改为8763,再启动一个server-hi。

1、如何在idea启动多个spring boot工程实例

(1)在idea点击Application的右下角,弹出选项后,点击Edit Configuration.

(2)打开配置后,将默认的Single instance only的钩去掉。

2、建一个服务消费者

重新新建一个spring-boot工程,取名为:service-ribbon。

在它的pom.xml继承父pom文件,并引入以下依赖。

<?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.forezp</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>
 
 
    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter2</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>
 
 
 
</project>

在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为service-ribbon,程序端口为8764.

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon

在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册,并且向程序的ioc注入一个bean:restTemplate,并通过@LoadBalanced注解表明这个restTemplate开启负载均衡的功能。

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {
 
    public static void main(String[] args) {
        SpringApplication.run( ServiceRibbonApplication.class, args );
    }
 
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
 
}

写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用程序名称代替具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例。

@Service
public class HelloService {
 
    @Autowired
    RestTemplate restTemplate;
 
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }
 
 
}

写一个controller,在controller中调用HelloService的方法

@RestController
public class HelloControler {
 
    @Autowired
    HelloService helloService;
 
    @GetMapping(value = "/hi")
    public String hi(@RequestParam String name) {
        return helloService.hiService( name );
    }
}

在浏览器上多次访问http://localhost:8764/hi?name=xxx




相关推荐

@PathParam的声明允许你在URI路径中去映射你的方法将使用的参数。 @Path("/library") pu

ZuulProxyAutoConfiguration 首先我们看一下zuul的配置类ZuulProxyAutoConfiguration, 这个类有一项工作是初始化Zuul默认

Tomcat Tomcat的最大并发数是可以配置的,实际运用中,最大并发数与硬件性能有很大关系的。Tomcat默认的HTTP实现是采用阻塞式的Socket通信,每个请求都需要创

Eureka Server在运行期间会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85

共同点 都是用来表示Spring某个类是否可以接收HTTP请求。 不同点 @Controller标识一个spring类是Spring MVC c

mvn依赖 &lt;dependency&gt; &lt;groupId

什么是jwt (json web token)jwt是一生中用来在网络上声明某种身份的令牌(TOKEN),它的特点是紧凑且自包含并且基于JSON,通过一些常用的算法对包含的主体

Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图表化界面。 修改service-hi 1、在pom工程文件引入相应的依赖</

简介 Spring cloud Sleuth主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持zipkin,你只需要在pom文件中引入相应的依赖即可。 1、

简介 在spring cloud中,有分布式配置中心组件spring cloud config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程git仓库中,在该组