1. 程式人生 > >我的spring cloud reset ribbon搭建

我的spring cloud reset ribbon搭建

不同的 均衡 efault param OS figure con ont eureka

技術分享圖片

技術分享圖片

請求這個controller,會負載均衡到不同的消費提供者微服務http://localhost:8764/hi?name=ye

技術分享圖片

技術分享圖片

demo服務註冊中心服務器

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {

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

application.yml

server:
port: 8888 #指定服務端口
eureka:
client:
registerWithEureka: false #是否將eureka自身作為應用註冊到eureka註冊中心
fetchRegistry: false #為true時,可以啟動,但報異常:Cannot execute request on any known server
serviceUrl:
defaultZone: http://localhost:8888/eureka/

demo-client

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoClientApplication {

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

package com.example.demo;

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

import java.net.Inet4Address;
import java.net.UnknownHostException;


@RestController
public class DemoController {
@Value("${server.port}")
private String port;

@RequestMapping("/test")
public String getWord() {
String address = null;
try {
address = Inet4Address.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return address + ":" + port;
}
}

server:
port: 7070
spring:
application:
name: cloud-client #為你的應用起個名字,該名字將註冊到eureka註冊中心

eureka-serviceIP: localhost

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/

第2個改port為7071再次運行main類

server:
port: 7071
spring:
application:
name: cloud-client #為你的應用起個名字,該名字將註冊到eureka註冊中心

eureka-serviceIP: localhost

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/

demo-service-ribbon

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon

package com.example.demo;

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

@RestController
public class HelloControler {

@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}


}

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

@Autowired
RestTemplate restTemplate;

public String hiService(String name) {
return restTemplate.getForObject("http://CLOUD-CLIENT/test?name="+name,String.class);
}

}

package com.example.demo;

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

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {

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

@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}

}

參考:http://blog.csdn.net/forezp/article/details/69788938

我的spring cloud reset ribbon搭建