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: {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文件
<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: {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();
}
网友评论