美文网首页
服务使用者-consumer

服务使用者-consumer

作者: cn_moexc | 来源:发表于2018-12-21 18:16 被阅读0次

创建服务使用者-SpringBoot项目
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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.moexc</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-feign</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>Dalston.SR5</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>

application.properties 如下:

#项目名
spring.application.name=consumer
#监听端口
server.port=9001
#服务中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

创建接口类用来调用之前发布的服务

package cn.moexc.consumer.fegin;

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

//与服务提供者名称一致
@FeignClient(name="provider")
public interface HelloFegin {
    @RequestMapping("/hello")
    public String hello(@RequestParam String name);
}

新建一个Controller.class:

package cn.moexc.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.moexc.consumer.fegin.HelloFegin;
@RestController
public class Hello {
    
    @Autowired
    HelloFegin helloFegin;
    
    @RequestMapping("/hello")
    public String hello(@RequestParam String name) {
        return helloFegin.hello(name);
    }
}

项目结构:


项目结构

启动类需要添加注解

@EnableDiscoveryClient
@EnableFeignClients

启动,访问http://localhost:9001/hello?name=123

显示 Hello, Cloud!123 代表成功

相关文章

  • 服务使用者-consumer

    创建服务使用者-SpringBoot项目pom.xml 如下: application.properties 如下...

  • kafka生产消费

    produce客户端 produce服务端 consumer客户端 consumer服务端

  • Dubbo Consumer 共享连接说明

    在Dubbo的consumer和provider的服务引用过程中,两者之间存在多个服务引用。 在consumer和...

  • spring cloud gateway中predicate、f

    以下是gateway的配置demo: consumer_low、consumer_high 所以对应的微服务都是同...

  • 默默背单词-399

    1. consumer:kənˈsuːmər] n. 消费者,顾客,用户;食用者,使用者,消耗者 2. custo...

  • Dubbo

    Dubbo 基础 Dubbo角色 provider:服务提供方 consumer:服务消费方 registry:注...

  • dubbo学习笔记

    架构 节点 角色说明Provider 暴露服务的服务提供方Consumer 调用远程服务的服务消费方...

  • 用Jmeter进行Dubbo接口测试

    节点角色 Provider: 暴露服务的服务提供方Consumer: 调用远程服务的服务消费方Registry: ...

  • dubbo

    Provider: 服务的提供方。 Consumer:服务消费方。 Registry:服务注册与发现的注册中心。 ...

  • Dubbo-基础概念介绍

    1.架构 1.1 服务角色介绍 Provider:暴露服务的服务提供方。 Consumer:调用远程服务的服务消费...

网友评论

      本文标题:服务使用者-consumer

      本文链接:https://www.haomeiwen.com/subject/rnevkqtx.html