• Nacos+openfeign


    1、简介

    Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

    2、准备工作

    2.1、安装 MySQL8

    从 Nacos 1.3.1 版本开始,数据库存储已经升级到8.0,并且它向下兼容

    2.2、安装Nacos

    http://ip:端口号/nacos

     

    启动配置管理

    1. 添加依赖:
      1. <dependency>
      2. <groupId>com.alibaba.cloudgroupId>
      3. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
      4. <version>${latest.version}version>
      5. dependency>

     在 application.properties 中配置 Nacos server 的地址:

    1. server.port=8070
    2. spring.application.name=service-provider
    3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

    说明:这里配置的 spring.application.name ,在启动服务后也能在Nacos 服务列表中查看到

     

    2、通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能:

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class NacosProviderApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(NacosProviderApplication.class, args);
    6. }
    7. @RestController
    8. class EchoController {
    9. @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
    10. public String echo(@PathVariable String string) {
    11. return "Hello Nacos Discovery " + string;
    12. }
    13. }
    14. }

     

    3、添加一个消费者:

    配置服务消费者,从而服务消费者可以通过 Nacos 的服务注册发现功能从 Nacos server 上获取到它要调用的服务。

    在 application.properties 中配置 Nacos server 的地址:

    1. server.port=8080
    2. spring.application.name=service-consumer
    3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

    通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能。给 RestTemplate 实例添加 @LoadBalanced 注解,开启 @LoadBalanced 与 Ribbon 的集成:

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class NacosConsumerApplication {
    4. @LoadBalanced
    5. @Bean
    6. public RestTemplate restTemplate() {
    7. return new RestTemplate();
    8. }
    9. public static void main(String[] args) {
    10. SpringApplication.run(NacosConsumerApplication.class, args);
    11. }
    12. @RestController
    13. public class TestController {
    14. private final RestTemplate restTemplate;
    15. @Autowired
    16. public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
    17. @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    18. public String echo(@PathVariable String str) {
    19. return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
    20. }
    21. }
    22. }

    4、启动 ProviderApplication 和 ConsumerApplication ,调用 http://localhost:8080/echo/2018,返回内容为 Hello Nacos Discovery 2018


    加入openFeign 

    1. <dependency>
    2. <groupId>org.springframework.cloudgroupId>
    3. <artifactId>spring-cloud-starter-openfeignartifactId>
    4. dependency>
    1. package com.formiss.feign.service;
    2. @FeignClient(value = "service-provider",path = "/provider")
    3. public interface FeignClientService {
    4. @GetMapping("/echo")
    5. String echo();
    6. }
    7. @RestController
    8. @RequestMapping(path = "/provider")
    9. public class ProviderController {
    10. @GetMapping (value = "/echo")
    11. public String echo() {
    12. return "Hello Nacos Discovery " ;
    13. }
    14. }
    15. @SpringBootApplication
    16. @EnableFeignClients(value = "com.formiss.feign.service")
    17. public class NacosConsumerApplication {
    18. public static void main(String[] args) {
    19. SpringApplication.run(NacosConsumerApplication.class, args);
    20. }
    21. }
    22. @RestController
    23. @RequestMapping("/comsumer")
    24. public class UserController {
    25. @Autowired
    26. FeignClientService feignClientService;
    27. @GetMapping("/echo")
    28. public String echo(){
    29. return feignClientService.echo();
    30. }
    31. }

    加入openfeign后 就不需要@EnableDiscoveryClient注解了

  • 相关阅读:
    狂神说Go语言学习笔记(四)
    Qt QSplitter拆分器
    金融工程学学习笔记第一章
    Android应用性能优化
    spring boot 显示数据库中图片
    Java 入门练习(16 - 20)
    ThreadLocal
    Linux中的进程程序替换
    idea中maven无法导包问题
    怎么团队合作,协作开发
  • 原文地址:https://blog.csdn.net/JemeryShen/article/details/126349035