• spring cloud(服务消费者(利用feign实现服务消费及负载均衡)——初学三)


    Feign是一个声明式的Web Service客户端,我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。

    它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。

    Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

    一、准备工作

      启动服务注册中心

      启动服务提供方  

      将服务提供方端口修改后再次启动一个服务

      访问 localhost:1111

      

    二、创建消费者

      1、创建spring boot项目(利用idea的Spring Initializr快速创建项目)

      2、添加feign依赖

      

    <?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>com.daqsoft</groupId>
        <artifactId>customer_demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>customer_demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Dalston.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!--添加feign依赖-->
            <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>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

      3、启动类中添加注解

      

    package com.daqsoft;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.embedded.LocalServerPort;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    //用来发现注册服务
    @EnableDiscoveryClient
    @EnableFeignClients
    public class CustomerDemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(CustomerDemoApplication.class, args);
        }
    }

      4、定义compute-service服务接口

    package com.daqsoft;
    
    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;
    
    /**
     * @Description Created by liaoxx on 2017-6-12.
     */
    //服务提供方的名称
    @FeignClient("compute-service")
    public interface ComputerClient {
        @RequestMapping(method = RequestMethod.GET, value = "/add")
        Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
    }

      5、web端调用

    package com.daqsoft;
    
    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.RestController;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @Description Created by liaoxx on 2017-6-12.
     */
    @RestController
    public class CustomController {
    
        @Autowired
        private ComputerClient client;
    
        @RequestMapping(value = "/add", method = RequestMethod.GET)
        public Integer add(){
            return client.add(10,20);
    
        }
    }

      6、配置文件不变

    spring.application.name=ribbon-consumer
    
    server.port=3333
    #服务注册中心地址
    eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

      7、启动服务,多次访问 localhost:3333

       在服务提供方不同端口打印的信息

      

       

      

      

  • 相关阅读:
    CAS简介
    Volatile的3大特性
    dsf对矩阵进行搜索
    JVM
    REST风格
    自定义Starters(自动配置Bean)
    反射和注解的原理
    mybatis的注解开发
    Java的多线程安全
    mybatis(3)
  • 原文地址:https://www.cnblogs.com/liao-xx/p/6994157.html
Copyright © 2020-2023  润新知