服务提供

我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello xxx,this is first messge”的服务

1、pom包配置

创建一个springboot项目,pom.xml中添加如下配置:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-eureka</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. </dependency>
  11. </dependencies>

2、配置文件

application.properties配置如下:

  1. spring.application.name=spring-cloud-producer
  2. server.port=9000
  3. eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

参数在上一篇都已经解释过,这里不多说。

3、启动类

启动类中添加@EnableDiscoveryClient注解

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class ProducerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ProducerApplication.class, args);
  6. }
  7. }

4、controller

提供hello服务

  1. @RestController
  2. public class HelloController {
  3. @RequestMapping("/hello")
  4. public String index(@RequestParam String name) {
  5. return "hello "+name+",this is first messge";
  6. }
  7. }

添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。

到此服务提供者配置就完成了。

服务调用

1、pom包配置

和服务提供者一致

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-eureka</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. </dependency>
  11. </dependencies>

2、配置文件

application.properties配置如下:

  1. spring.application.name=spring-cloud-consumer
  2. server.port=9001
  3. eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3、启动类

启动类添加@EnableDiscoveryClient@EnableFeignClients注解。

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableFeignClients
  4. public class ConsumerApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ConsumerApplication.class, args);
  7. }
  8. }
  • @EnableDiscoveryClient :启用服务注册与发现
  • @EnableFeignClients:启用feign进行远程调用

Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

4、feign调用实现

  1. @FeignClient(name= "spring-cloud-producer")
  2. public interface HelloRemote {
  3. @RequestMapping(value = "/hello")
  4. public String hello(@RequestParam(value = "name") String name);
  5. }
  • name:远程服务名,及spring.application.name配置的名称

此类中的方法和远程服务中contoller中的方法名和参数需保持一致。

5、web层调用远程服务

将HelloRemote注入到controller层,像普通方法一样去调用即可。

  1. @RestController
  2. public class ConsumerController {
  3. @Autowired
  4. HelloRemote HelloRemote;
  5. @RequestMapping("/hello/{name}")
  6. public String index(@PathVariable("name") String name) {
  7. return HelloRemote.hello(name);
  8. }
  9. }

到此,最简单的一个服务注册与调用的例子就完成了。

测试

简单调用

依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目

先输入:http://localhost:9000/hello?name=neo 检查spring-cloud-producer服务是否正常

返回:hello neo,this is first messge

说明spring-cloud-producer正常启动,提供的服务也正常。

浏览器中输入:http://localhost:9001/hello/neo

返回:hello neo,this is first messge

说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。

负载均衡

以上面spring-cloud-producer为例子修改,将其中的controller改动如下:

  1. @RestController
  2. public class HelloController {
  3. @RequestMapping("/hello")
  4. public String index(@RequestParam String name) {
  5. return "hello "+name+",this is producer 2 send first messge";
  6. }
  7. }

在配置文件中改动端口:

  1. spring.application.name=spring-cloud-producer
  2. server.port=9003
  3. eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

打包启动后,在eureka就会发现两个服务提供者,如下图:

然后在浏览器再次输入:http://localhost:9001/hello/neo 进行测试:

第一次返回结果:hello neo,this is first messge

第二次返回结果:hello neo,this is producer 2 send first messge

不断的进行测试下去会发现两种结果交替出现,说明两个服务中心自动提供了服务均衡负载的功能。如果我们将服务提供者的数量在提高为N个,测试结果一样,请求会自动轮询到每个服务端来处理。

