美文网首页
springCloud服务调用

springCloud服务调用

作者: zxy_3197 | 来源:发表于2019-02-21 17:00 被阅读0次

    1.被调用方 pom.xml文件
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>1.3.8.RELEASE</version>
    </dependency>

    2.被调用方 application.yml配置
    spring:
    cloud:
    consul:
    discovery:
    enabled: true
    health-check-interval: 10s
    health-check-path: {server.context-path}/health hostname: 192.168.205.18 instance-id:{spring.cloud.consul.discovery.service-name}:{spring.cloud.consul.discovery.hostname}:{server.port}
    port: {server.port} register: true service-name:{spring.application.name}
    enabled: true
    host: 192.168.205.196
    port: 8509
    application:
    name: demo-zxy22
    server:
    context-path: /
    port: 8080
    3.被调用方 主程序类添加注解
    @EnableDiscoveryClient
    @EnableFeignClients

    示例如下:
    @EnableDiscoveryClient
    @EnableFeignClients
    @SpringBootApplication
    @EnableSwagger2
    public class DemoApplication {
    public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class,args);
    }
    }

    4、调用方 pom.xml文件
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>1.3.8.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
    <version>1.4.6.RELEASE</version>
    </dependency>
    5.调用方 application.yml
    spring:
    cloud:
    consul:
    discovery:
    enabled: true
    health-check-interval: 10s
    health-check-path: {server.context-path}/health hostname: 192.168.205.18 instance-id:{spring.cloud.consul.discovery.service-name}:{spring.cloud.consul.discovery.hostname}:{server.port}
    port: {server.port} register: true service-name:{spring.application.name}
    enabled: true
    host: 192.168.205.196
    port: 8509
    application:
    name: demo-zxy11
    server:
    context-path: /
    port: 8083
    6、调用方 主程序 添加注解
    @EnableDiscoveryClient
    @EnableFeignClients
    示例如下
    @EnableDiscoveryClient
    @EnableFeignClients
    @SpringBootApplication
    public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
    

    }

    8、如何调用?
    创建接口,controller层直接调用。
    示例如下:
    package com.example.client.remote;
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    /**

    • @description: 调用用户模块服务
    • @author: zhangxinyu
    • @date: 2018/11/28
      */
      @FeignClient(name = "demo-zxy22")
      public interface UserService {
      @GetMapping(value = "/testboot/getuser")
      public Integer getUser();
      }

    相关文章

      网友评论

          本文标题:springCloud服务调用

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