Ribbon方式
创建的eureka-consumer工程,pom.xml
在client的基础上添加ribbon依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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.cloud</groupId>-->
<!--<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>-->
<</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在启动程序Application
中同样添加@EnableDiscoveryClient
向服务中心注册,并且注入一个beanrestTemplate
;
通过@LoadBalanced
注解表明开启负载均衡的功能
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}
写一个测试controller类,注入restTemplate
,并在/consumer
接口中消费之前注册的服务,url为之前注册的EUREKA-CLIENT
,
@RestController
public class CosumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer")
public String consumer() {
String url = "http://EUREKA-CLIENT/disc";
return restTemplate.getForObject(url, String.class);
}
}
启动之前创建的服务中心,两个客户端和一个消费端,访问刚才创建的接口http://localhost:2101/consumer
,浏览器是两个client服务的交替显示,说明ribbon已经做了负载均衡
Services: [eureka-client, eureka-consumer]:2001 #客户端1
Services: [eureka-client, eureka-consumer]:2002 #客户端2
Services: [eureka-client, eureka-consumer]:2001 #客户端1
Services: [eureka-client, eureka-consumer]:2002 #客户端2
Services: [eureka-client, eureka-consumer]:2001 #客户端1
Services: [eureka-client, eureka-consumer]:2002 #客户端2
此时的结构:
- 一个服务中心
eureka-server
,端口 1001, - 两个客户端
eureka-client
, 端口分别是2001 和2002, - 一个消费端
eureka-consumer
, 端口是2101,当访问 eureka-consumer时,因为用ribbon进行了负载均衡,会轮流的调用两个客户端。
Feign方式
在之前eureka-consumer基础上,引入Feign,pom.xml
文件依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动application添加@EnableFeignClients
注解,删除restTemplate
,修改后如下:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}
添加一个FeignServerClient抽象类接口,通过@FeignClient("服务名")
来指定调用哪个服务,比如调用eureka-client
@FeignClient("eureka-client")
public interface FeignServerClient {
@GetMapping("/disc")
String consumer();
}
ConsumerController删除restTemplate
,注入刚才创建的抽象类接口
@RestController
public class CosumerController {
@Autowired
FeignServerClient feignServerClient;
@GetMapping("/consumer")
public String consumer() {
return feignServerClient.consumer();
}
}
按照Ribbon一样启动启动之前创建的服务中心、客户端和消费端,访问http://localhost:2101/consumer
,输出结果和ribbon一样,Feign具有负载均衡