【Nacos】注册中心(二)

一、版本问题

很多人开始整合spring cloud和springboot的时候,会出现版本的的问题。现在推荐使用官方推荐版本整合问题。

地址:https://start.spring.io/actuator/info  官方推荐

二、整合

一.新建一个cloud父工程

二.父工程POM

这里我们使用的是F版本的cloud

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.kr</groupId>
    <artifactId>kr-cloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>kr-consumer</module>
        <module>kr-producer</module>
    </modules>


    <!-- springboot版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>

            <!-- 注意cloud与boot的版本 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <optional>true</optional>
        </dependency>

        <!-- 加入webClient -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

三.生产者模块(kr-producer)

1.application.yml

#注册服务名
spring:
  application:
    name: producer-nacos-server
  #nacos地址
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

#端口号
server:
  port: 8001

2.生产者工程代码

ProducerApplication.java

/**
 * Created by Janson 
 * 2020/6/3
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {

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

TestController

package cn.kr.controller;

import cn.kr.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Janson  
 * 2020/6/3
 */
@RestController
public class TestController {
    @Autowired
    private ServerConfig serverConfig;
    @GetMapping("/test")
    public String hello(@RequestParam String msg)  {
        String res = "生产者服务器(" + serverConfig.getUrl() + "):" + msg;
        System.out.println(res);
        return res;
    }
}

ServrerConfig.java

package cn.kr.config;

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 获取IP地址
 * Created by Janson 
 * 2020/6/3
 */
@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
    private int serverPort;

    public String getUrl() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "http://"+address.getHostAddress() +":"+this.serverPort;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
    }
}

四.消费者模块(kr-consumer)

1.application.yml

#注册服务名
spring:
  application:
    name: consumer-nacos-server
  #nacos地址
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

#端口号
server:
  port: 8002

2.生产者工程代码

ConsumerApplicaiton

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

TestController

package cn.kr.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Janson  
 * 2020/6/3
 */
@RestController
public class TestController {
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping("/test")
    public String test() {
        //springcloud common的负载均衡接口,提供服务名实现服务调用
        ServiceInstance serviceInstance = loadBalancerClient.choose("producer-nacos-server");
        String url = serviceInstance.getUri()+"/test?msg=nacos";
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        String msg = url + "---- return : " + result;
        System.out.println(msg);
        return msg;
    }
}

五.启动,验证

启动生产者与消费者

再浏览器中输入:http://localhost:8848/nacos

出现两个实例则是注册成功

访问:localhost:8002/test

这样,通过nacos为注册中心的就完成了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值