spring cloud(三)服务提供与调用的更多相关文章

  1. Spring Cloud 微服务六:调用链跟踪Spring cloud sleuth +zipkin

    前言:随着微服务系统的增加,服务之间的调用关系变得会非常复杂,这给运维以及排查问题带来了很大的麻烦,这时服务调用监控就显得非常重要了.spring cloud sleuth实现了对分布式服务的监控解决 ...

  2. Spring Cloud(三):声明式调用

    声明式服务调用 前面在使用spring cloud时,通常都会利用它对RestTemplate的请求拦截来实现对依赖服务的接口调用,RestTemplate实现了对http的请求封装处理,形成了一套模 ...

  3. Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】

    Spring Cloud(三):服务提供与调用 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇文章我们介绍了 Eureka 服务 ...

  4. Spring Cloud微服务系列文,服务调用框架Feign

    之前博文的案例中,我们是通过RestTemplate来调用服务,而Feign框架则在此基础上做了一层封装,比如,可以通过注解等方式来绑定参数,或者以声明的方式来指定请求返回类型是JSON.    这种 ...

  5. Spring Cloud 微服务三: API网关Spring cloud gateway

    前言:前面介绍了一款API网关组件zuul,不过发现spring cloud自己开发了一个新网关gateway,貌似要取代zuul,spring官网上也已经没有zuul的组件了(虽然在仓库中可以更新到 ...

  6. Spring Cloud 网关服务 zuul 三 动态路由

    zuul动态路由 网关服务是流量的唯一入口.不能随便停服务.所以动态路由就显得尤为必要. 数据库动态路由基于事件刷新机制热修改zuul的路由属性. DiscoveryClientRouteLocato ...

  7. 【多线程】java多线程Completablefuture 详解【在spring cloud微服务之间调用,防止接口超时的应用】【未完成】

    参考地址:https://www.jianshu.com/p/6f3ee90ab7d3 示例: public static void main(String[] args) throws Interr ...

  8. Dubbo和Spring Cloud微服务架构'

    微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.虽然微服务架构没有公认的技术标准和规范或者草案,但业 ...

  9. Spring Cloud 微服务

    https://mp.weixin.qq.com/s?__biz=MzU0OTE4MzYzMw==&mid=2247486301&idx=2&sn=f6d45860269b61 ...

随机推荐

  1. Intellij IDEA注册激活破解

    1.2017年适用(2016.3.5到2017.2.4版均生效) 安装IntelliJ IDEA 最新版 启动IntelliJ IDEA 输入 license时,选择输入 [License serve ...

  2. spring拦截器-过滤器的区别

    1.  理解 拦截器 :是在面向切面编程的时候,在你的 service 或者一个方法前调用一个方法,或者在方法后调用一个方法:比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业 ...

  3. 解决Windows下文件在Linux下打开出现乱码的问题

    目录 问题 原理 解决 总结 参考资料 问题 前几天生病了,Java一直在看代码但是没跟着打,于是决定偷一波小小的懒,直接把教材的代码从Windows通过共享文件夹放到了Linux里面.但是编译的时候 ...

  4. Eclipse安装fatjar(不用自己下载fatjar包)

    .安装Eclipse-jee-luna-SR2-win32-x86_64版本的插件支持 方法如下: Help -> Install New Software... -> Work with ...

  5. Oracle数据库基础入门《一》Oracle服务器的构成

    Oracle数据库基础入门<一>Oracle服务器的构成 Oracle 服务器是一个具有高性能和高可靠性面向对象关系型数据库管理系统,也是一 个高效的 SQL 语句执行环境. Oracle ...

  6. CSS 步骤进度条

    ;;; } .wizard li {;; text-align: center; line-height: 30px; height: 30px; background-color: #C3C3C3; ...

  7. JavaScript 原型链学习(三)原型对象存在的问题 与 组合使用构造函数和原型

    原型对象也不是没有缺点.首先,它省略了为构造函数传递初始化参数这一环节, 结果所有实例在默认情况下都将取得相同的属性值.虽然这会在某种程度上带来一些不方便, 但还不是原型对象的最大问题.原型对象的最大 ...

  8. transition属性值

    一.transition-property: transition-property是用来指定当元素其中一个属性改变时执行transition效果,其主要有以下几个值:none(没有属性改变):all ...

  9. Python 多进程基本语法

    需求:  在有多线程的情况下,我们可以使用线程帮我们处理一些事情,但是在python这里 由于RSA锁的缘故,我们只能够用到一个cpu帮我们处理事情,一个cpu在处理多个线程时,是通过上下文的切换使我 ...

  10. 如何恢复IIS出厂默认设置

    How to restore IIS settings and Default Web Site? http://superuser.com/questions/704850/how-to-resto ...