搭建步骤:
1、新建工程 demo_feign_provider
maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
新建控制器类:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author zhangyugu
* @Date 2020/7/9 6:56 上午
* @Version 1.0
*/
@RestController
public class TestController {
@GetMapping("/hello")
public String hello(@RequestParam("name") String name){
return "hello " + name;
}
}
新建启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author zhangyugu
* @Date 2020/7/9 6:54 上午
* @Version 1.0
*/
@SpringBootApplication
@EnableDiscoveryClient
//@EnableFeignClients
public class FeignProviderApplication {
public static void main(String[] args) {
SpringApplication.run(FeignProviderApplication.class, args);
}
}
application.yml 配置:
server:
port: 8854
spring:
application:
name: demo-feign-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
2、新建工程 demo_feign_consumer
maven依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
新建feign接口及控制器实现类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author zhangyugu
* @Date 2020/7/7 7:03 下午
* @Version 1.0
*/
@RestController
public class TestController {
@Autowired
Client client;
@GetMapping("/hello")
@ResponseBody
public String hello(@RequestParam("name") String name) {
return client.hello(name);
}
}
@FeignClient("demo-feign-provider")
interface Client {
@GetMapping("hello")
String hello(@RequestParam(name = "name") String name);
}
新建启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @Author zhangyugu
* @Date 2020/7/7 6:53 下午
* @Version 1.0
*/
@SpringBootApplication
@EnableFeignClients
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
新建配置文件application.yml:
server:
port: 8823
spring:
application:
name: demo-swagger
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
3、在已经启动nacos的前提下,先后启动 demo_feign_provider、demo_feign_consumer
测试:http://localhost:8823/hello?name=guzy
返回 hello guzy