springcloud-Netflix创建服务消费者

在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的。Spring cloud 有两种服务调用方式,两种都是http方式的不是rpc方式

一种是 ribbon + restTemplate

另一种是 feign。首先讲解下基于 ribbon + rest

Ribbon

创建服务消费者-Ribbon方式

Ribbon 是一个负载均衡客户端,可以很好的控制 http 和 tcp 的一些行为。

创建方式也是,在总工程下面创建maven工程包,导入pom.xml,application.yml,Application入口类;以及编写自己的业务逻辑

1.创建一个 hello-spring-cloud-web-admin-ribbon 服务消费者项目

POM.XML,导入springboot和Ribbon的依赖

<?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.outlook.liufei32</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent> <artifactId>hello-spring-cloud-web-admin-ribbon</artifactId>
<packaging>jar</packaging> <name>hello-spring-cloud-web-admin-ribbon</name>
<url>https://github.com/Swagger-Ranger</url>
<inceptionYear>2018-Now</inceptionYear> <dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency> <!--web需要thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End --> <!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--ribbon的插件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- Spring Cloud End --> <!-- 解决 thymeleaf 模板引擎一定要执行严格的 html5 格式校验问题 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.outlook.liufei32.hello.spring.cloud.web.admin.ribbon.WebAdminRibbonApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

完善结构目录,并将服务注册到服务中心

在启动类上加入@EnableDiscoveryClient 注解注册到服务中心

package com.outlook.liufei32.hello.spring.cloud.web.admin.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient //发现服务注解,注解是不分顺序的
public class WebAdminRibbonApplication { public static void main( String[] args ) {
SpringApplication.run(WebAdminRibbonApplication.class, args);
}
}

配置application.yml

spring:
application:
name: hello-spring-cloud-web-admin-ribbon
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html server:
port: 8764 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

配置Configuration,注入RestTemplate 的 Bean,并通过 @LoadBalanced 注解表明开启负载均衡功能

package com.outlook.liufei32.hello.spring.cloud.web.admin.ribbon.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; @Configuration
public class RestTemplateConfiguration { @Bean//生成配置一个Bean
@LoadBalanced//访问负载均衡器,自动去寻找服务提供者
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

创建service,来测试请求restTemplate负载均衡

package com.outlook.liufei32.hello.spring.cloud.web.admin.ribbon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class AdminService { @Autowired
private RestTemplate restTemplate;//因为我在config里配置了restTemplate Bean所以能自动注入 /**
* url:就是http://+服务名,即在你要调用的service微服务里applicat.yml中配置的服务名,restTemplate会自动去寻找可用的服务
* @param message
* @return
*/
public String sayHi( String message ) {
return restTemplate.getForObject("http://hello-spring-cloud-service-admin/hi?message=" + message, String.class);
}
}

创建controller来解释web访问然后去访问service

package com.outlook.liufei32.hello.spring.cloud.web.admin.ribbon.controller;

import com.outlook.liufei32.hello.spring.cloud.web.admin.ribbon.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class AdminController { @Autowired
private AdminService adminService; @RequestMapping(value = "hi",method = RequestMethod.GET)
public String sayHi( @RequestParam String message ) {
return adminService.sayHi(message);
}
}

ribbon的架构

Feign

创建包和基本项目结构

在总的包下新建

spring-cloud-web-admin-feign工程,导入pom.xml

<?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.outlook.liufei32</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../spring-cloud-dependencies/pom.xml</relativePath>
</parent> <artifactId>spring-cloud-web-admin-feign</artifactId>
<packaging>jar</packaging> <name>spring-cloud-web-admin-feign</name>
<url></url>
<inceptionYear>2019-Now</inceptionYear> <dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End --> <!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Spring Cloud End --> <!-- 解决 thymeleaf 模板引擎一定要执行严格的 html5 格式校验问题 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.outlook.liufei32.spring.cloud.web.admin.feign.WebAdminFeignApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

其中feign的依赖就是:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

创建启动类

package com.outlook.liufe32.spring.cloud.web.admin.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableDiscoveryClient//发现服务
@EnableFeignClients//Feign
public class WebAdminFeignApplication { public static void main( String[] args ) {
SpringApplication.run(WebAdminFeignApplication.class, args);
}
}

配置yml

spring:
application:
name: spring-cloud-web-admin-feign
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html server:
port: 8765 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

创建Feign访问服务的接口和访问controller

这一步是不同于ribbon的地方,feign其实也集成了ribbon

package com.outlook.liufe32.spring.cloud.web.admin.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "spring-cloud-service-admin")//这里就是访问的服务名,在服务提供者的yml里或者去eureka里找服务名
public interface AdminService { //这里的写法就类似controller但不是方法而是接口
@RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi( @RequestParam(value = "message") String message ); }

