标签:pre http import start app string enc ref 成功
官网:https://cloud.spring.io/spring-cloud-openfeign/2.2.x/reference/html/#spring-cloud-feign
Feign是声明式的web服务客户端,使用feign可以使web服务客户端编写起来更加容易。
创建一个接口,在上面加上Feign的注解。
提供者需要注册到nacos上提供服务,这里使用的注册中心是nacos。
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
这里将controller一起写到了启动类里。
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProvider9001Application {
public static void main(String[] args) {
SpringApplication.run(NacosProvider9001Application.class, args);
}
@RestController
public class EchoController {
/**
* serverPort从application配置文件读取
*/
@Value(value = "${server.port}")
private String serverPort;
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string + ":" + serverPort;
}
}
}
server.port=9001
# 服务名,也是注册到nacos上的名
spring.application.name=nacos-provider
# nacos注册中心的地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
消费者也要注册到nacos,然后使用feign调用提供者的接口。
<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>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类添加注解@EnableFeignClients即可
@SpringBootApplication
@EnableFeignClients
public class NacosFeignApplication {
public static void main(String[] args) {
SpringApplication.run(NacosFeignApplication.class, args);
}
}
server.port=8088
spring.application.name=nacos-feign
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
package com.example.nacosfeign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* FeignClient中的value是想要调用的微服务名称
*
* @author Zzwen
* @date 2020/6/3 14:37
*/
@Component
@FeignClient(value = "nacos-provider")
public interface FeignService {
/**
* mapping填入想要调用的微服务接口的相应路径
*
* @param string string
* @return string
*/
@RequestMapping("/echo/{string}")
String echo(@PathVariable String string);
}
@Controller
public class FeignController {
@Resource
private FeignService feignService;
@ResponseBody
@RequestMapping("/feign/{string}")
public String feignString(@PathVariable String string) {
String echo = feignService.echo(string);
return "feign, " + echo;
}
}
启动成功后:
运行成功后在nacos网页上能够查看到这两个微服务,名称就是application.name
首先测试provider服务器能否正常访问,访问:http://localhost:9001/echo/1111
在通过feign调用接口进行访问,访问:http://localhost:8088/feign/stet
标签:pre http import start app string enc ref 成功
原文地址:https://www.cnblogs.com/Zzwena/p/13231159.html