概述
服务注册:http://blog.csdn.net/qq_40075229/article/details/78978853
1.新建maven(war)项目:springcloud-ribbonclient
pom.xml添加依赖:spring-cloud-starter-ribbon
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka:服务注册 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- ribbon:服务发现 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
2.src/main/resources目录下新建application.yml
该文件配置:
server:
port: 8088
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: ribbonclient
#服务名不能带"_",多个相同的服务的服务名要相同
#配置赋值均衡算法
#被调用的服务的服务名
SENDMAIL:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
BestAvailableRule
选择一个最小的并发请求的server
AvailabilityFilteringRule 过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值)
WeightedResponseTimeRule 根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。
RetryRule
对选定的负载均衡策略机上重试机制。
RoundRobinRule
roundRobin方式轮询选择server
RandomRule
随机选择一个server
ZoneAvoidanceRule
复合判断server所在区域的性能和server的可用性选择server
3.添加配置类MyConfiguration
@Configuration
public class MyConfiguration {
//RestTemplate:调用服务
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
4.新建contrller:SendController
@RestController
public class SendController {
@Autowired
private RestTemplate restTemplate;
//测试负载均衡
/* @Autowired
private LoadBalancerClient loadBalancer;
@RequestMapping("choosePub")
public String choosePub() {
StringBuffer sb=new StringBuffer();
for(int i=0;i<=10;i++) {
ServiceInstance ss=loadBalancer.choose("SENDMAIL");//从两个sendmail中选择一个 这里涉及到选择算法
sb.append(ss.getUri().toString()+"<br/>");
}
return sb.toString();
} */
//在springcloud-ribben服务中调用springcloud-sendemail的发送邮件服务
@PostMapping("/mail")
public String sendMail(String send_to,String send_subject,String send_content){
String results =null;
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
Map<String, Object> map = new HashMap<String, Object>();
map.put("send_to", send_to);
map.put("send_subject", send_subject);
map.put("send_content", send_content);
HttpEntity<Map<String, Object>> request = new HttpEntity<>(map, headers);
//调用SENDMAIL服务中的发送邮件服务
results = restTemplate.postForObject("http://SENDMAIL/psend", request, String.class);
} catch (Exception e) {
return "发送失败";
}
return results;
}
}
5.webapp目录新建 send.html
<form action="/mail" method="post">
邮件接收人:<input type="text" name="send_to" value="邮件接收人的邮箱"><br/>
邮件主题:<input type="text" name="send_subject" value="柒个我"><br/>
邮件内容:<textarea rows="40" cols="50" name="send_content" >柒个不一样的我</textarea><br>
<input type="submit" value="提交">
</form>
6.启动类:Main
@RibbonClient(value="SENDMAIL") //表示当前服务需要调用的其他服务的名称
@EnableEurekaClient //该注解继承了@EnableDiscoveryClient
@SpringBootApplication //该注解继承了@Configuration
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
程序启动顺序: springcloud-eurekaserver-->springboot-sendemail-->springcloud-ribbonclient
浏览器访问:http://localhost:8088/send.html 点击提交
页面出现发送成功,则代表服务调用服务成功
最后
以上就是痴情羽毛为你收集整理的springcloud服务发现(ribbon)的全部内容,希望文章能够帮你解决springcloud服务发现(ribbon)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复