SpringCloudマイクロサービスはXiaobai(Hoxton.SR8)でも使用できます(1)Eureka |サービスの登録と検出

開始して直接コピーするだけで、マイクロサービス(Hoxton.SR8)を構築できます。2020.8.28がリリースされ、SpringCloudによって構築された記事が整理されています。乾物をお見逃しなく
https://spring.io/blog/2020/08/28/spring- cloud-hoxton-sr8-has-been-released

1.親pomプロジェクトを作成します

注意:

  1. Spring Assistantはプラグインです。自分でインストールする必要があります、インストールチュートリアルリファレンス
  2. 3番目の図は、以前は必須のコンポーネントの選択はありませんでした。ideajのバージョンである必要があります。問題ありません。最初にコンポーネントを選択し、作成後に削除します。
  3. 私たちが使用するspringbootバージョンは2.2.10です
  4. 最後に、プロジェクト名とプロジェクトの場所を選択します

   

1.1現段階での親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>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.10.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.zqh.www</groupId>
    <artifactId>cloud-hoxton-sr8</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-hoxton-sr8</name>
    <description>Hoxton.SR8微服务</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <dependencies>
    </dependencies>

    <!-- https://spring.io/blog/2020/08/28/spring-cloud-hoxton-sr8-has-been-released -->
    <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>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.Eurekaレジストリを作成します

注意:

  1. プロジェクトを右クリックしてモジュールを作成し、Mavenを選択し、[次へ]をクリックして、プロジェクト名を入力します

   

2.1現段階でのeureka-serverプロジェクトの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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-hoxton-sr8</artifactId>
        <groupId>com.zqh.www</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
         <!-- 给Eureka注册中心添加认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

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

2.2EurekaServerApplicationとapplication.ymlを構成します

注意:

  1. ideajは、パッケージパス、アプリケーションファイル、およびapplication.ymlの生成に役立たなかったので、これが私自身の作成です。
package com.zqh.www;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.core.env.Environment;

/**
 * 开启eureka服务
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    private final static Logger logger = LoggerFactory.getLogger(EurekaServerApplication.class);

    public static void main(String[] args) {
        Environment env = SpringApplication.run(EurekaServerApplication.class, args).getEnvironment();
        logger.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t"
                        + "Local: \t\thttp://localhost:{}{}"
                        + "\n----------------------------------------------------------",
                env.getProperty("spring.application.name"), env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path") != null ? env.getProperty("server.servlet.context-path") : "");
    }
}
server:
  port: 8081
spring:
  application:
    name: eureka-server
  security:
    user:
      # 配置spring security登录用户名和密码
      name: root
      password: root
eureka:
  client:
    # 让自己不需要注册在上面禁止客户端注册,表明自己是一个eureka server
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  instance:
    hostname: localhost
package com.zqh.www.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * 默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

  

2.3eurekaサービスページへのブラウザアクセス

注:クリックしてhttp:// localhost:8081にアクセスします  アカウント:rootパスワード:root

3.クライアントを作成して登録します

注意:

  1. プロジェクトを右クリックしてモジュールを作成し、Mavenを選択し、[次へ]をクリックして、プロジェクト名を入力します
  2. 2.2の手順に従って、パッケージパス、EurekaClientApplicationおよびapplication.ymlを作成します。

   

3.1現段階でのeureka-clientプロジェクトの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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-hoxton-sr8</artifactId>
        <groupId>com.zqh.www</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.2EurekaClientApplication和application.yml 

package com.zqh.www;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.core.env.Environment;

/**
 * 开启eureka客户端
 */
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

    private final static Logger logger = LoggerFactory.getLogger(EurekaClientApplication.class);

    public static void main(String[] args) {
        Environment env = SpringApplication.run(EurekaClientApplication.class, args).getEnvironment();
        logger.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t"
                        + "Local: \t\thttp://localhost:{}{}"
                        + "\n----------------------------------------------------------",
                env.getProperty("spring.application.name"), env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path") != null ? env.getProperty("server.servlet.context-path") : "");
    }
}
server:
  port: 8083
spring:
  application:
    name: eureka-client
eureka:
  client:
    service-url:
      #注册地址
      defaultZone: http://root:root@localhost:8081/eureka/
  #显示服务器IP加端口
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

4.ブラウザのeurekaサービスページを更新すると、サービスの登録が完了します。

5.まとめ

  • eurekaサービスの開始を完了し、セキュリティ認証の検証を追加します
  • 完全なクライアント登録eureka

6.giteeアドレス

ソースリファレンス

おすすめ

転載: blog.csdn.net/itjavaee/article/details/109045823