ribbon 搭建

Posted jonban

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ribbon 搭建相关的知识,希望对你有一定的参考价值。

 

1.   新建Maven  项目    ribbon

 

2.   pom.xml

<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>
    <groupId>com.java</groupId>
    <artifactId>ribbon</artifactId>

    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>${project.artifactId}</name>

    <!-- 配置版本常量 -->
    <properties>
        <jdk.version>1.8</jdk.version>
        <spring.cloud.version>2.0.0.RELEASE</spring.cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>${spring.cloud.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>${spring.cloud.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>


        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

 

3.   application.yml

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url: 
      defaultZone: http://192.168.1.102:8080/eureka/  
      #defaultZone: http://s0.com:8080/eureka/,http://s1.com:8080/eureka/,http://s2.com:8080/eureka/  

 

4.   RibbonStarter.java

package com.java.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class RibbonStarter extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(RibbonStarter.class);
    }

}

 

 

5.   ConfigBean.java

package com.java.ribbon.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RetryRule;

@Configuration
public class ConfigBean {

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

    @Bean
    public IRule myRule() {
        // return new RoundRobinRule();
        // return new RandomRule();
        return new RetryRule();
    }

}

 

 

6.   HostService.java

package com.java.ribbon.service;

import com.alibaba.fastjson.JSONObject;

public interface HostService {

    JSONObject getHostMessage(String id);

}

 

7.   HostServiceImpl.java

package com.java.ribbon.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.java.ribbon.service.HostService;

@Service
@RibbonClient(name = "MICROSERVICE")
public class HostServiceImpl implements HostService {

    /**
     * Eureka中注册的微服务地址
     */
    private static final String REST_URL_PREFIX = "http://MICROSERVICE";

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public JSONObject getHostMessage(String id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/getHostMessage/" + id, JSONObject.class);
    }

}

 

 

8.   HostController.java

package com.java.ribbon.controller;

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

import com.alibaba.fastjson.JSONObject;
import com.java.ribbon.service.HostService;

@RestController
public class HostController {

    @Autowired
    private HostService hostService;

    @GetMapping("/getHostMessage/{id}")
    public JSONObject getHostMessage(@PathVariable String id) {
        return hostService.getHostMessage(id);
    }

}

 

 

9.  运行测试

启动  Eureka   服务注册中心,参考  https://www.cnblogs.com/jonban/p/9931412.html

启动  MicroService  微服务, 参考   https://www.cnblogs.com/jonban/p/9931579.html

启动  Ribbon服务,运行 RibbonStarter.java

 

浏览器输入URL

http://192.168.1.102/getHostMessage/hello

 

返回数据如下:

{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}

截图如下:

技术分享图片

 

调用微服务返回数据成功

 

 

 

.



以上是关于ribbon 搭建的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud:ribbon项目搭建(Gradle项目)

Spring Cloud Ribbon快速搭建

Java之 Spring Cloud 微服务搭建Ribbon(第一个阶段)SpringBoot项目实现商品服务器端是调用

我的spring cloud reset ribbon搭建

10分钟零基础带你入门Ribbon小项目-啥?小白都能看懂?

SpringCloud+Eureka+Feign+Ribbon的简化搭建流程,加入熔断,网关和Redis缓存[2]