• Spring Cloud Alibaba Nacos 中处理Consumer的UnknownHostException的问题



    参考:https://blog.csdn.net/ilyz5609/article/details/113400203

    问题的原因是 Spring Cloud Alibaba 2020 之后弃用了 spring-cloud-starter-netflix-ribbon ,而改用 loadbalancer


    Spring Cloud Alibaba 版本对应参考: https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E


    ---------------------------------父级pom.xml----------------------------

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.sparkmap</groupId>
      <artifactId>micro-service</artifactId>
      <version>0.0.1</version>
      <packaging>pom</packaging>
      <name>micro-service</name>
    
      <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    
        <spring-boot.version>2.4.2</spring-boot.version>
        <spring-cloud.version>2020.0.1</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
        <spring-security-oauth2.version>2.5.1.RELEASE</spring-security-oauth2.version>
    
        <slf4j.version>1.7.32</slf4j.version>
        <logback.version>1.2.6</logback.version>
        <junit.version>4.13.2</junit.version>
        <commons-lang3.version>3.12.0</commons-lang3.version>
        <commons-io.version>2.11.0</commons-io.version>
        <commons-compress.version>1.21</commons-compress.version>
        <commons-text.version>1.9</commons-text.version>
        <commons-net.version>3.8.0</commons-net.version>
        <commons-math3.version>3.6.1</commons-math3.version>
        <commons-dbcp2.version>2.9.0</commons-dbcp2.version>
        <fastjson.version>1.2.78</fastjson.version>
        <dom4j.version>2.1.3</dom4j.version>
        <jackson.version>2.13.0</jackson.version>
    
        <httpcore.version>4.4.14</httpcore.version>
        <httpclient.version>4.5.13</httpclient.version>
    
        <druid.version>1.2.8</druid.version>
        <postgresql.version>42.2.24</postgresql.version>
        <postgis.version>2.5.1</postgis.version>
        <jts.version>1.18.2</jts.version>
        <geotools.version>25.2</geotools.version>
        <proj4j.version>1.1.3</proj4j.version>
        <gdal.version>3.2.0</gdal.version>
    
        <maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
      </properties>
    
      <modules>
        <module>nocas-consumer</module>
        <module>nocas-provider</module>
      </modules>
    
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
          <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
    
          <!--replace spring security bom intetval -->
          <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>${security.oauth.version}</version>
          </dependency>
    
          <!-- druid -->
          <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>${dbcp.version}</version>
          </dependency>
    
          <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>${dom4j.version}</version>
          </dependency>
    
          <!--jackson -->
          <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
          </dependency>
          <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
          </dependency>
          <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
          </dependency>
    
          <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>${commons-compress.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>${commons-math3.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>${commons-text.version}</version>
          </dependency>
    
          <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>${commons-net.version}</version>
          </dependency>
    
          <!--  HTTP  -->
          <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>${httpcore.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>${httpclient.version}</version>
          </dependency>
    
          <!--    JTS   -->
          <dependency>
            <groupId>org.locationtech.jts</groupId>
            <artifactId>jts-core</artifactId>
            <version>${jts.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.locationtech.proj4j</groupId>
            <artifactId>proj4j</artifactId>
            <version>${proj4j.version}</version>
          </dependency>
    
          <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
          </dependency>
    
          <dependency>
            <groupId>net.postgis</groupId>
            <artifactId>postgis-jdbc</artifactId>
            <version>${postgis.version}</version>
          </dependency>
    
          <!-- log -->
          <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
          </dependency>
    
          <!--  logback -->
          <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
          </dependency>
    
          <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
          </dependency>
    
          <!--  Test -->
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
          </dependency>
    
        </dependencies>
      </dependencyManagement>
    
    
      <build>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <version>${spring-boot.version}</version>
              <configuration>
                <layers>
                  <enabled>true</enabled>
                </layers>
              </configuration>
              <executions>
                <execution>
                  <goals>
                    <goal>repackage</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    
    </project>




    ---------------------------------服务提供者----------------------------

    pom.xml


    <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/maven-v4_0_0.xsd">
      <parent>
        <artifactId>micro-service</artifactId>
        <groupId>com.sparkmap</groupId>
        <version>0.0.1</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.sparkmap</groupId>
      <artifactId>nacos-provider</artifactId>
      <version>0.0.1</version>
      <packaging>jar</packaging>
      <name>nacos-provider</name>
      <url>http://maven.apache.org</url>
    
      <dependencies>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <!-- <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency> -->
    
        <!-- <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency> -->
    
        <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    
      </dependencies>
    
    </project>


    application.yml


    spring:
      application:
        name: nacos-provider
      cloud:
        nacos:
          username: nacos
          password: nacos
          discovery:
            enabled: true
            server-addr: 192.168.2.18:8848
            group: nacos_dev
          # config:
          #   server-addr: 192.168.2.18:8848
          #   file-extension: yaml
    server:
      port: 8081


    ProviderApplication.java


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


    DemoContoller.java


    package com.demo;
    
    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.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class DemoContoller {
      
      @Value("${server.port}")
      private String port;
    
      @GetMapping(value = "/demo/{string}")
      public String demoTesting(@PathVariable("string") String string){
          return "hello" + string + ": " + port;
      }
    }




    ---------------------------------服务使用者----------------------------

    pom.xml

    <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/maven-v4_0_0.xsd">
      <parent>
        <artifactId>micro-service</artifactId>
        <groupId>com.sparkmap</groupId>
        <version>0.0.1</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.sparkmap</groupId>
      <artifactId>nacos-consumer</artifactId>
      <version>0.0.1</version>
      <packaging>jar</packaging>
      <name>nacos-consumer</name>
      <url>http://maven.apache.org</url>
    
      <dependencies>
    
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    
        <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
          <exclusions>
            <exclusion>
              <!--   配置禁用ribbon  -->
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>          
            </exclusion>
          </exclusions>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    
        <!--   采用loadbalancer   -->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-loadbalancer</artifactId>      
        </dependency>
    
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    
        <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
    
      </dependencies>
    
    </project>


    application.yml

    spring:
      application:
        name: nacos-consumer
      cloud:
        nacos:
          username: nacos
          password: nacos
          discovery:
            enabled: true
            server-addr: 192.168.2.18:8848
            group: nacos_dev
          # config:
          #   server-addr: 192.168.2.18:8848
          #   file-extension: yaml
        # sentinel:
        #   eager: true
        #    transport:
        #      dashboard: localhost:8080
    # feign:
    #   sentinel:
    #     enabled: true
    
    server:
      port: 8082
    management: 
      endpoints: 
        web: 
          exposure: 
            include: "*"
    # ribbon:
    #   nacos:
    #     enabled: false


    ConsumerApplication.java

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


    RestConfig.java

    package com.demo;
    
    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;
    
    @Configuration
    public class RestConfig {
    
        //@LoadBalanced实现负载均衡,有多个服务提供者,一定要使用,不然会报错
        @LoadBalanced
        @Bean    
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
      
    }


    PrintController.java

    package com.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class PrintController {
    
      @Value("${server.port}")
      private String port;
    
      // @Value("${service.name.provider}")
      // private String webServerName;
    
      @Autowired
      private RestTemplate restTemplate;
    
      @GetMapping(value = "/port")
      public String echo() {
        //
        String webServerName = "nacos-provider";
        return "I'm port: " + port + ", " + restTemplate.getForObject("http://" + webServerName + "/demo/hi", String.class);
      }
    
    }
  • 相关阅读:
    Windows 10 开发日记(二)-- 手势顺序调研
    Windows 10 开发日记(一) -- 手势通览
    开篇说两句
    我从上海回苏州了,你懂的!
    数组名相关
    指针相关笔记
    static相关
    输入分式,输出化简式的C语言算法
    语句界定
    switch相关
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/15413568.html
Copyright © 2020-2023  润新知