本文共 4692 字,大约阅读时间需要 15 分钟。
一、在前一章的基础上启动eureka-server和两个eureka-client实例(在application.properties更改下端口号)新增一个模块service-ribbon
修改父工程的pom.xml文件在<Modules>节点处增加service-ribbon
eureka-server eureka-client service-ribbon
service-ribbon的pom.xml的内容如下:
4.0.0 service-ribbon 0.0.1-SNAPSHOT jar service-ribbon Demo project for Spring Boot com.example chapter2 0.0.1-SNAPSHOT UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.cloud spring-cloud-starter-netflix-eureka-client 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
配置文件application-properties内容如下:
#server 地址eureka.client.service-url.defaultZone=http://localhost:8001/eureka/#一个server 8001 两个client分别为8002 8003server.port=8004#实例名spring.application.name=service-ribbon
启动类ServiceRibbonApplication的内容如下:
package com.example;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.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;/** * 通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean * @author xiaobu */@EnableEurekaClient@EnableDiscoveryClient@SpringBootApplicationpublic class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } /*** * @author xiaobu * @date 2018/11/6 11:32 * @return org.springframework.web.client.RestTemplate * @descprition restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。 * @version 1.0 */ @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); }}
service-ribbon的目录结构如下
定义一个消费接口ClientService
package com.example.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;/** * @author xiaobu * @version JDK1.8.0_171 * @date on 2018/11/6 11:34 * @descrption */@Servicepublic class ClientService { @Autowired RestTemplate restTemplate; /*** * @author xiaobu * @date 2018/11/6 11:42 * @param name 名字 * @return java.lang.String * @descprition 直接用的程序名替代了具体的url地址, * 在ribbon中它会根据服务名来选择具体的服务实例, * 根据服务实例在请求的时候会用具体的url替换掉服务名 * @version 1.0 */ public String clientService(String name){ return restTemplate.getForObject("http://eureka-client/test?name=" + name, String.class); }}
再定义个controller
package com.example.controller;import com.example.service.ClientService;import com.sun.org.glassfish.gmbal.ParameterNames;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * @author xiaobu * @version JDK1.8.0_171 * @date on 2018/11/6 11:36 * @description V1.0 */@RestControllerpublic class ClientController { @Autowired ClientService clientService; @GetMapping("/test") public String test(@RequestParam String name){ return clientService.clientService(name); }}
多次访问 出现的效果如下:
转载地址:http://lugai.baihongyu.com/