springcloud服务消费端之从消费端直接到服务提供端-03

「爱情、让人受尽委屈。」 2022-03-28 07:08 351阅读 0赞

springcloud入门详细搭建微服务环境-01

springcloud服务提供端-02

springcloud消费端是在以上的基础上搭建的:

1.导入依赖的jar包

  1. <!-- springBoot支持 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- spring boot 测试 -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-test</artifactId>
  10. </dependency>
  11. <!-- Eureka 客户端依赖 -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  15. </dependency>
  16. <!-- spring cloud 核心,自动配置 -->
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-starter-config</artifactId>
  20. </dependency>

2.写一个消费服务端的入口类

  1. package cn.mesmile;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @Created with IDEA
  6. * @author: Super Zheng
  7. * @Description: java类作用描述
  8. * @Date:2019/1/6
  9. * @Time:14:11
  10. 这里演示 直接从 消费端到服务提供端
  11. */
  12. @SpringBootApplication
  13. public class UserConsumerApplication {
  14. public static void main(String[] args) {
  15. SpringApplication.run(UserConsumerApplication.class);
  16. }
  17. }

3.写一个config类

  1. package cn.mesmile.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.client.RestTemplate;
  5. /**
  6. * @Created with IDEA
  7. * @author: Super Zheng
  8. * @Description: java类作用描述
  9. * @Date:2019/1/6
  10. * @Time:14:15
  11. */
  12. @Configuration
  13. public class ConfigBean {
  14. @Bean
  15. public RestTemplate getRestTemplate(){
  16. return new RestTemplate();
  17. }
  18. }

4.写一个消费端controller类

  1. package cn.mesmile.controller;
  2. import cn.mesmile.User;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.client.RestTemplate;
  8. /**
  9. * @Created with IDEA
  10. * @author: Super Zheng
  11. * @Description: java类作用描述
  12. * @Date:2019/1/6
  13. * @Time:14:12
  14. */
  15. @RestController
  16. @RequestMapping("/consumer")
  17. public class UserController {
  18. @Autowired
  19. private RestTemplate restTemplate;
  20. @RequestMapping("/user/{id}")
  21. public User getUser(@PathVariable(name = "id") Long id){
  22. String url = "http://localhost:8001/provider/user/"+id;
  23. return restTemplate.getForObject(url, User.class);
  24. }
  25. }

5.写配置文件

  1. server:
  2. port: 9001
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪里
  7. instance:
  8. prefer-ip-address: true #显示客户端ip
  9. spring:
  10. application:
  11. name: USER-CONSUMER #不要使用下划线

6.启动注册中心入口类,启动服务提供端入口类,启动消费服务端入口类

20190109215628444.png

发表评论

表情:
评论列表 (有 0 条评论,351人围观)

还没有评论,来说两句吧...

相关阅读