SpringCloud 中使用 Ribbon的方法详解

网友投稿 249 2023-01-20


SpringCloud 中使用 Ribbon的方法详解

在前两章已经给大家讲解了Ribbon负载均衡的规则 以及 如何搭建Ribbon并调用服务,那么在这一章呢 将会给大家说一说如何在SpringCloud中去使用Ribbon。在搭建之前 我们需要做一些准备工作。

1. 搭建Eureka服务器:springCloud-ribbon-server(项目名称)

2. 服务提供者:springCloud-ribbon-police(项目名称)

3. 服务调用者:springCloud-ribbon-person(项目名称)

搭建Eureka服务器

配置 pom.xml,加入springCloud核心依赖、配置及eureka服务器依赖

org.springframework.boot

spring-boot-starter-parent

1.5.13.RELEASE

org.springframework.cloud

spring-cloud-dependencies

Dalston.SR5

pom

import

org.springframework.cloud

spring-cloud-starter-config

org.springframework.cloud

spring-cloud-starter-eureka-server

配置 application.yml(红色部分是必须要写的,黑色部分不写也能正常运行 但是建议写上,在这里笔者将官网的代码贴上)

server:

port: 8761

eureka:

instance:

hostname: localhost

client:

registerWithEureka: false 禁止向eureka注册服务,因为它自己本身就是服务器

fetchRegistry: false 这里不需要抓取注册表

serviceUrl:

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

创建启动类:Application.java(将服务跑起来放着,稍后会用到)配置 pom.xml,加入springCloud核心依赖、配置及eureka服务依赖

@SpringBootApplication

@EnableEurekaServer

public class Application {

public static void main(String[] args) {

new SpringApplicationBuilder(Application.class).web(true).run(args);

}

}

服务提供者

配置 pom.xml,加入springCloud核心依赖、配置及eureka客户端依赖

org.springframework.boot

spring-boot-starter-parent

1.5.13.RELEASE

org.springframework.cloud

spring-cloud-dependencies

Dalston.SR5

pom

import

org.springframework.cloud

spring-cloud-starter-config

org.springframework.cloud

spring-cloud-starter-eureka

配置 application.yml(需要使用defaultZone向服务器注册服务,否则就算该服务运行起来了,但没有向服务器注册服务,也是使用不了的)(name 这个名称是显示在服务列表中的名称,养成好习惯,一定要起有意义的名称)

spring:

application:

name: springCloud-ribbon-police

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8761/eureka/

因为该服务是提供服务的,所以下面会建一个实体类及Controller用来对外提供服务,创建实体类:Police.java

public class Police {

private String id;// 警察编号,用来保存用户输入的参数

private String url;// 处理请求的服务器url

private String message;// 提示信息

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

}

创建对外提供服务的Controller:PoliceController.java(@RestController注解中包含了@Controller+@ResponseBody)

@RestController

public class PoliceController {

@RequestMapping(value="/getPolice", method=RequestMethod.GET, produces=MediaType.APPLICATION_jsON_VALUE)

public Police getPolice(HttpServletRequest request){

Police p = new Police();

p.setUrl(request.getRequestURL().toString());

p.setMessage("警察派出成功");

return p;

}

@RequestMapping(value="/getPoliceById/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)

public Police getPolice(HttpServletRequest request, @PathVariable("id") String id){

Police p = new Police();

p.setId(id);

p.setUrl(request.getRequestURL().toString());

p.setMessage("指定警察派出成功");

return p;

}

}

因为我们要测试负载均衡,所以这里的服务提供者需要开启多个服务实例,下面我们用读取手动输入端口号的方法,启动多个服务实例,笔者在这里启动了两个服务实例:8080、8081

@SpringBootApplication

@EnableEurekaClient

public class PoliceApplication {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String port = scan.nextLine();

new SpringApplicationBuilder(PoliceApplication.class).properties("server.port="+port).run(args);

}

}

如下图,出现了两个服务实例,分别是:8080、8081,红色的信息咱们先不管他,如果实在有看着不顺眼的小伙伴,可以配置心跳(简单的来说,就是配置服务器每隔多久检查一次服务实例状态,如果某个服务因为某些原因停掉 不能用了,那么就将该服务 从服务列表中移除掉)

服务调用者

配置 pom.xml,加入springCloud核心依赖、配置及eureka客户端依赖、Ribbon依赖

org.springframework.boot

spring-boot-starter-parent

1.5.13.RELEASE

org.springframework.cloud

spring-cloud-dependencies

Dalston.SR5

pom

import

org.springframework.cloud

spring-cloud-dependencies

Dalston.SR5

pom

import

org.springframework.cloud

spring-cloud-starter-config

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.cloud

spring-cloud-starter-ribbon

配置 application.yml(这里也将该服务注册到服务器,一定要进行注册)

server:

port: 9090

spring:

application:

name: springCloud-ribbon-person

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8761/eureka/

创建调用服务Controller:PersonController.java

RestTemplate 是由 Spring Web 模块提供的工具类,与 SpringCloud 无关,是独立存在的

因 SpringCloud 对 RestTemplate 进行了一定的扩展,所以 RestTemplate 具备了负载均衡的功能

@RestController

@Configuration

public class PersonController {

@Bean

@LoadBalanced

public RestTemplate getRestTemplate(){

return new RestTemplate();

}

@RequestMapping("/getPolice")

public String getPolice(){

RestTemplate rt = getRestTemplate();

String result = rt.getForObject("http://springCloud-ribbon-police/getPolice", String.class);

return result;

}

@RequestMapping("/getPoliceById/{id}")

public String getPoliceById(@PathVariable("id") String id){

RestTemplate rt = getRestTemplate();

String result = rt.getForObject("http://springCloud-ribbon-police/getPoliceById/"+id, String.class);

return result;

}

}

创建启动类:PersonApplication.java

@SpringBootApplication

@EnableEurekaClient

public class PersonApplication {

public static void main(String[] args) {

new SpringApplicationBuilder(PersonApplication.class).web(true).run(args);

}

}

到目前为止,eureka服务器、服务提供者、服务调用者(负载均衡)就已经全写好了,下面我们访问接口,来试一下 服务到底能不能调通

我们分别调用:http://localhost:9090/getPolice、http://localhost:9090/getPoliceById/100

总结

以上所述是给大家介绍的SpringCloud 中使用 Ribbon的方法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot集成dubbo注解版的示例代码
下一篇:程序员常用接口管理工具(程序员常用接口管理工具包括)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~