java B2B2C 源码 springmvc mybatis多租户电子商城系统-Eureka服务消费Feign

一、Feign简介

Feign是一种声明式、模板化的HTTP客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六.这段话来源于官方文档,说白了就是通过Feign来调用Rest接口,而无需使用其他HTTP访问组件,并且同时还提供了负载均衡、编解码等功能,使用起来很方便。

二、环境介绍

首先在A服务器上启动Eureka服务,然后在B、C两台服务器上分别启动ms-demo-provider服务,这里也可以部署在一台服务器上采用不同的端口。访问Eureka界面查看服务注册状态,之后在本地新建客户端调用工程进行测试。此时A为注册中心,B、C分别为服务提供者(提供相同的接口),本地工程为服务消费者。

三、项目代码

在Idea中创建maven工程,ms-eurekaclient-demo工程代码结构如下:

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接口类如下:

package com.cloud.microservice.eurekaclientdemo.FeignClient;

public interface  IUserService {
    String findAll();
}复制代码

UserServiceImp实现类如下:

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已经注册到服务中心

Honghu代码结构图:

Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六

转载于:https://juejin.im/post/5c453a24e51d45520a76ac1d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值