spring cloud(服務消費者(利用feign實現服務消費及負載均衡)——初學三)

 

Feign是一個聲明式的Web Service客戶端,咱們只須要使用Feign來建立一個接口並用註解來配置它既可完成。java

它具有可插拔的註解支持,包括Feign註解和JAX-RS註解。Feign也支持可插拔的編碼器和解碼器。web

Spring Cloud爲Feign增長了對Spring MVC註解的支持,還整合了Ribbon和Eureka來提供均衡負載的HTTP客戶端實現。spring

1、準備工做apache

  啓動服務註冊中心app

  啓動服務提供方  maven

  將服務提供方端口修改後再次啓動一個服務ide

  訪問 localhost:1111spring-boot

  

 

2、建立消費者ui

  一、建立spring boot項目(利用idea的Spring Initializr快速建立項目)編碼

  二、添加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>

 

  三、啓動類中添加註解

  

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);
    }
}

 

  四、定義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);
}

 

  五、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);

    }
}

 

  六、配置文件不變

spring.application.name=ribbon-consumer

server.port=3333
#服務註冊中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

 

  七、啓動服務,屢次訪問 localhost:3333

   在服務提供方不一樣端口打印的信息

  

   

相關文章
相關標籤/搜索