zoukankan      html  css  js  c++  java
  • spring-cloud-eureka-client-consumer

    服务注册中心eureka-server已经搭好,并且SPRING-CLOUD-NETFLIX-EUREKA-CLIENT-APPLICATION提供一个hello服务

    编写一个eureka-client-consumer服务消费者,去消费该服务

    一、新建module,选择对应的springcloud模块,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>lf.liyouyou</groupId>
            <artifactId>spring-cloud-netflix-demo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <groupId>lf.youyou</groupId>
        <artifactId>spring-cloud-eureka-client-consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-cloud-eureka-client-consumer</name>
        <description>Demo project for Spring Boot</description>
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
    </project>

    二、启动类添加注解

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    public class SpringCloudEurekaClientConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringCloudEurekaClientConsumerApplication.class, args);
        }
    
    }

    三、编写调用hello服务代码

      首先编写一个可以远程调用服务feign接口,name属性配置需要调用的服务名称

    package lf.youyou.lf.com;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient(name="spring-cloud-netflix-eureka-client-application")
    public interface HelloRemote {
    
        @RequestMapping(value = "/hello")
        public String hello(@RequestParam(value = "name") String name);
    }

      Controller

      

    package lf.youyou.lf.com;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @Autowired
        HelloRemote helloRemote;
    
        @RequestMapping("/hello/{name}")
        public String index(@PathVariable("name") String name) {
            return helloRemote.hello(name);
        }
    }

    四、配置application.properties

    spring.application.name=spring-cloud-netflix-eureka-client-consumer
    server.port=9090
    eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

    启动服务,

    访问注册中心 http://localhost:8000/ 查看eureka面板

    再访问http://localhost:9090/hello/lf

    返回:hello lf,nice to meet you!

  • 相关阅读:
    scala之伴生对象的继承
    scala之伴生对象说明
    “Failed to install the following Android SDK packages as some licences have not been accepted” 错误
    PATH 环境变量重复问题解决
    Ubuntu 18.04 配置java环境
    JDBC的基本使用2
    DCL的基本语法(授权)
    ZJNU 1374
    ZJNU 2184
    ZJNU 1334
  • 原文地址:https://www.cnblogs.com/flgb/p/13802515.html
Copyright © 2011-2022 走看看