手把手搭建微服务框架(四):搭建服务消费者(使用Fegin消费服务),并注册到注册中心(Eureka),整合Hystrix

1.新建maven工程,在pom.xml导入相关依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.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.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

2.在resources目录下新建application.properties

#端口
server.port=8888
#服务名称
spring.application.name=server-fegin
#注册服务地址
eureka.client.serviceUrl.defaultZone=http://localhost:5555/eureka
#指定 Eureka Client 间隔多久需要向 Eureka Server 发送心跳来告知 Eureka Server 该实例还存活着。
eureka.instance.lease-expiration-duration-in-seconds=10
#Eureka Client 向 Eureka Server 发送心跳的时间间隔
eureka.instance.lease-renewal-interval-in-seconds=5

3.新建UserService接口

package com.fegin.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value="server-provision")
public interface UserService {
    
    
	
	@RequestMapping("/username")
	public String getUsername();
}

4.将UserService注入到UserController

package com.fegin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fegin.service.UserService;

@RestController
public class UserController {
    
    

	@Autowired
	private UserService userService;

	@RequestMapping("/username")
	public String getUserName() {
    
    
		String username = this.userService.getUsername();
		return username;
	}
}

5.配置服务启动类

package com.fegin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeginApp {
    
    
	public static void main(String[] args) {
    
    
		SpringApplication.run(FeginApp.class, args);
	}
}

6.访问http://localhost:8888/username消费服务

在这里插入图片描述

7.由于Fegin自带负载均衡策略,修改服务提供者端口号,再次启动

访问http://localhost:8888/username

张三--------端口6666
张三--------端口6667

8.整合断路器Hystrix,application.properties添加Hystrix配置

#启用hystrix
feign.hystrix.enabled=true
#在测试中踩的坑,不要和上面的配置一起使用,会一致提示报错,不进Hystrix方法
#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000

9.在UserService类上配置MyHystrix

package com.fegin.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fegin.hystrix.MyHystrix;

@FeignClient(value="server-provision",fallback=MyHystrix.class)
public interface UserService {
    
    
	
	@RequestMapping("/username")
	public String getUsername();
}

10.创建MyHystrix实现UserService接口

package com.fegin.hystrix;

import org.springframework.stereotype.Component;
import com.fegin.service.UserService;

@Component
public class MyHystrix implements UserService{
    
    
	@Override
	public String getUsername() {
    
    
		//服务降级
		return "网络异常...";
	}
}

在这里插入图片描述

11.关闭所有server-provision服务提供者,访问消费者

http://localhost:8888/username

此时没有服务提供者,会自动跳转到Hystrix,页面显示网络异常

猜你喜欢

转载自blog.csdn.net/weixin_44315761/article/details/106748120