springcloud==ribbon

单独使用ribbon

建立两个服务端,分别是8080和8081

建立客户端

 代码

package org.example.ribbon;

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.niws.client.http.RestClient;

public class TestRestClient {

	public static void main(String[] args) throws Exception {
		// 设置请求的服务器
		ConfigurationManager.getConfigInstance().setProperty(
				"my-client.ribbon.listOfServers",
				"localhost:8080,localhost:8081");
		// 获取REST请求客户端
		RestClient client = (RestClient) ClientFactory
				.getNamedClient("my-client");
		// 创建请求实例
		HttpRequest request = HttpRequest.newBuilder().uri("/users/123").build();
		// 发 送10次请求到服务器中
		for (int i = 0; i < 6; i++) {
			HttpResponse response = client.executeWithLoadBalancer(request);
			String result = response.getEntity(String.class);
			System.out.println(result);
		}
	}
}

依赖


    4.0.0

    org.example
    demo-cacerts
    1.0-SNAPSHOT
    jar

    demo-cacerts
    http://maven.apache.org

    
        UTF-8
        2.2.2
    


    
        
            junit
            junit
            3.8.1
            test
        
        
            org.projectlombok
            lombok
            1.18.28
        
        
            com.baidu.aip
            java-sdk
            4.8.0
        
        
        
            com.alibaba
            fastjson
            1.2.62
        


        
            com.netflix.ribbon
            ribbon
            ${ribbon.version}
        
        
            com.netflix.ribbon
            ribbon-httpclient
            ${ribbon.version}
        
        
        
            com.netflix.ribbon
            ribbon-loadbalancer
            ${ribbon.version}
        
        
            com.netflix.ribbon
            ribbon-core
            ${ribbon.version}
        

        
            com.netflix.archaius
            archaius-core
            0.7.4
        

        
            commons-configuration
            commons-configuration
            1.10
        

        
        
            com.ecwid
            ecwid-mailchimp
            2.0.1.0
        



    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    7
                    7
                
            
        
    

你可能感兴趣的:(spring,cloud,spring,cloud)