博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring cloud的的服务消费者
阅读量:4149 次
发布时间:2019-05-25

本文共 8800 字,大约阅读时间需要 29 分钟。

距上次写东西已经一个月多了,今个体验了把spring cloud的ribbon(ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。)的负责均衡功能,感觉还行,因此记一笔

都是简单的测试,贴上原理图

首先向服务中心注册一个Eureka server服务和两个Eureka client服务(做两个client是为了轮询,验证ribbon的负责均衡功能),

再注册一个服务消费者,如下图

注册成功可以在服务中心看到

通过ribbon可以看到请求轮询到两个Eureka client,如图

具体创建过程

创建一个springboot工程,然后创建三个module,分别为一个server和两个client

server的pom:

4.0.0
com.example
eurekaserver
0.0.1-SNAPSHOT
jar
eurekaserver
服务注册中心
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
UTF-8
UTF-8
1.8
Dalston.SR4
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
配置文件:

server.port=8761eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.eureka-server-u-r-l-context=http://${eureka.instance.hostname}:${server.port}/eureka/
package com.example.eurekaserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer//向注册中心注册服务@SpringBootApplicationpublic class EurekaserverApplication {	public static void main(String[] args) {		SpringApplication.run(EurekaserverApplication.class, args);	}}

两个client除了端口的基本一样:

4.0.0
com.example
eurekaclient_1
0.0.1-SNAPSHOT
jar
eurekaclient_1
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
UTF-8
UTF-8
1.8
Dalston.SR4
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
配置文件:

server.port=8762eureka.client.eureka-server-u-r-l-context=http://localhost:8761/eureka/spring.application.name=service-hi
server.port=8763eureka.client.eureka-server-u-r-l-context=http://localhost:8761/eureka/spring.application.name=service-hi
package com.example.eurekaclient_1;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@EnableEurekaClient@RestControllerpublic class Eurekaclient1Application {	public static void main(String[] args) {		SpringApplication.run(Eurekaclient1Application.class, args);	}	@Value("${server.port}")	String port;	@RequestMapping("/hi")	public String home(@RequestParam String name){		return "hi"+name+",welcome here!"+port;	}}
服务消费者

pom:

4.0.0
com.example
service-ribbon
0.0.1-SNAPSHOT
jar
service-ribbon
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
UTF-8
UTF-8
1.8
Dalston.SR4
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-ribbon
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
配置:

server.port=8764eureka.client.eureka-server-u-r-l-context=http://localhost:8761/eureka/spring.application.name=server-ribbon
package com.example.serviceribbon;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 restTemplate(){		return new RestTemplate();	}}
开始测试:

package com.example.serviceribbon.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class HelloService {    @Autowired    RestTemplate restTemplate;    public String hiService(String name){        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);    }}
package com.example.serviceribbon.controller;import com.example.serviceribbon.service.HelloService;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;@RestControllerpublic class HelloController {    @Autowired    HelloService helloService;    @RequestMapping(value="/hi")    public String hi(@RequestParam String name){        return helloService.hiService(name);    }}

注意,这个使用到@RestController的类必须在@SpringBootpplication类的同包或子包下,否则,无法加载!

转载地址:http://aftti.baihongyu.com/

你可能感兴趣的文章
构造型模式
查看>>
svn out of date 无法更新到最新版本
查看>>
java杂记
查看>>
RunTime.getRuntime().exec()
查看>>
Oracle 分组排序函数
查看>>
删除weblogic 域
查看>>
VMware Workstation 14中文破解版下载(附密钥)(笔记)
查看>>
日志框架学习
查看>>
日志框架学习2
查看>>
SVN-无法查看log,提示Want to go offline,时间显示1970问题,error主要是 url中 有一层的中文进行了2次encode
查看>>
NGINX
查看>>
Qt文件夹选择对话框
查看>>
DeepLearning tutorial(5)CNN卷积神经网络应用于人脸识别(详细流程+代码实现)
查看>>
DeepLearning tutorial(6)易用的深度学习框架Keras简介
查看>>
DeepLearning tutorial(7)深度学习框架Keras的使用-进阶
查看>>
流形学习-高维数据的降维与可视化
查看>>
Python-OpenCV人脸检测(代码)
查看>>
python+opencv之视频人脸识别
查看>>
人脸识别(OpenCV+Python)
查看>>
6个强大的AngularJS扩展应用
查看>>