第一种方式LoadBalancerClient
-
依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
-
配置文件
spring.application.name=eureka-consumer server.port=3333 #服务注册中心 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
-
application
@EnableDiscoveryClient @SpringBootApplication public class Application { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
-
controller
@RestController public class DemoController { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired LoadBalancerClient loadBalancerClient; @Autowired RestTemplate restTemplate; @GetMapping("/hello") public String hello(){ ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client"); logger.info("host:"+serviceInstance.getHost()+"---port:"+serviceInstance.getPort()+"---uri"+serviceInstance.getUri()); String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/hello"; String forObject = restTemplate.getForObject(url, String.class); return forObject; } }
第二种方式Ribbon
-
依赖
在一种方式下添加依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
配置文件修改端口号即可
修改Application.java,在RestTemplate上添加注解
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
controller修改为
@GetMapping("/hello2")
public String hello2(){
return restTemplate.getForObject("http://eureka-client/hello",String.class);
}
第三种方式Feign
-
依赖
在第一种方式下添加依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
配置文件修改端口号即可
Application.java添加注解@EnableFeignClients
支持Feign客户端
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
创建一个接口
@FeignClient("eureka-client")
设置使用的服务名称
@FeignClient("eureka-client")
public interface DemoService {
@GetMapping("/hello")
String hello();
}
controller
@RestController
public class DemoController {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
DemoService demoService;
@GetMapping("/hello")
public String hello(){
return demoService.hello();
}
}
三种方式可以混合使用,第二种方式因为将RestTemplate对象添加了注解,所以第一种方式就不能直接使用了,只能利用其它方式发送http请求获取数据。