美文网首页
服务使用者-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

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