我的spring cloud reset ribbon搭建

本文介绍了一个基于Spring Cloud的微服务架构实现案例,包括服务注册中心、客户端服务及负载均衡服务的具体配置与代码实现。

 

 

 

 

 

请求这个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.youkuaiyun.com/forezp/article/details/69788938

转载于:https://www.cnblogs.com/yeyusheng/p/8530428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值