Kiba518

Kiba518

沈阳-架构-开发。

Fork me on GitHub

Getway实现nacos注册及服务转发与读取Nacos配置里的list数据

Getway实现nacos注册及服务转发

首先创建一个module——kiba-getway,如下图:

image

我们新建的项目缺少resources文件夹,打开project structure——moudules,右键java,创建文件夹,如下图:

image

然后选择resources,再点击【mark a directory as a resources root】,设置resource root,如下图:

image

然后修改pom里的组件版本,文件代码如下:

<?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.kibaframework</groupId>
    <artifactId>kiba-getway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>kiba-getway</name>
    <description>kiba-getway</description>
    <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.3.12.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.SR11</spring.cloud.version> 
        <spring.cloud.alibaba.version>2.2.10-RC1</spring.cloud.alibaba.version> 
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <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>
            <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>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency> 
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <mainClass>com.example.kibagetway.KibaGetwayApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

然后在resources下添加bootstrap.yml文件,编写内容如下:

spring:
  application:
    name: kiba-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 10.1.0.145:8848
        username: nacos
        password: nacos
        auto-register: true
        reactive:
          enabled: false #关闭响应式发现服务,不需要返回消息,增加注册速度,简化复杂度
    gateway:
      discovery:
        locator:
          enabled: true #网关能够通过特定的机制发现和使用其他服务。该配置会将其他服务自动根据服务名进行映射
      httpclient:
        max-header-size: 104857600
    loadbalancer:
      retry:
        enabled: true #启动负载均衡重试


上文设置了gateway.discovery.locator.enabled=true。所以会将其他服务自动根据服务名进行映射。

项目结构很简单,如下图:

image

然后启动项目,将网关注册进nacos,如下。

image

我的nacos里已经有一个服务了。正常请求地址——localhost:8588/user/getFiegn,如下图:

image

请求网关地址——localhost:9010/app-kiba-feign/user/getFiegn,如下图:

image

网关成功转发了请求。

注:getway项目一定要最后启动,他是在启动时进行一次服务扫描,然后将服务信息自动配置进去的。如果先启动getway,那么后面启动的服务,将无法被注册进网关。

读取Nacos配置里的list数据

nacos配置

kiba:
	attack:
		white-list:
			- /weixin
			- /zhifubao

java编写类如下:

@Data
@RefreshScope
@ConfigurationProperties("kiba.attack")
public class WhiteListProperties {
    private final List<String> whiteList = new ArrayList<>();

}

使用时再类上加@EnableConfigurationProperties({WhiteListProperties.class}),Spring Boot 会自动将相关的配置属性绑定到指定的配置属性类中,方便在代码中进行使用和操作。

@Slf4j
@Component
@AllArgsConstructor
@EnableConfigurationProperties({WhiteListProperties.class})
public class XssFilter implements GlobalFilter, Ordered {
	private final ObjectMapper objectMapper;
	@Autowired
	private Environment env;

	// 设置白名单路径
	private final WhiteListProperties whiteListProperties;
}

nacos排错

错误1

nacos启动不了,肯定是首先看/soft/nacos/nacos/logs/start.out文件。但有时候这里日志输出不准确。
这时候要看nacos.log。
例如
start.out提示

Error creating bean with name 'basicAuthenticationFilter' defined in class path resource [com/alibaba/nacos/prometheus/filter/PrometheusAuthFilter.class]:

但nacos.log会提示:

"Host '10.1.0.100' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"

那这时,我们就需要在linux里进入mysql。

mysql -u root -p

然后输入密码。
然后输入flush-hosts

flush-hosts;

错误2

Error creating bean with name 'grpcSdkServer': Invocation of init method failed; nested exception is java.io.IOException: Failed to bind to address 0.0.0.0/0.0.0.0:9848 

提示这种错误,就是端口冲突,有时候我们换成8849还是不行,这时候就多换几个端口试试,比如8858。

注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!



若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!

https://www.cnblogs.com/kiba/p/18160147

posted @ 2024-04-26 15:11  kiba518  阅读(113)  评论(0编辑  收藏  举报