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
原创粉丝点击