spring-cloud微服务之【naocs-discovery(注册与发布)-springcloud】--持续更新

启动服务发现

本节通过实现一个简单的 echo service 演示如何在您的 Spring Cloud 项目中启用 Nacos 的服务发现功能。

pom.xml添加依赖(服务发现与注册)

 <dependency>
     <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 </dependency>

yml配置

服务提供者(provider)yml配置

server:
  port: 9091

spring:
  cloud:
    nacos:
      discovery:
        server-addr: http://101.132.99.21:8848
        register-enabled: true #默认为true意思就是需要注册上去
  application:
    name: service-provider #这个到时候基于ribbon负载均衡的时候,客户端调用的时候使用的是这个名字哦
#属性说明
#spring.cloud.nacos.discovery.server-addr 配置nacos地址
#spring.cloud.nacos.discovery.register-enabled 默认为true,表示是否将自己注册到nacos中
#========默认是注册
#本yml演示的是provider服务提供者,所以自然而然的需要将自己注册到nacos中

服务消费者(consumer)yml配置

server:
  port: 9099

spring:
  cloud:
    nacos:
      discovery:
        server-addr: http://101.132.99.21:8848
        register-enabled: false #默认为true意思就是需要注册上去
  application:
    name: service-consumer
#register-enabled设置为false,表示不要把自己注册到nacos中,其他都是一样的。
#记得自己定义服务端口

代码

服务提供者代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
//@EnableDiscoveryClient【我不加,注意】
public class NacosProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }

    @RestController
    class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return "Hello Nacos Discovery " + string;
        }
    }
}

说明:

  • Spring Cloud Edgware开始,@EnableDiscoveryClient 或@EnableEurekaClient 可省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。【也就是说springcloud E版本开始以后就不需要加了】

服务消费者代码

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
public class NacosConsumerApplication {

    @LoadBalanced//启用客户端基于ribbon的负载均衡功能
    //这样就可以使用支持使用注册中心的服务名称进行调用
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/consumer/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://example/echo/" + str, String.class);
        }
        //使用基于ribbon负载均衡,然后调用使用的是注册到nacos的服务名称
      
    }
}

另外还可以使用基于openFegin的调用,详情参考springcloud-openFeign章节。

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值