Spring Boot2.0实现微服务调用

微服务相互调用可避免跨域问题

调用两种方式
1.RestTemplate
底层采用httpclient的技术,属于spring boot,是spring boot 默认采用的rebbon服务调用。
2.Fegin(建议)
属于spring cloud

====================================================================

RestTemplate:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

控制器

@RestController

public class OrderController {

     //RestTemplate 是springboot 提供的web组件,底层用的httpclient技术

    @Autowired

    private RestTemplate restTemplate; **

 

    @RequestMapping("/getmerber")

    public String getmerber(){

        //String url = "http://localhost:2001/index";  //方式一:直接调用,一般不采用

        

          //方式二:使用服务别名去注册中心获取对应的服务调用地址

          //注意这里用的是服务别名,需要开启 “注册中心” 和在启动文件添加@LoadBalanced注解

        String url="http://Merber/index";

        return restTemplate.getForObject(url,String.class); **

    }

 

}

============================

启动文件添加注入RestTemplate

    @Bean **

    @LoadBalanced

    RestTemplate restTemplate(){

        return new RestTemplate();

    }

 

 

添加@LoadBalaced可以 使用服务名字查找注册中心的地址,从而达到实现微服务调用。

如果要调用的微服务有两个端口号(有一个备用服务),即可实现负载均衡。

Fegin:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

服务提供者:提供对应的接口实现方法

服务消费者:创建对应的方法接口(表明指向服务提供者),并在控制器中调用

=======================================

 

服务提供者:

 

  创建控制器(实现接口内容)

@RestController

public class HelloController { ******

    @GetMapping("/hello/{name}")

    public String index(@PathVariable String name){

        return "this is admin ,hello!" + name;

    }

}

--------------------

  入口文件

 

@SpringBootApplication

@EnableEurekaClient

public class AmncloudAdminApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(AmncloudAdminApplication.class, args);

    }

}

-----------------

pom.xml

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-eureka</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

        </dependency>

 

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-openfeign</artifactId>

        </dependency>

 

======================================================

服务消费者:

  接口

 

@FeignClient(name= "amncloud-admin") ******重点

public interface HelloRemote {

    @GetMapping("/hello/{name}")

    public String hello(@PathVariable("name") String name);

}

------------------------

控制器

 

@RestController

public class UcenterController {

    @Autowired ******重点

    HelloRemote helloRemote;

 

    @GetMapping("/hello/{name}")

    public String index(@PathVariable("name") String name) {

        return helloRemote.hello(name);

    }

}

-------------------------

入口文件

@SpringBootApplication

@EnableFeignClients   #注意与服务提供者的不同*********重点

@EnableDiscoveryClient

public class AmncloudUcenterApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(AmncloudUcenterApplication.class, args);

    }

}

 

 

-------------------

pom.xml

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-eureka</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

        </dependency>

     <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-openfeign</artifactId>

        </dependency>

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值