5 minutes to build SpringCloud Eureka service registration center

Only need to keep the pom.xml file to create the parent project

Here only need to build a microservice and other operations are not

<?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>com.tyy.springcloud</groupId>
    <artifactId>cloudstudy</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <!--	这里是父级下面控制的子级 -->
    <modules>
    	<!--  服务客户端  -->
     	<module>cloud-provider-8001</module>
     	<!--  注册中心  -->
     	<module>cloud-eureka-server9001</module>
    </modules>
      <!-- 统一管理jar包版本   -->
      <!-- 具体这样 就是为了方便不在选择jar包版本号 防止jar包冲突报错 -->
    <properties>
    	<!-- 编码格式 与JDK1.8 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
	</properties>
	  <!--子模块继承之后,提供作用:锁定版本+子module不用groupId和version-->
    <dependencyManagement>
        <dependencies>
            <!--spring boot 2.2.2-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud Hoxton.SR1-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
      </dependencies>
   </dependencyManagement>   
</project>

Build the registry cloud-eureka-server9001

First of all, to build the project is basically to write pom, write configuration...

  1. pom file
<?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>cloudstudy</artifactId>
        <groupId>com.tyy.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server9001</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>
  1. aplication.yml
server:
  port: 9001
eureka:
  instance:
    hostname: eureka9001.com #eureka服务端的实例名称
  client:
    # false 表示不向注册中心注册自己
    register-with-eureka: false
    # false 表示自己就是注册中心我的职责就是维护服务实例,并不需去检查服务
    fetch-registry: false
    service-url:
      # 集群就是指向其他eureka 单机就是指向自己	
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka9001.com:9001/eureka/
    server:
      #关闭自我保护机制,保证不可用服务被及时踢除
      enable-self-preservation: false
      eviction-interval-timer-in-ms: 2000

3. Startup

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

  1. Go to the hosts file in the computer C:\Windows\System32\drivers\etc
    Insert picture description here

If you can't find it, just show the hidden file
Insert picture description here

Build the client cloud-provider-8001 and register to 9001

Still write the pom file first

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloudstudy</artifactId>
        <groupId>com.tyy.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-8001</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>
</project>

2.application.yml

server:
  port: 8001

spring:
  application:
    name: cloud-dept-service
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding-utr-8&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver

eureka:
  client:
    # 表示是否将自己注册到EurekaServer 默认true
    register-with-eureka: true
    service-url:
       defaultZone: http://eureka9001.com:9001/eureka/
  instance:
    instance-id: 8001
    prefer-ip-address: true       #访问路径显示ip地址


mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3. Startup

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

test

Is it very simple? When starting, you must start the registry first and then start the client

Insert picture description here

That's it even if it's built~!

Guess you like

Origin blog.csdn.net/weixin_44562387/article/details/115293523