springCloud服务调用

1.被调用方 pom.xml文件

org.springframework.cloud
spring-cloud-starter-consul-discovery
1.3.1.RELEASE


org.springframework.boot
spring-boot-starter-actuator
1.3.8.RELEASE

2.被调用方 application.yml配置
spring:
cloud:
consul:
discovery:
enabled: true
health-check-interval: 10s
health-check-path: {spring.cloud.consul.discovery.service-name}:{server.port}
port: {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文件

org.springframework.cloud
spring-cloud-starter-consul-discovery
1.3.1.RELEASE


org.springframework.boot
spring-boot-starter-actuator
1.3.8.RELEASE



org.springframework.cloud
spring-cloud-starter-feign
1.4.6.RELEASE

5.调用方 application.yml
spring:
cloud:
consul:
discovery:
enabled: true
health-check-interval: 10s
health-check-path: {spring.cloud.consul.discovery.service-name}:{server.port}
port: {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服务调用)