上一篇已经将了如何将生产者注册到eureka中,本篇文章讲的是如何通过eureka去消费生产者
model的创建前三篇的操作 已经很熟悉了所以 就调过创建model的过程,直接到项目内部的编写
项目结构图:
首先要把pom中的依赖添加进去
<dependencies>
<!--调用公用实体类,相当于依赖这个api-->
<dependency>
<groupId>com.wangsc.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- robbin 配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--上面的eureka pom里已经添加robbin的依赖了,所以这块可以不用-->
<!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>srping-cloud-starter-robbin</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
yml的配置文件主需要把当前服务注册到eureka中即可, 如果需要数据库配置自行添加即可
#服务端口号
server:
port: 1025
#服务注册到eureka中
eureka:
client:
register-with-eureka: false #是否将自己注册客户端到eureka上
service-url:
defaultZone: http://eureka7001.com:7001/euerka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
eureka整合ribbon只需要在client端进行ribbon的配置即可,默认的策略是轮询机制,如果需要可以自行配置负载均衡的策略。
**此负载均衡只是针对客户端,切记。
启动类需要添加上相关的robbin注解
microservicecloud-dept是生产的微服务名称切莫填写错误,后面的配置就是你自定义的配置规则,如果不写就是默认轮询方式
配置bean
本次写的不是用feign模式调用微服务。
/**
* @author:wang
* @Description:
* @date:2018/11/7
*/
@Configuration
public class ConfigBean {
@Bean
@LoadBalanced() //Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具。
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
/**loadbalance
* 默认是负载均衡机制是轮询。
* 如果想切换其他的官方提供给的方法可以配置一**********释去掉这样就切换到随机模式*/
// @Bean
// public IRule myRule(){
// return new RandomRule();
// }
}
自定义负载均衡的策略,需要继承AbstractLoadBalancerRule实现里面的choose方法,就是负载均衡的策略
/**
* @author:wang
* @Description:
* @date:2018/11/23
*/
public class UserFiveRules extends AbstractLoadBalancerRule {
/**
* 负载均衡算法,每台服务器,调动五次后,调用下一台
* @param lb
* @param key
* @return
*/
/**每台机器调用的次数*/
private Integer totle = 0;
/** 每台机器的索引,目前分3台*/
private Integer service_index = 0 ;
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
}
Server server = null;
while (server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();
List<Server> allList = lb.getAllServers();
int serverCount = allList.size();
if (serverCount == 0) {
/*
* No servers. End regardless of pass, because subsequent passes
* only get more restrictive.
*/
return null;
}
if (totle < 4) {
++totle;
} else {
totle=0;
service_index++;
}
Integer index = (service_index % 3);
server = upList.get(index);
if (server == null) {
/*
* The only time this should happen is if the server list were
* somehow trimmed. This is a transient condition. Retry after
* yielding.
*/
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
// Shouldn't actually happen.. but must be transient or a bug.
server = null;
Thread.yield();
}
return server;
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
// TODO Auto-generated method stub
}
}
controller模仿调用
/**
* @author:wang
* @Description:
* @date:2018/11/7
*/
@RestController
public class DeptController_Consumer {
@Autowired
private RestTemplate restTemplate;
/**服务提供端 */
private static final String REST_URL_PREFIX = "http://microservicecloud-dept";
// private static final String REST_URL_PREFIX = "http://localhost:8001";
@RequestMapping(value = "/consumer/add")
public Boolean add(Dept dept){
return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, Boolean.class);
}
@RequestMapping(value = "/consumer/get/{id}")
public Dept get(@PathVariable("id") Long id){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class);
}
@RequestMapping(value = "/consumer/list")
public List<Dept> list(){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
}
}
由于这段项目是很早以前写的,今天才仅需更新,可能有一些遗漏望请见谅