SpringCloud-Netiflix(第一代)

SpringCloud-Netiflix(第一代)

五大神兽

说明 组件
服务治理 Eureka
负载均衡 Ribbon
服务调用 Feign
服务熔断与降级 hystrix
路由网关 Zuul
配置中心 config

Eureka服务治理

搭建Eureka服务器

导包

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

主启动类开启Eureka服务

@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class TsEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(TsEurekaApplication.class, args);
    }
    
}

编写配置文件

server.port=8761
# 是否向服务器注册自己
eureka.client.register-with-eureka=false
# 是否获取服务列表
eureka.client.fetch-registry=false
#Eureka的服务地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/

访问http://127.0.0.1:8761/ 即可看到Eureka的监控页面

注册服务

导包

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

主启动类开启Eureka客户端

@EnableEurekaClient

@EnableEurekaClient
@SpringBootApplication
@RestController
public class TsProvider8001Application {

    public static void main(String[] args) {
        SpringApplication.run(TsProvider8001Application.class, args);
    }

    @RequestMapping("/a")
    public HashMap<String,String> a() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name","服务提供者->集群8001号");
        return map;
    }
    
}

编写配置文件

spring.application.name=provider
server.port=8001
# 注册到http://127.0.0.1:8761/eureka/
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
eureka.instance.prefer-ip-address=true

Ribbon负载均衡

使用 @LoadBalanced

Eureka内已经集成了Ribbon不需要额外导包

@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

负载均衡策略

轮询(默认) 随机 权重 ···

IRule接口的实现类

Feign远程调用

导包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

主启动类开启Feign支持

@EnableFeignClients

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

向Eureka注册

server.port=8080
spring.application.name=consumer
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/

使用
provider为访问提供者的名称

@FeignClient(value = "provider")
public interface InfoService {
    @RequestMapping("/a")
    public Map<String ,String> getInfo();
}

此时,调用此接口的方法相当于远程调用:http://provider/a

hystrix服务降级

导包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

主启动类添加熔断支持

@EnableCircuitBreaker

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

编写业务

fallback = MyFallback.class 就是服务降级到MyFallback

还有一个fallbackFactory,实现FallbackFactory可以返回错误信息

@FeignClient(value = "provider",
        fallback = MyFallback.class)
public interface InfoService {
    @RequestMapping("/a")
    public Map<String ,String> getInfo();
}

MyFallback:

@Component
public class MyFallback implements InfoService {
    @Override
    public Map<String, String> getInfo() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name","出现异常");
        return map;
    }
}

Zuul路由网关

导包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

主启动类添加路由支持

@EnableZuulProxy

@EnableDiscoveryClient//@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class TsGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(TsGatewayApplication.class, args);
    }
}

配置

#端口
server.port=80
spring.application.name=zuul
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true
# 取消所有的默认服务地址
zuul.ignored-services=*
# 将consumer服务映射为ts/**
zuul.routes.consumer=/ts/**

源码地址:https://gitee.com/botaoshow/netflix

posted @   星时代曹波涛  阅读(105)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· 开发的设计和重构,为开发效率服务
· 从零开始开发一个 MCP Server!
· Ai满嘴顺口溜,想考研?浪费我几个小时
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
点击右上角即可分享
微信分享提示