4.消费者(Feign)
来源:互联网 发布:c语言ide 编辑:程序博客网 时间:2024/06/08 20:15
1.Feign介绍
- Feign 采用的是基于接口的注解
- Feign 整合了ribbon
2.服务注册中心/服务(port=8763)/服务(port=8764)分别启动
查看eureks窗口服务确保正常启动(http://localhost:8761)
3.创建feign消费者工程(feign_test)
(1)pom.xml
<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.tyf</groupId> <artifactId>feign_test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>feign_test</name> <url>http://maven.apache.org</url> <!-- 1 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 2 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- 3 --><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 4 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <!-- 5 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency><!-- 6 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- 7 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 8 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- 9 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>
(2)配置文件(resources/application.yml)
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8766spring: application: name: feign-consumer-test
指定服务注册中心地址/消费者服务name/消费者服务port
(3)service接口
package com.tyf.feign_test;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;/* * 消费者service(用接口调用别人的controller来代替自己的service) * 1.@FeignClient:指定想要调用的服务提供者的名称 * 2.@RequestMappin:服务提供者程序中的一个具体url映射(服务提供者一个控制器下面的某个具体方法) * 3.@RequestParam: * 拼接起来就是:http://eureks-serverProvider-test/helloWorld/test?name=xxx */@FeignClient(value = "eureks-serverProvider-test")public interface modelService {@RequestMapping(value = "/helloWorld/test",method = RequestMethod.GET) String consumerTest(@RequestParam(value = "name") String name);}主要是写个接口方法,通过注解@FeignClient指定想要调用的服务名称,注解@RequestMapping指定想要调用的服务的某个控制器下的某个具体方法,接口方法返回一个string用来接收服务提供者方法的返回值
(4)controller
package com.tyf.feign_test;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;/* * 消费者控制器: * * 1.modelService:持有一个接口service,调用接口的方法(真正调用的是别人提供的方法) * * 2.访问url:http://localhost:8766/feignConsumer/test?name=xxx * * 3.参数name传给接口方法,再传给服务提供者(controller传给service计算得到结果) * 服务提供者将计算结果返回给本地(先返回给本地service接口再返回给本地controller) * */@RestController@RequestMapping("/feignConsumer")public class modelController {@Autowired modelService model_service; @RequestMapping(value = "/test",method = RequestMethod.GET) public void test(@RequestParam String name){ System.out.println(model_service.consumerTest(name)); }}写一个消费者控制器,提供一个url映射提供给用户http请求,控制器持有一个接口的实例用来调用远程服务
(5)启动类app,启动后等待用户访问
package com.tyf.feign_test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;/** * 启动类 * 1.@SpringBootApplication: * 2.@EnableDiscoveryClient: * 3.@EnableFeignClients:开启feign功能 * */@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class App {public static void main(String[] args) { SpringApplication.run(App.class, args); }}
(6)启动消费者程序,查看eureka窗口检查消费者和服务提供者是否正常启动
4.访问消费者(查看消费者/服务提供者控制台信息)
url:http://localhost:8766/feignConsumer/test?name=xxx
多次访问分别是不同端口下的服务提供者提供了服务(负载均衡)
阅读全文
0 0
- 4.消费者(Feign)
- SpringCloud--服务消费者(Feign)
- SpringCloud: 服务消费者(Feign)
- Spring Cloud(五):服务消费者Feign
- 第三篇: 服务消费者(Feign)
- springcloud入门之服务消费者Feign
- SpringCloud教程三:服务消费者(Feign)
- Feign
- Feign
- [bigdata-108] spring-cloud-04 分布式服务的feign消费者
- SpringCloud教程 | 第三篇: 服务消费者(Feign)
- Spring Cloud | 第三篇:服务消费者(Feign)
- SpringCloud教程 | 第三篇: 服务消费者(Feign)
- SpringCloud 教程 | 第三篇: 服务消费者(Feign)
- Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign
- springcloud实战之6 服务消费者(Feign)
- SpringCloud教程 | 第三篇: 服务消费者(Feign)
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
- 父进程产生一系列子进程,每个子进程打印自己的PID然后退出。要求父进程最后打印PID。
- myeclipse2016破解过程
- 【计算机算法分析】贪心算法——活动安排问题
- 线段树详解
- Masonry学习之基本布局
- 4.消费者(Feign)
- iptables防火墙策略
- 处理【由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面】
- SSH之hibernate(二)
- tcp/ip学习笔记--第26章 telnet and Rlogin:Remote Login
- VS2013 如何创建应用程序图标
- cocos2DX-lua 点击事件封装
- 子进程会继承父进程用户id,用户组id,用户信息,文件描述符,数据段,堆栈,当前工作目录,创建文件权限,信号处理方式,进程组号,会话期号。
- 使用Spring框架的好处是什么?