3. 配置application.yml
server:
port: 8001
#指定当前eureka客户端注册地址
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka
# 指定应用名称
spring:
application:
name: provider
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName UserController
* @Description TODO
* @author cuiyingfan
* @date 2021/8/4 16:38
* @Version 1.0
*/
@RestController
@RequestMapping("user")
public class UserController {
@Value("${server.port}")
String port;
@GetMapping("/sayHello")
public String sayhello() {
return "I`m provider " + port + " ,Hello consumer!";
}
@GetMapping("/sayHi")
public String sayHi() {
return "I`m provider " + port + " ,Hi consumer!";
}
@GetMapping("/sayHaha")
public String sayHaha() {
return "I`m provider " + port + " ,Haha consumer!";
}
}
访问 http://localhost:8001/user/sayHi 查看
server:
port: 8003
#指定当前eureka客户端注册地址
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka
# 指定应用名称
spring:
application:
name: consumer
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @ClassName UserApi
* @Description TODO
* @author cuiyingfan
* @date 2021/8/4 16:55
* @Version 1.0
*/
@FeignClient("provider")
public interface UserApi {
@GetMapping("user/sayHello")
String sayhello();
@GetMapping("user/sayHi")
String sayHi();
@GetMapping("user/sayHaha")
String sayHaha();
}
import com.ivan.user.api.UserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName UserController
* @Description TODO
* @author cuiyingfan
* @date 2021/8/4 17:01
* @Version 1.0
*/
@RestController
public class UserController {
@Autowired
private UserApi userApi;
@GetMapping("user")
public String show() {
return userApi.sayHaha();
}
}
启动后共4个模块,Eureka注册中心、2个服务提供者、1个消费者
7. 访问消费者对外提供的接口 http://localhost:8003/user ,发现消费者通过Ribbon负载均衡轮询调用2个服务提供者
原文:https://www.cnblogs.com/isIvanTsui/p/15099929.html