脱离Eureka使用Ribbon

岁酱吖の 提交于 2020-08-11 18:56:14

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.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>
        <project-name>spring-boot-ribbon</project-name>
    </properties>

    <groupId>org.fiend</groupId>
    <artifactId>${project-name}</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>${project-name}</name>
    <description>${project-name}</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>

        <!-- 加入断路器依赖 - 被zuul和feign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <!-- ================= 应用 ================== -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <!--<version>1.10</version>-->
        </dependency>

        <!-- javax api -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- 包含 mvc,aop 等jar资源 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    ...
</project>

application.yml

server:
  port: 16700
  max-http-header-size: 8192

# ========================== logging配置 ========================== #
#配置日志
logging:
  config: /data/config/logback-spring.xml  # 指定logback-spring.xml文件路径
#  level:
#    root: debug
spring:
  application:
    name: spring-boot-ribbon
  mvc.async.request-timeout: 20000
  http:
    encoding:
      charset: UTF-8
      force: true
      enabled: true

# ======================== ribbon代理服务 ======================== #
# 属性 micro-provider-srv.ribbon.listOfServers 用于名为 micro-provider-srv 的Ribbon客户端设置请求的地址列表
micro-provider-srv:
  ribbon:
    listOfServers: localhost:6001,localhost:6002
micro-consumer-srv:
  ribbon:
    listOfServers: localhost:7001,localhost:7002
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 配置 IRule 实现类
    # NFLoadBalancerClassName # 配置 IloadBalancer 实现类
    # NFLoadBalancerPingClassName # 配置 IPing 实现类
    # NIWSServerListClassName     # 配置 ServerList 实现类
    # NIWSServerListFilterClassName # 配置 ServerListFilter 实现类

# ======================== hystrix 配置 ======================== #
# 只需要在yml中配置zuul连接超时, socket超时以及断溶超时即可,
# 但需要注意的是, 当hystrix的值小的时候, hystrix生效, 当ribbon.ReadTimeout小的时候, ribbon生效
hystrix:
  command:
    default:
      execution:
        timeout:    # 配置命令的执行, 是否会超时
          enabled: true
        isolation:  # 命令的执行超时时间  超时将执行回退
          #          strategy: SEMAPHORE # 信号量模式, 转发线程和请求线程是一个线程, 不是官方最为推荐的隔离策略
          thread:
            timeoutInMilliseconds: 85000 # 该值要大于 ribbonTimeout = (20000 + 20000) * (0 + 1) * (1 + 1)

SpringBootRibbonSrv.java

package org.fiend.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * 该注解指定项目为springboot,由此类当作程序入口
 * 自动装配 web 依赖的环境
 * @author Administrator
 * @date 2018/4/18 14:45:06
 **/
@EnableCircuitBreaker
@SpringBootApplication
public class SpringBootRibbonSrv {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootRibbonSrv.class, args);
    }

    /**
     * 让restTemplate具备Ribbon负载均衡的能力
     */
    @Bean(name = "restTemplate")
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

TestController.java

package org.fiend.ribbon.controller;

import org.fiend.ribbon.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 单例模式
 * @author Administrator
 */
@RestController
public class TestController {
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	TestService testService;

	@RequestMapping("hi")
	public String hi() {
		return "hi, age = " + 123;
	}

	/**
	 * ribbon -- get请求
	 */
	@RequestMapping("ribbon-test")
	public String ribbonTest() {
		// myRestTemplate.headForHeaders()
		return testService.ribbonTest();
	}

	/**
	 * ribbon -- get请求
	 */
	@RequestMapping("hello-netflix")
	public String helloNetflix() {
		// myRestTemplate.headForHeaders()
		return testService.helloNetflix();
	}
}

TestService.java

package org.fiend.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author Administrator
 */
@Service
public class TestService {
    Logger log = LoggerFactory.getLogger(getClass());

    public static final String SPRING_DATA_SRV = "http://micro-provider-srv/";

    @Autowired
    RestTemplate restTemplate;

    /* =================== scope = prototype ==================== */
    public String ribbonTest() {
        return restTemplate.getForEntity(SPRING_DATA_SRV + "hi", String.class).getBody();
    }

    /**
     * 需要在启动类上加注解 @EnableCircuitBreaker 才有效
     * 使用ribbon进行微服务之间的调用 -- 断路器hystrix应用
     */
    @HystrixCommand(fallbackMethod = "helloFallBack", commandKey = "helloFallBack") // 注解指定发生错误时的回调方法
    public String helloNetflix() {
        return restTemplate.getForObject(SPRING_DATA_SRV + "hello", String.class);
    }

    public String helloFallBack() {
        return "Error occurred!";
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!