Eureka注册中心:《使用IntelliJ IDEA创建Spring Cloud服务注册中心》
服务提供者创建:《使用IntelliJ IDEA创建Spring Cloud的Eureka Client》
Ribbon实现负载均衡:《使用IntelliJ IDEA创建Ribbon项目实现负载均衡》
集成Feign的项目:《使用IntelliJ IDEA创建集成Feign的项目简化服务调用的网络连接》
Ribbon项目中使用Hystrix熔断器:《使用IntelliJ IDEA在Spring Cloud的Ribbon项目中使用Hystrix熔断器》
File---new---module---Spring Assistant
点击next
点击next
点击Finish
AcyxfeigntwoApplication.java
package com.acyx.feign;
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
@EnableDiscoveryClient
@EnableFeignClients
public class AcyxfeigntwoApplication {
public static void main(String[] args) {
SpringApplication.run(AcyxfeigntwoApplication.class, args);
}
}
HomeFeignService.java
package com.acyx.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "acyx-stock")
public interface HomeFeignService {
@RequestMapping(value = "/home")
String homeFeign(@RequestParam(value = "name") String name);
}
HomeFeignController.java
package com.acyx.feign.controller;
import com.acyx.feign.service.HomeFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeFeignController {
@Autowired
private HomeFeignService homeFeignService;
@RequestMapping("/homeFeign")
public String homeFeign(String name){
return homeFeignService.homeFeign(name);
}
}
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8101
spring:
application:
name: acyx-feign
在acyxfeigntwo中的application.yml,添加配置,开启hystrix熔断器。修改后,如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8101
spring:
application:
name: acyx-feign
feign:
hystrix:
enabled: true
pom.xml中添加如下内容:
org.springframework.cloud
spring-cloud-openfeign-core
org.springframework.cloud
spring-cloud-starter-openfeign
HomeFeignServiceHystric.java
package com.acyx.feign.service.impl;
import com.acyx.feign.service.HomeFeignService;
import org.springframework.stereotype.Component;
@Component
public class HomeFeignServiceHystric implements HomeFeignService {
@Override
public String homeFeign(String name) {
return "sorry, ACYX-STOCK is not normal! Hystrix fallbackMethod execute";
}
}
HomeFeignService.java中给@FeignClient添加fallback,修改后如下:
package com.acyx.feign.service;
import com.acyx.feign.service.impl.HomeFeignServiceHystric;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "acyx-stock",fallback = HomeFeignServiceHystric.class)
public interface HomeFeignService {
@RequestMapping(value = "/home")
String homeFeign(@RequestParam(value = "name") String name);
}
依次启动Eureka注册中心、acyxstock、acyxstocktwo、acyxfeigntwo,然后在浏览器中访问:http://127.0.0.1:8761
在启动acyxfeigntwo时控制台报错,如下图:
在pom.xml中添加如下依赖:
org.springframework.cloud
spring-cloud-starter-feign
点击如下箭头指示的图标,reimport依赖的jar包,出现如下图椭圆标注的unknown
在控制台显示如下内容
Could not find artifact org.springframework.cloud:spring-cloud-starter-feign:pom:unknown in nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public)
为什么上述提示信息中包含nexus-aliyun呢?因为在本地安装的maven的settings.xml配置文件中mirror配置的是nexus-aliyun。如需配置,可以参考文章《maven中settings.xml配置使用阿里云Maven仓库》
出现unknown可尝试添加version信息,查看配置的maven仓库http://maven.aliyun.com/nexus/content/groups/public中的spring-cloud-starter-feign的信息。
在pom.xml中添加如下依赖:
org.springframework.cloud
spring-cloud-starter-feign
2.0.0.M2
点击maven中reimport按钮
此时,再次尝试启动acyxfeigntwo,启动成功。
依次启动Eureka注册中心、acyxstock、acyxstocktwo、acyxfeigntwo,然后在浏览器中访问:http://127.0.0.1:8761
在浏览器中输入http://127.0.0.1:8101/homeFeign?name=abc
刷新
再次刷新
此时,关闭服务acyxstock、acyxstocktwo
在浏览器中输入http://127.0.0.1:8101/homeFeign?name=abc
Feign在简化网络连接的同时,还使用了Ribbon负载均衡的功能。
参考文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.2.RELEASE/reference/html/#how-to-include-hystrix