Spring Cloud Netflix是Spring Cloud生态系统中的重要组成部分,也是使用Spring Cloud进行微服务架构开发的主要方向之一。Spring Cloud Netflix主要目标是为开发者提供构建分布式系统所需的组件,包括服务注册中心、负载均衡、断路器、转发器、过滤器等。二
4.0.0
com.lzc
springcloud-netflix
1.0-SNAPSHOT
pom
pojo
pojo/user-pojo
server
8
8
UTF-8
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR3
pom
import
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-test
4.0.0
com.lzc
springcloud-netflix
1.0-SNAPSHOT
eureka-server
jar
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Hello world!
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class, args);
}
}
server:
port: 8761 # 标识当前Eureka服务端的端口号
eureka:
instance:
hostname: localhost # 当前Eurek的实例名称
client:
registerWithEureka: false # 在客户端依赖中默认为true,所以服务端需要关闭
fetchRegistry: false # 在客户端依赖中默认为true,所以服务端需要关闭
serviceUrl: # http://localhost:8761/eureka/
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false # 关闭Eureka自我保护机制
4.0.0
com.lzc
server
1.0-SNAPSHOT
user-server
jar
UTF-8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Hello world!
*/
@SpringBootApplication
@EnableEurekaClient
public class UserServerApp {
public static void main(String[] args) {
SpringApplication.run(UserServerApp.class, args);
}
}
server:
port: 1020 # 标识当前user服务端的端口号
eureka:
client:
serviceUrl: # http://localhost:8761/eureka/
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true # 开启使用IP地址进行注册
instance-id: ${spring.application.name}:${server.port} # 修改实例Id
spring:
application: # 指定此服务的应用名称
name: user-server
@Bean
@LoadBalanced// 开启负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
// 交给spring管理就能开启随机算法 这里直接@bean就行了
// @Bean
// public RandomRule randomRule(){
// return new RandomRule();
// }
-Dserver.port=1021 -Deureka.instance.instance-id=user-server:1021
OpenFeign是一个声明式的Web服务客户端,是SpringCloud生态系统中负责服务间调用的一个服务调用工具。通过使用OpenFeign,我们可以方便地定义和调用RESTful服务,使得服务间调用变得简单、优雅,而且容易维护。它基于Netflix Feign库进行了扩展和改进,支持了更多的特性和功能。
org.springframework.cloud
spring-cloud-starter-openfeign
package com.lzc.feign;
import com.lzc.domain.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
// contextId FeignClient的唯一标识,推荐类名小驼峰
// path 映射路径 也可以requestMapping写在类上
// value 服务名称
@FeignClient(contextId = "userFeignClient",path = "user",value = "user-server")
public interface UserFeignClient {
@GetMapping
User getUser();
}
package com.lzc.controller;
import com.lzc.domain.User;
import com.lzc.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/pay")
public class PayController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping
public User getUser(){
return userFeignClient.getUser();
}
}