我的spring cloud reset ribbon搭建
Posted 不甘平凡,叶育生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我的spring cloud reset ribbon搭建相关的知识,希望对你有一定的参考价值。
请求这个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搭建的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud 消费服务Feign(踩着坑往前爬)
Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群
java.lang.AbstractMethodError: org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.cho