博客
关于我
Nacos作为服务注册中心演示
阅读量:115 次
发布时间:2019-02-26

本文共 4036 字,大约阅读时间需要 13 分钟。

基于 Nacos 的服务提供者与消费者配置

服务提供者配置

1. 模块创建

在父工程下新建一个module,命名为 cloudalibaba-provider-payment-9001

2. 依赖项添加

在项目的 pom.xml 中添加以下依赖:

com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery

3. 服务注册配置

application.yml 中添加以下内容:

server:    port: 9001spring:    application:        name: nacos-payment-provider    cloud:        nacos:            discovery:                server-addr: 172.28.129.83:8848            management:                endpoints:                    web:                        exposure:                            include: '*'

4. 主启动类

创建一个主启动类 PaymentApplication9001

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class PaymentApplication9001 {    public static void main(String[] args) {        SpringApplication.run(PaymentApplication9001.class, args);    }}

5. 业务接口开发

创建 PaymentController

import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class PaymentController {    @Value("${server.port}")    private String serverPort;    @GetMapping(value = "/payment/nacos/{id}")    public String getPayment(@PathVariable("id") Integer id) {        return "nacos registry, serverPort: " + serverPort + "\t id=" + id;    }}

服务消费者配置

1. 模块创建

在父工程下新建一个module,命名为 cloudalibaba-provider-payment-9002

2. 服务发现配置

修改 application.yml 中的端口号为 9002,并保留服务发现地址:

server:    port: 9002spring:    application:        name: nacos-payment-provider-9002    cloud:        nacos:            discovery:                server-addr: 172.28.129.83:8848

3. 主启动类

创建一个主启动类 PaymentApplication9002

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class PaymentApplication9002 {    public static void main(String[] args) {        SpringApplication.run(PaymentApplication9002.class, args);    }}

负载均衡演示

1. 消费者配置

新建一个module,命名为 cloudalibaba-consumer-nacos-order-83

2. 服务发现配置

application.yml 中添加以下内容:

server:    port: 83spring:    application:        name: nacos-order-consumer    cloud:        nacos:            discovery:                server-addr: 172.28.129.83:8848            service-url:                nacos-user-service: http://nacos-payment-provider

3. 负载均衡实现

创建 ApplicationContextConfig

import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class ApplicationContextConfig {    @Bean    @LoadBalanced    public RestTemplate restTemplate() {        return new RestTemplate();    }}

创建 OrderNacosController

import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;@RestController@Slf4jpublic class OrderNacosController {    @Resource    private RestTemplate restTemplate;    @Value("${service-url.nacos-user-service}")    private String serverURL;    @GetMapping(value = "/consumer/payment/nacos/{id}")    public String paymentInfo(@PathVariable("id") Long id) {        log.info("请求路径: /consumer/payment/nacos/{}", id);        return restTemplate.getForObject(serverURL + "/payment/nacos/" + id, String.class);    }}

4. 测试验证

通过访问 http://localhost:83/consumer/payment/nacos/1,可以看到服务请求会自动切换到不同提供者的实例,说明 Nacos 已成功进行负载均衡。

转载地址:http://jhfk.baihongyu.com/

你可能感兴趣的文章
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>