srpingcloud ribbon

按照 EurekaClient 项目创建流程再创建一个 ribbon 项目

application.properties文件和pom文件配置相同

修改配置文件application.properties端口为10002

启动class中添加如下代码

@RestController
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

    //注册RestTemplate
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


    @GetMapping("/movie/{name}")
    public String findById(@PathVariable String name) {
        System.out.println("ribbon="+name);
        // VIP: Virtual IP http://microservice-provider-user/即虚拟IP 服务提供者的ServiceId (spring.application.name)
        return this.restTemplate().getForObject("http://eureka-client/hi?name=" + name, String.class);
    }

}

启动项目

访问 http://127.0.0.1:10002/movie/name

 

 已经实现服务间的相互调用,从ribbon项目调用client项目。ribbon可以实现负载,现在要在创建一个client项目。

复制EurekaClient 项目,修改成eureka_client_fb名称,修改启动端口号为10003,启动项目。

http://127.0.0.1:8761/ 如图

 

 eureka-clent 服务有两个项目注册在eureka上,端口为10001,10003

重新启动ribbon项目,访问http://127.0.0.1:10002/movie/name

会负载到两个eureka-clent 服务上,结果分别为

hi name,i am from port:10003

hi name,i am from port:10001

 

posted @ 2021-01-21 18:30  音Duang  阅读(115)  评论(0编辑  收藏  举报