1、说明

springcloud中由服务消费者调用服务提供者一共有两种方法rest和feign

2、feign

(1)使用feign的方式进行服务调,搭建服务提供者。

  1. 创建一个web项目(服务提供者)
  2. 修改pom文件
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  3. 在项目启动类上加@EnableDiscoveryClient注解
  4. 添加配置文件
    spring.application.name=spring-cloud-producer
    server.port=9000
    #将服务注册的地址
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  5. 编写测试代码
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController; @RestController
    public class HelloController {
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
    return "这是服务提供者,参数:"+name;
    }
    }

6、效果

(2)使用feign的方式进行服务调,搭建服务消费者。

  1. 创建一个web项目(服务消费者)
  2. 修改pom文件
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
    <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.4.RELEASE</version>
    </dependency>
  3. 在项目启动类上加@EnableDiscoveryClient 启动服务注册和发现 @EnableFeignClients 启用feign进行远程调用 注解
  4. 添加配置文件
    spring.application.name=spring-cloud-consumer
    server.port=9001
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  5. 编写测试代码

5.1编写调用接口

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; // name的值是服务提供者的配置文件中的spring.application.name
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
String hello(@RequestParam(value = "name") String name);
}

5.2编写调用类

import com.comsuer.comsuer.Service.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class helloController {
@Autowired
private HelloRemote helloRemote; @RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name);
} }

6. 效果

 3、rest

springcloud使用rest+ribbon实现服务调用和服务提供者的负载均衡

(1)搭建服务提供者

  1. 创建一个web项目
  2. 修改pom文件
    <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>
  3. 在启动类上加@EnableDiscoveryClient 注解
  4. 添加配置文件
    spring.application.name=spring-cloud-producer
    server.port=9000
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  5. 编写测试代码
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController; @RestController
    public class HelloController {
    @PostMapping("/hello")
    @ResponseBody
    public String index(@RequestBody String name) {
    return "第一个提供者"+name;
    }
    }
  6. 按照上面的五个步骤再构建一个服务提供者

(2)搭建服务消费者

  1. 创建一个web项目
  2. 修改配置文件
    <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>
    <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.49</version>
    </dependency>
  3. 在启动类上添加@EnableDiscoveryClient 注解,并修改启动类的代码,修改如下
    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 ComsuerApplication { public static void main(String[] args) {
    SpringApplication.run(ComsuerApplication.class, args);
    }
    // 实现负载均衡
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
    return new RestTemplate();
    }
    }
  4. 添加配置文件
    spring.application.name=spring-cloud-consumer
    server.port=9001
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  5. 编写测试代码
    import com.alibaba.fastjson.JSON;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate; @RestController
    public class helloController { @Autowired
    private RestTemplate rest;
    @PostMapping("/hello")
    @ResponseBody
    public String hello(String name){
    System.err.println(name);
    String url = "http://spring-cloud-producer/hello";
    User user = new User();
    user.setName(name);
    user.setId("1");
    String s1 = JSON.toJSONString(user);
    String s = rest.postForObject(url, s1, String.class);
    return s;
    }
    }

rest调用效果,会调一次一,调一次二

4.总结

feign方式的负载均衡和rest步骤基本一样。

springcloud的服务提供者与服务消费者的更多相关文章

  1. 为什么Eureca Client要分成服务提供者和服务消费者呢?

    [学习笔记]转载 6)为什么Eureca Client要分成服务提供者和服务消费者呢? 通 常来讲,服务提供方是重量的耗时的,所以可能在n台机器上.而服务消费方是轻量的,通过配置ribbon和@Loa ...

  2. spring cloud微服务架构 服务提供者和服务消费者

    服务提供者和服务消费者 下面这张表格,简单描述了服务提供者/消费者是什么:   | 名词 | 概念 | | ----- | ----------------------- | | 服务提供者 | 服务 ...

  3. Dubbo搭建HelloWorld-搭建服务提供者与服务消费者并完成远程调用(附代码下载)

    场景 Dubbo简介与基本概念: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555224 Dubbo环境搭建-ZooKe ...

  4. SpringCloud系列二:硬编码实现简单的服务提供者与服务消费者

    从本文开始,以一个电影售票系统为例讲解Spring Cloud 1. 版本 jdk:1.8 SpringBoot:2.0.0.RELEASE SpringCloud:Finchley.M8 2. 系统 ...

  5. 《springcloud 一》搭建注册中心,服务提供者,服务消费者

    注册中心环境搭建 Maven依赖信息 <parent> <groupId>org.springframework.boot</groupId> <artifa ...

  6. SpringCloud(2)服务消费者(rest+ribbon)

    1.准备工作 这一篇文章基于上一篇文章的工程.启动eureka-server 工程,端口为 8761.分别以端口 8762 和 8763 启动 service-hi 工程.访问 localhost:8 ...

  7. SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

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

  8. SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

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

  9. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/ 本文出自方志朋的博客 在上一篇文章,讲了 ...

随机推荐

  1. 使用CEfSharp之旅(4)cefsharp 调用F12

    原文:使用CEfSharp之旅(4)cefsharp 调用F12 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https://bl ...

  2. CNN网络中的不变性理解

    神经网络中的不变性 原文:https://blog.csdn.net/voxel_grid/article/details/79275637     个人认为cnn中conv层对应的是“等变性”(Eq ...

  3. CodeForces 258D Little Elephant and Broken Sorting(期望)

    CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...

  4. LoadRunner内部结构(2)

    LoadRunner内部结构(2) 接着(1)的内容: 17.默认的LRReport文件夹创建在本地分析机器的My Documents文件夹下来存储分析会话文件. 18.可以使用HTML格式 19.格 ...

  5. 多线程MT和多线程MD的区别

    这段时间司在招实习生,而不管是远程的电话面试或者是实际现场面试中领导都喜欢问你这个问题,但是可惜的是能很好答上来的人很少.后来发现不管是应届的实习生,甚至有些实际参加工作几年的人也未必真的了解这个问题 ...

  6. springboot与分布式(zookeeper+dubbo)

    docker安装zookeeper命令: docker pull zookeeper:3.4.14 docker启动zookeeper命令: docker run --name zk01 -p 218 ...

  7. CSS自动换行、强制不换行、强制断行、超出显示省略号

    转自:https://blog.csdn.net/liuyan19891230/article/details/50969393 P标签是默认是自动换行的,因此设置好宽度之后,能够较好的实现效果,但是 ...

  8. System.Web.Mvc.FileStreamResult.cs

    ylbtech-System.Web.Mvc.FileStreamResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...

  9. nginx+supervisor 前后端分离项目的发布流程

    [第一部分] 前端发布(vue项目),假设项目名为demo_vue Step1:编译打包前端项目 cd到demo_vue目录下, 执行cnpm run build:prod命令,生成disc文件夹 S ...

  10. 进程外Session---数据库Session的配置

    怎么将Session记录到数据库中呢? 其实微软已经为我们设置好了.只要我们简单的配置一下就行了. 当然也要在 “我的电脑” 鼠标右键--->管理--->服务和应用程序----> 服 ...