1. ribbon组件
ribbon配置文件 pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.tedu</groupId> <artifactId>springcloud-ribbon-client</artifactId> <version>1.0-SNAPSHOT</version> <!-- 此项目作为子项目继承cloud demo项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--eureka client--> <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> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
application.properties
server.port=9009 spring.application.name=server-ribbon eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka
service
package cn.tedu.service; import com.fasterxml.jackson.databind.annotation.JsonAppend; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService { @Autowired private RestTemplate client; //@bean+@loadbalance 注解,ribbon会对resttemplate发送任何请求做拦截操作,将域名寻找抓取的服务名称做为实例节点 public String sayHi(String name){ //http://service-hi/hello?name=wang String sayHi =client.getForObject("http://service-hi/hello?name="+name,String.class); return sayHi; } }
controller
package cn.tedu.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import cn.tedu.service.HelloService; @RestController public class HelloController { @Autowired private HelloService helloService; //客户端调用hi接口 @RequestMapping("hi") public String sayHi(String name){ return "ribbon"+helloService.sayHi(name); } }
启动文件
package cn.tedu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; 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; @SpringBootApplication @EnableEurekaClient public class StarterRibbon { public static void main(String[] args) { SpringApplication.run(StarterRibbon.class,args); } @Bean @LoadBalanced public RestTemplate initRestTemplate(){ return new RestTemplate(); } }