其他分享
首页 > 其他分享> > 第二篇 服务消费者(rest ribbon)(Finchley版本)

第二篇 服务消费者(rest ribbon)(Finchley版本)

作者:互联网

前言:

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

一、ribbon简介

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

二、准备工作

这一篇文章基于上一篇文章的工程,启动eureka-server 工程;启动service-hi工程,它的端口为8762;将service-hi的配置文件的端口改为8763,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。

三、建一个服务消费者

<?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>
    <parent>
        <groupId>com.gblfy</groupId>
        <artifactId>sc-f-chapter2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.gblfy</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>

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

</project>
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon
@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
public interface HelloService {

    String hiService(String myName);
}

测试实现类HelloServiceImpl

@Service
public class HelloServiceImpl implements HelloService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String hiService(String myName) {

        return restTemplate.getForObject("http://SERVICE-HI//hello?myName="+myName,String.class);
    }
}
@RestController
public class HelloControler {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }
}

在浏览器上多次访问 http://localhost:8764/hello?name=gblfy,浏览器交替显示:

hello,gblfy,我的应用名称是:service-hi,端口是:8762

hello,gblfy,我的应用名称是:service-hi,端口是:8763

四、此时的架构

源码下载:https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter2

标签:String,service,restTemplate,端口,rest,Finchley,hi,ribbon
来源: https://blog.csdn.net/weixin_40816738/article/details/90053609