开发工具:STS
新建项目前,请确认工作环境已经配置好了Maven。
本章提及的Eureka Server 请阅读上一篇博客这里 https://blog.csdn.net/Lee_SmallNorth/article/details/95975546
简介:
Eureka 与 Ribbon配合使用的架构图
注意:以下方法是新建两个项目,一个作为普通接口调用(可直接新建一个项目然后配置EurekaClient就可以了),一个作为负载均衡使用,或者直接拷贝,然后把配置端口号修改一下。
新建项目1,作为普通接口使用
application.yml
server:
port: 1004
eureka:
client:
service-url:
#配置Eureka Server的地址,因为是要注册服务到Eureka Server
defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
instance:
#将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
prefer-ip-address: true
#指明ip
instance-id: 10.8.65.38:dev-ribbon-client:1004
#告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
lease-expiration-duration-in-seconds: 2
#每间隔1s向服务器发送一次心跳,证明自己还活着
lease-renewal-interval-in-seconds: 1
spring:
application:
name: dev-ribbon-client
启动类添加注解
编写测试Controller
package com.portal.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RibbonController {
@GetMapping("/getRibbon")
public Map<String, Object> getRibbon()
{
Map<String, Object> map = new HashMap<>();
map.put("姓名", "张三");
map.put("年龄", 25);
map.put("性别", "男");
map.put("电话", "18020000111");
return map;
}
}
启动测试,没有问题的话继续
新建项目2,作为负载均衡使用
application.yml
server:
port: 1003
eureka:
client:
service-url:
#配置Eureka Server的地址,因为是要注册服务到Eureka Server
defaultZone: http://xiaobei:xiaobei@localhost:1001/eureka/
instance:
#将自己的ip注册到EurekaServer,建议此处配置加上,在进行Jenkins/Docker构建部署项目会需要。
prefer-ip-address: true
#指明ip
instance-id: 10.8.65.38:dev-ribbon-client:1003
#告诉服务端,如果我2s以内没有给你发送心跳,代表我“死”了,将我踢掉
lease-expiration-duration-in-seconds: 2
#每间隔1s向服务器发送一次心跳,证明自己还活着
lease-renewal-interval-in-seconds: 1
spring:
application:
name: dev-ribbon-client
这里要注意看下,我是把项目1配置文件中服务名称和项目2一致的。
启动类
package com.portal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient //开启服务注册
public class SpringcloudRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudRibbonApplication.class, args);
}
/**
* ribbon + resttemplate调用方式
* @return
*/
@Bean //注入实体类
@LoadBalanced //实现负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
编写测试类Controller
package com.portal.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class RibbonController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getRibbonMap")
public Map<String,Object> getRibbonMap()
{
//http://dev-ribbon-client/getRibbon 这个就是项目1的测试方法,通过Eureka中服务名称直接调用
return restTemplate.getForObject("http://dev-ribbon-client/getRibbon", Map.class);
}
}
到此项目搭建完毕,准备测试,启动Eureka Server ,项目1,项目2,进入Eureka管理页面
测试负载均衡接口
成功,礼花大炮哄起来,哈哈哈