创建工程
创建 user_openfeign 工程
添加依赖pom.xml
<dependencies>
<!-- openfeign依赖 -->
<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>
</dependency>
<!-- springboot依赖,不写版本号表示继承父工程 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- mybatis与springboot的整合 -->
<!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<!--mysql-connector-java-->
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- </dependency>-->
<!--jdbc-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-jdbc</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配置文件
在resources文件夹中创建application.yml配置文件
server:
port: 9001 # 服务端口
spring:
application:
name: user-consumer # 注册服务名
cloud:
nacos:
discovery:
server-addr: 106.13.165.100:8848 # nacos地址
# 配置服务提供者url,便于在类中调用
service-provider:
notice-provider:
name: notice-provider
urls:
notice: /notice/{id}
getNoteById: /getNoteById/{id}
# 负载均衡策略
notice-provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
负载均衡策略参见:
https://www.malaoshi.top/show_1EF5iJxtARRK.html
https://www.malaoshi.top/show_1EF5iInH93Gc.html
主启动类
package top.malaoshi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class UserFeignMain {
public static void main(String[] args) {
SpringApplication.run(UserFeignMain.class, args);
}
}
FeignClient接口
package top.malaoshi.clients;
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="${service-provider.notice-provider.name}") //微服务名称
public interface NoticeClient {
@GetMapping(value = "${service-provider.notice-provider.urls.notice}")
public String notice(@PathVariable("id") String id);
}
controller
package top.malaoshi.controller;
import lombok.extern.slf4j.Slf4j;
import top.malaoshi.clients.NoticeClient;
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
@Slf4j
public class FollowCtrl {
@Resource
private NoticeClient noticeClient;
@GetMapping(value="/follow/{id}")
public String follow(@PathVariable("id") String id){
log.info("user_openfeign:id:"+id);
return noticeClient.notice(id);
}
}
修改notice服务的NoticeCtrl
打印一段信息,区别user调用的是哪一个notice服务
package top.malaoshi.controllers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j //添加注解
public class NoticeCtrl {
@GetMapping(value = "/notice/{id}")
public String notice(@PathVariable String id) {
log.info("id--",id); //直接使用
return "通知成功! " + id;
}
}
测试
启动
启动nacos
启动2个notice
启动user_openfeign
测试服务
访问 http://localhost:9001/follow/1 能够随机调用1个notice
其中一个notice服务会打印信息