创建controller

package com.outlook.liufe32.spring.cloud.web.admin.feign.controller;

import com.outlook.liufe32.spring.cloud.web.admin.feign.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class WebAdminController { @Autowired
private AdminService adminService; @RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi(@RequestParam(value = "message") String message) {
return adminService.sayHi(message);
}
}

测试访问

http://localhost:8765/hi?message=HelloFeign

访问feign的fontroller,就可以看到feign去调用了不同的服务提供者端口,即不同的服务者实例

Hi,your message is :"HelloFeign" i am from port:8762
Hi,your message is :"HelloFeign" i am from port:8763

本博客为Swagger-Ranger的笔记分享,文章会持续更新

文中源码地址: https://github.com/Swagger-Ranger

欢迎交流指正,如有侵权请联系作者确认删除: liufei32@outlook.com

springcloud-Netflix创建服务消费者的更多相关文章

  1. 创建服务消费者(Feign)

    概述 Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS ...

  2. springcloud干货之服务消费者(ribbon)

    本章介绍springcloud中的服务消费者 springcloud服务调用方式有两种实现方式: 1,restTemplate+ribbon, 2,feign 本来想一篇讲完,发现篇幅有点长,所以本章 ...

  3. 「 从0到1学习微服务SpringCloud 」05服务消费者Fegin

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  4. 创建服务消费者(Ribbon)

    概述 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的.Spring cloud 有两种服务调用方式,一种是 ribbon + restTempla ...

  5. springcloud干活之服务消费者(feign)

    springcloud系列文章的第三篇 本章将继续讲述springcloud的消费者(feign) Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端. ...

  6. 「 从0到1学习微服务SpringCloud 」04服务消费者Ribbon+RestTemplate

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  7. SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解

    前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...

  8. springcloud的服务提供者与服务消费者

    1.说明 springcloud中由服务消费者调用服务提供者一共有两种方法rest和feign 2.feign (1)使用feign的方式进行服务调,搭建服务提供者. 创建一个web项目(服务提供者) ...

  9. springCloud学习-服务消费者(rest+ribbon)

    1.ribbon简介 spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign. Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器 它可以 ...

随机推荐

  1. OpenCv-Python 图像处理基本操作

    1. 图片加载.显示和保存 import cv2 img = cv2.imread("01.jpg") imgGrey = cv2.imread("01.jpg" ...

  2. Spring 配置 详细

    一.连接池概述 数据库连接池概述: 数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指 ...

  3. SoapUI测试登录

    实际登录过程为: 1. 打开/login页面 2. 登录 3. 自动调整至/dashboard页面 SoapUI的设计上,每一次请求后,都会清理掉cookie,于是设计的测试登录过程如下: 1. po ...

  4. 面试题: mysql数据库 已看1 索引和事务 没用

    mysql数据库面试总结 2017年09月04日 00:11:40 阅读数:151 结合网上大神还有自己面试经历,收集的总结Mysql面试题,方便自己准备面试: mysql一个永远都复习不完,尽量总结 ...

  5. (十七)Spring 集成Quartz

    在使用jdk的timer时发现无法满足这次的开发需求:即无法在指定的日期进行执行任务.这便引入一个优秀的开源任务调度框架“quartz”.这里加入的是quartz-1.8.6版本.Quart的官网:h ...

  6. 关于 Number() parsint() abs() 的区别

    1. parseInt(‘’)      parseInt() 函数可解析一个字符串,并返回一个整数. 如果第一个字符不是数字或者负号,parseInt() 就会返回NaN 2.Number()    ...

  7. Centos7.2 下安装配置pip

    一.pip下载 wget https://files.pythonhosted.org/packages/ae/e8/2340d46ecadb1692a1e455f13f75e596d4eab3d11 ...

  8. ASP.NET自定义控件组件开发

    ASP.NET的开发都是事件驱动的,现在我们就来为控件添加事件.在事件之前 对委托事件要要熟悉. 其实定义事件的步骤很简单: 1.声明一个委托. 2.定义一个携带事件信息的类. 3.定义事件 4.定义 ...

  9. 利用memoize缓存到Redis出现多个参数同一个结果

    在为后端输出加入Redis缓存的过程中出现的问题. 在我利用Flask-restful架构的后端中,理所当然的利用装饰器marshal_with对我的返回数据进行格式化输出. 举个最简单的例子: fr ...

  10. Node.js 内置模块crypto加密模块(3) HMAC

    HMAC:哈希消息认证码 ( Hash-based Message Authentication Code ) HMAC是密钥相关的哈希算法 使用 HMAC 进行加密的Node实现的一种方法: &qu ...