【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爲註冊中心的就完成了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章