一、pom依赖文件内容
在(二)基础上添加下面依赖
<
dependency
>
<
groupId
>org.springframework.cloud
</
groupId
>
<
artifactId
>spring-cloud-starter-ribbon
</
artifactId
>
<
version
>1.4.4.RELEASE
</
version
>
</
dependency
>
二、配置文件application.yml内容
spring:
application:
name: cloud-consumer
server:
port:
8766
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
三、启动类内容
@EnableDiscoveryClient
@SpringBootApplication
public class CloudConsumerApplication {
public static void main(String[] args) { SpringApplication.
run(CloudConsumerApplication.
class, args); }
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate(); }}
四、使用RestTemplate调用前文(二)发布的服务
@RestController
public class ConsumerController {
@Autowired
RestTemplate
restTemplate;
@RequestMapping(
"/hello")
public String sayHello(
@RequestParam Integer id ){
return
restTemplate.getForObject(
"http://hello-service/helloClient?id=" + id, String.
class); } }
五、访问localhost:8766/hello?id=123,效果如下:
