JAVA springboot ssm b2b2c多用戶商城系統源碼-Eureka服務消費Feign

1、Feign簡介

Feign是一種聲明式、模板化的HTTP客戶端。這使得Web服務客戶端的寫入更加方便 要使用Feign建立一個界面並對其進行註釋。它具備可插入註釋支持,包括Feign註釋和JAX-RS註釋。Feign還支持可插拔編碼器和解碼器。Spring Cloud增長了對Spring MVC註釋的支持,並使用Spring Web中默認使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign時提供負載均衡的http客戶端。這段話來源於官方文檔,說白了就是經過Feign來調用Rest接口,而無需使用其餘HTTP訪問組件,而且同時還提供了負載均衡、編解碼等功能,使用起來很方便。html

2、環境介紹

首先在A服務器上啓動Eureka服務,而後在B、C兩臺服務器上分別啓動ms-demo-provider服務,這裏也能夠部署在一臺服務器上採用不一樣的端口。訪問Eureka界面查看服務註冊狀態,以後在本地新建客戶端調用工程進行測試。此時A爲註冊中心,B、C分別爲服務提供者(提供相同的接口),本地工程爲服務消費者。java

3、項目代碼

在Idea中建立maven工程,ms-eurekaclient-demo工程代碼結構以下:web

1: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>com.cloud.microservice</groupId>
    <artifactId>ms-eurekaclient-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ms-eurekaclient-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-hystrix</artifactId>
        </dependency>
        <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>
    </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>複製代碼
複製代碼

2:application.properties中的配置信息以下:

複製代碼
spring.application.name=ms-eurekaclient-demo
server.port=9800

# 註冊中心地址
eureka.client.serviceUrl.defaultZone=http://xx.xx.xx.xx:9000/eureka/

# Indicates whether this client should fetch eureka registry information from eureka server
# 客戶端是否要從eureka server獲取註冊信息,默認爲true
eureka.client.fetchRegistry=true

# Indicates how often(in seconds) to fetch the registry information from the eureka server
# 從eureka server獲取註冊信息的頻率,默認爲30秒,縮短配置時間能夠緩解服務上線時間過長的問題
eureka.client.registryFetchIntervalSeconds=10複製代碼
複製代碼

3:在入口類Application中增長@EnableEurekaClient和@EnableFeignClients的註解

  • @EnableEurekaClient:註解用來標識開啓服務發現功能,聽說也可以使用@EnableDiscoveryClient,網上有人說@EnableEurekaClient自己就是用@EnableDiscoveryClient來實現的,這點沒仔細研究過
  • @EnableFeignClients:註解用來開啓Feign功能
複製代碼
package com.cloud.microservice.eurekaclientdemo;

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 FeignDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignDemoApplication.class, args);
    }
}複製代碼
複製代碼

4:建立IUserFeignServiceClient接口類,代碼以下:

複製代碼
package com.cloud.microservice.eurekaclientdemo.FeignClient;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("ms-demo-provider")
public interface IUserFeignServiceClient {
    //Feign定義服務提供者接口
    @RequestMapping(value = "/demo/user/1.0/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    String findAll();
}複製代碼
複製代碼

5:建立IUserService接口類和UserServiceImp實現類,代碼以下:

IUserService接口類以下:spring

package com.cloud.microservice.eurekaclientdemo.FeignClient;

public interface  IUserService {
    String findAll();
}複製代碼

UserServiceImp實現類以下:apache

複製代碼
package com.cloud.microservice.eurekaclientdemo.FeignClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImp implements  IUserService{
    @Autowired
    private IUserFeignServiceClient userFeignServiceClient;

    public String findAll() {
        return "Feign: " + userFeignServiceClient.findAll();
    }
}複製代碼
複製代碼

6:Rest接口定義,代碼以下:

複製代碼
package com.cloud.microservice.eurekaclientdemo.FeignClient;

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;

@RestController
public class FeignDemoController {
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/feigndemo/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public String feignDemo() {
        return userService.findAll();
    }
}複製代碼
複製代碼

啓動工程,而後刷新Eureka界面,能夠看到Feign已經註冊到服務中心json

Honghu代碼結構圖:bash

更多詳細源碼參考來源服務器

Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼請加企鵝求求:一零三八七七四六二六
app

相關文章
相關標籤/搜索