码迷,mamicode.com
首页 > 编程语言 > 详细

springcloud-feign

时间:2019-07-03 20:17:17      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:system   width   专用   contex   deb   depend   依赖   ota   value   

1 feign的本质是还是使用HTTP协议调用

==================>feign调用思路
1 必须开启Eureka (调用者和被调用者都需要注册自身到注册中心中)
2 假如A是服务提供者B是接口调用者。 B调用A服务
3 我们不需要在A服务的启动类上加上 @EnableFeignClients 因为B是feigen的客户端
4 feign接口是写在B服务上的,然后@FeignClient(value = "FSH-HOUSE" , path = "/house")。中value的值就是A 服务注册在eureka中的服务名字
我们想要调用哪个服务,就写哪个服务的名字
5 无论A服务还是B服务,都必须注册在eurka中
6 B的启动类加上@EnableFeignClients 来声明B服务是一个feigen客户端的程序
7 fegin的调用,应该对被调用者完全透明,B调用A 那么B需要开启fegin,但A仍然只是注册到eurka中的A
而且A的服务启动类应该不需要加上@EnableFeignClients,也就是说A并不知道B是如何调用的,restTemplate或者feign,A只需要注册到Eureka中就行了

===================》注解含义
@EnableEureka 开启Eureka Server 作为注册中心的服务,需要在启动类上加上这个注解
@EnableDiscoveryClient 表示这个服务是一个Eureka客户端,可以提供服务
@EnableEurekaClient 和@EnableDiscoveryClient作用一样,只不过是eureka专用注解
@EnableFeignClients 标注此服务为feigen的客户端

我们来写一个实例测试一下:

1 启动Eureka Server 

  1.1 配置文件application.properties

spring.application.name=fangjia-eureka
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=true

#安全
security.basic.enabled=true
security.user.name=wyp
security.user.password=123456

 1.2 编写启动类 

package wyp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurkaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurkaServerApplication.class,args);
    }
}

 1.3 配置文件如下

<?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>

    <groupId>wyp</groupId>
    <artifactId>fangjia-eureka</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2 编写服务提供者

  2.1 编写提供者

package wyp.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/house")
public class HelloEureka {

    @GetMapping("/hello")
    public  String hello(){
  System.out.println("this is 8081");
        return "this is 8080 : hello";
    }
}

  2.2编写启动类

package wyp;

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;

@SpringBootApplication
@EnableDiscoveryClient
//@EnableFeignClients
public class FshHouseServicApplication {
    public static void main(String[] args) {
        SpringApplication.run(FshHouseServicApplication.class,args);
    }
}

 2.3 配置文件 application.properties

spring.application.name=fsh-house
server.port=8081
eureka.client.serviceUrl.defaultZone=http://wyp:123456@localhost:8761/eureka/

 2.4编写启动类

package wyp;

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;

@SpringBootApplication
@EnableDiscoveryClient
//@EnableFeignClients
public class FshHouseServicApplication {
    public static void main(String[] args) {
        SpringApplication.run(FshHouseServicApplication.class,args);
    }
}

 2.5 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>

    <groupId>wyp</groupId>
    <artifactId>fanjia-fsh-house­service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

3 编写fegin客户端

3.1 启动类

package wyp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FshSubstitutionServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(FshSubstitutionServiceApplication.class,args);
    }
}

3.2 配置类

package wyp.configuration;


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 BeanConfiguration {

    @Bean
   @LoadBalanced
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }
}
package wyp.configuration;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfiguration {

    @Bean
    Logger.Level  feginLoggerLevl(){
        return  Logger.Level.FULL;
    }
}

3.3 Controller

package wyp.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import wyp.feginInterface.HouseRemoteClient;


@RestController
@RequestMapping("substitution")
public class Xcontroller {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HouseRemoteClient houseRemoteClient;


    @GetMapping("/callHello")
    public String getHello(){
        for (int i=0 ;i<10 ;i++){
            String forObject = restTemplate.getForObject("http://FSH-HOUSE/house/hello", String.class);
        }
        return "dada";
    }


    /**
     * 通过fegin方式
     * @return
     */
    @GetMapping("/callHello1")
    public String getHello1(){
        String hello = houseRemoteClient.hello();
        System.out.println(hello);
        return hello;
    }
}

3.4 fegin接口

package wyp.feginInterface;


import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import wyp.configuration.FeignConfiguration;

@FeignClient(value = "FSH-HOUSE" , path = "/house",configuration = FeignConfiguration.class)
public interface HouseRemoteClient {

    @GetMapping("hello")
    String hello();
}

3.5 配置文件application.properties

spring.application.name=fsh-substitution
server.port=8082
eureka.client.serviceUrl.defaultZone=http://wyp:123456@localhost:8761/eureka/


#fegin日志配置

logging.level.wyp.feginInterface.HouseRemoteClient=DEBUG

3.6 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>

    <groupId>wyp</groupId>
    <artifactId>fangjia-fsh-substitution-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

4 测试

我们在浏览器输入http://localhost:8082/substitution/callHello1

技术图片

 

springcloud-feign

标签:system   width   专用   contex   deb   depend   依赖   ota   value   

原文地址:https://www.cnblogs.com/wangpipi/p/11128378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!