关联知识:
内容提要:
服务注册中心使用nacos。
项目示例步骤:
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
server:
port: 8003
spring:
application:
# 服务注册时使用的别名
name: openfeign-consumer
cloud:
nacos:
discovery:
# nacos的地址
server-addr: localhost:8848
package learn.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OpenfeignConsumer {
public static void main(String[] args) {
SpringApplication.run(OpenfeignConsumer.class, args);
}
}
package learn.demo.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
// 在配置类中注入bean使用,可以为所有的http请求统一进行配置
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
package learn.demo.controller;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/openfeign/consumer/")
public class ConsumerController {
// 提供服务提供者注册时使用的别名
private static final String URL_PAYMENT_PRE_ALIAS = "http://nacos-provider/nacos/provider/";
// 服务提供者提供的接口
private static final String PAYMENT_API = "test";
@Resource
private RestTemplate restTemplate;
@GetMapping("test/alias")
public String testAlias() {
return restTemplate.getForObject(URL_PAYMENT_PRE_ALIAS+PAYMENT_API, String.class);
}
}
localhost:8001/nacos/provider/test
访问微服务接口,能正确返回信息localhost:8003/openfeign/consumer/test/alias
访问微服务接口,能正确返回信息服务注册中心使用nacos。
项目示例步骤:
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-loadbalancerartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
server:
port: 8004
spring:
application:
# 服务注册时使用的别名
name: openfeign-consumer1
cloud:
nacos:
discovery:
# nacos的地址
server-addr: localhost:8848
package learn.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class OpenfeignConsumer1 {
public static void main(String[] args) {
SpringApplication.run(OpenfeignConsumer1.class, args);
}
}
package learn.demo.inter;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
@Component
@FeignClient(value = "nacos-provider", path = "/nacos/provider/")
public interface ProviderInter {
@GetMapping("test")
String test();
}
package learn.demo.controller;
import jakarta.annotation.Resource;
import learn.demo.inter.ProviderInter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/openfeign/consumer1/")
public class ConsumerController {
@Resource
private ProviderInter providerInter;
@GetMapping("test")
public String test() {
return providerInter.test();
}
}
localhost:8001/nacos/provider/test
访问微服务接口,能正确返回信息localhost:8004/openfeign/consumer1/test
访问微服务接口,能正确返回信息