一、准备工作
1. 新建Eureka注册中心模块(单机)
① 引入依赖
<dependencies> <!-- web 启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka server端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
② application.properties配置
# 设置spring应用命名,可以自定义,非必要 spring.application.name=eureka-server # 设置Eureka Server WEB控制台端口,自定义 server.port=8761 #是否将自己注册到Eureka-Server中,默认的为true eureka.client.register-with-eureka=false #是否从Eureka-Server中获取服务注册信息,默认为true eureka.client.fetch-registry=false
③ 启动eureka
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
执行main方法,访问 http://localhost:8761 查看。
2. 新建服务接口模块api-service
这个很简单,只有接口声明。
public interface UserService { Map<String,Object> getUser(Integer id); }
二、Ribbon中使用熔断器
1. 新建服务提供者模块
① 引入依赖
<dependencies> <!-- web 启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--接口依赖--> <dependency> <groupId>com.example</groupId> <artifactId>api-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
② application.properties配置
spring.application.name=userService server.port=8666 eureka.instance.instance-id=user-service #注册中心地址 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/ #访问信息可以使用IP地址 eureka.instance.prefer-ip-address=true
③ 启动类
@EnableDiscoveryClient @SpringBootApplication public class AppService { public static void main(String[] args) { SpringApplication.run(AppService.class, args); } }
④ 服务接口实现
@Service public class UserServiceImpl implements UserService { @Override public Map<String, Object> getUser(Integer id) { Map<String, Object> data = new HashMap<>(); data.put("id", Math.random() + "-" + id); data.put("name", "test name"); data.put("age", 20); return data; } }
⑤ 对外访问controller
@RestController public class UserController { @Autowired UserService userService; @RequestMapping("/getUser/{id}") public Map<String, Object> getUser(@PathVariable(value = "id") Integer id){ return userService.getUser(id); } }
2. 新建消费者模块
① 引入依赖
<dependencies> <!-- web 启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- ribbon --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <!-- 整合hystrix --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> </dependencies>
② application.properties
spring.application.name=userServiceConsumer server.port=8777 eureka.instance.instance-id=user-service-consumer eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/ #访问信息可以使用IP地址 eureka.instance.prefer-ip-address=true
③ 启动类
@EnableHystrix //在启动类上添加@EnableHystrix注解开启Hystrix的熔断器功能。 @EnableEurekaClient @SpringBootApplication public class RibbonConsumerApplication { //当添加@LoadBalanced注解,就代表启动Ribbon,进行负载均衡 @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonConsumerApplication.class, args); } }
④ controller
@RestController @RequestMapping("/hystrix/consumer") public class HystrixConsumerController { @Autowired private RestTemplate restTemplate; /** * 调用 user微服务 */ @HystrixCommand(fallbackMethod = "getDefaultUser") @GetMapping("getUser") public String getUser(Integer id) { String url = String.format("http://%s/getUser/" + id, "userService"); return restTemplate.getForObject(url, String.class); } public String getDefaultUser(Integer id) { System.out.println("熔断,默认回调函数"); return "{"id":-1,"name":"熔断用户","age":"21"}"; } }
⑤ 访问:http://localhost:8777/hystrix/consumer/getUser?id=7,可以正常访问,然后停掉服务,还会发现进入熔断操作。