搭建
搭建eureka注册中心
……
我的以前的博客里面有搭建集群与单机的细节
注册中心访问地址
http://localhost:7001/
搭建两个生产者
provider-8001
provider-8001
搭建8001
pom
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 8001
spring:
application:
# 服务名称
name: provider-service
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka # 集群版
defaultZone: http://localhost:7001/eureka # 单机版
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Provider8001 {
public static void main(String[] args) {
SpringApplication.run(Provider8001.class,args);
}
}
业务类(controller)
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/get/{id}")
public String getPaymentById(@PathVariable("id") Long id)
{
return serverPort+id;
}
}
搭建8002
……
与8001相同
搭建一个消费者
feign-consumer-80
pom
<dependencies>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般基础通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 80
spring:
application:
# 服务名称
name: feign-consumer-service
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka # 集群版
defaultZone: http://localhost:7001/eureka # 单机版
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
//开启feign
@EnableFeignClients
public class Feign80 {
public static void main(String[] args) {
SpringApplication.run(Feign80.class,args);
}
}
业务类(service)
通过接口的方式调用远程服务接口
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
//生产者服务名称
@FeignClient(value = "PROVIDER-SERVICE")
public interface PaymentFeignService
{
//生产者地址
@GetMapping(value = "/payment/get/{id}")
String getPaymentById(@PathVariable("id") Long id);
}
业务类(controller)
import com.xiong.service.PaymentFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class FeignController
{
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/get/{id}")
public String getPaymentById(@PathVariable("id") Long id)
{
return paymentFeignService.getPaymentById(id);
}
}
启动测试
启动
启动eureka注册中心
启动两个生产者
启动一个消费者
测试
访问注册中心
http://localhost:7001/
出现以下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Oh5DiUlY-1643181209998)(C:\Users\38117\AppData\Roaming\Typora\typora-user-images\image-20220126150544047.png)]
访问消费者
让消费者调用生产者接口
http://localhost/consumer/payment/get/1
发现8001与8002 来回切换,即说明部署成功。
来回切换是因为openFeign自己整合了Ribbon
常见面试题
启动两个生产者
启动一个消费者
测试
访问注册中心
http://localhost:7001/
出现以下
[外链图片转存中…(img-Oh5DiUlY-1643181209998)]
访问消费者
让消费者调用生产者接口
http://localhost/consumer/payment/get/1
发现8001与8002 来回切换,即说明部署成功。
来回切换是因为openFeign自己整合了Ribbon
常见面试题
……
更多推荐
springcloud中openFeign的使用
发布评论