1

* 创建spring-cloud项目

clipboard.png
clipboard.pngclipboard1.png

  • pom.xml引入maven依赖,指定cloud和springboot版本
<?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.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencyManagement>

<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

</dependencies>

</dependencyManagement>

</project>

* 搭建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>spring-cloud</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>

</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloud.eureka</groupId>
<artifactId>spring-eureka-server</artifactId>

<dependencies>

<!--Eureka-seve依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<!--security依赖,给Eureka服务加锁,也可以不用引依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

</dependencies>

</project>

* 配置application.yml配置文件

#指定端口号
server:
    port: 7001

#指定服务名称
spring:
    application:
        name: eureka-server

#引用security依赖时需要配置,反之不需要
security:
    user:
     name: admin
     password: 123456

#指定服务地址
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
    fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)~~~~
  server:
    enable-self-preservation: false #关闭保护模式

如果引用security依赖,需要配置。

 @EnableWebSecurity
    public class WebSecurityConfig extends  WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws  Exception {
            http.csrf().ignoringAntMatchers("/eureka/\*\*");
            super.configure(http);
        }
    }

* 配置启动类

在启动类上添加@EnableEurekaServer注解

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

* 启动Eureka服务端

引用security依赖后访问Eureka地址会出现如下图情况,输入配置文件中配置的账号密码进行登录
clipboard.png
clipboard1.png

* 搭建Eureka-Server集群

修改本地hosts文件

路径:C:\Windows\System32\drivers\etc
clipboard.png

127.0.0.1 replica1
127.0.0.1 replica2

新增两个yml文件

application-replica1.yml文件配置如下:

server:
  port: 7002
#指定服务名称
spring:
 application:
  name: eureka-server
#引用security依赖时需要配置,反之不需要
security:
 user:
  name: admin
  password: 123456
  
user-security:
 name: admin #需要注册的注册中心的security的账户
 passwd: 123456 #需要注册的注册中心security的密码
 hostname: localhost #需要注册的注册中心服务部署的路径
 port: 7003 #需要注册的注册中心的端口号

#指定服务地址
eureka:
 instance:
  hostname: replica1
  lease-expiration-duration-in-seconds: 20 #定义服务多久不去续约认为服务失效
  lease-renewal-interval-in-seconds: 5 #定义服务多久去注册中心续约
 client:
  register-with-eureka: true #指定是否要注册到注册中心(允许注册到注册中心)
  fetch-registry: true #指定是否要从注册中心获取服务(允许获取服务)
 service-url:
  defaultZone: http://${user-security.name}:${user-security.passwd}@${user-security.hostname}:${user-security.port}/eureka/ #指定注册中心地址
registry-fetch-interval-seconds: 5 #定义去eureka服务端获取服务列表的时间间隔
 server:
  enable-self-preservation: false #关闭保护模式
  eviction-interval-timer-in-ms: 2000 #扫描失效服务

application-replica2.yml文件配置如下:

server:
  port: 7003

#指定服务名称
spring:
  application:
    name: eureka-server
  #引用security依赖时需要配置,反之不需要
  security:
    user:
      name: admin
      password: 123456


user-security:
  name: admin #需要注册的注册中心的security的账户
  passwd: 123456 #需要注册的注册中心security的密码
  hostname: localhost #需要注册的注册中心服务部署的路径
  port: 7002 #需要注册的注册中心的端口号


#指定服务地址
eureka:
  instance:
    hostname: replica2
    lease-expiration-duration-in-seconds: 20 #定义服务多久不去续约认为服务失效
    lease-renewal-interval-in-seconds: 5 #定义服务多久去注册中心续约
  client:
    register-with-eureka: true #指定是否要注册到注册中心(允许注册到注册中心)
    fetch-registry: true #指定是否要从注册中心获取服务(允许获取服务)
    service-url:
      defaultZone: http://${user-security.name}:${user-security.passwd}@${user-security.hostname}:${user-security.port}/eureka/ #指定注册中心地址
    registry-fetch-interval-seconds: 5  #定义去eureka服务端获取服务列表的时间间隔
  server:
    enable-self-preservation: false #关闭保护模式
    eviction-interval-timer-in-ms: 2000 #扫描失效服务

启动时修改指定的配置文件进行启动,修改方式如下:
clipboard.png
clipboard1.png
修改启动时读取的配置文件,进行启动.

访问Eureka-server服务

clipboard.png
输入账号密码后登陆
clipboard1.png
如图,两个Eureka-Server服务相互注册.

创建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>spring-cloud</artifactId>
        <groupId>com.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-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>

</project>

配置文件如下:

server:
  port: 8080

spring:
  application:
    name: eureka-client


user-security:
  name: admin #需要注册的注册中心的security的账户
  passwd: 123456 #需要注册的注册中心security的密码
  hostname: localhost #需要注册的注册中心服务部署的路径
  port1: 7002 #需要注册的注册中心的端口号
  port2: 7003 #需要注册的注册中心的端口号

eureka:
  client:
    register-with-eureka: true #指定是否要注册到注册中心(允许注册到注册中心)
    fetch-registry: true #指定是否要从注册中心获取服务(允许获取服务)
    service-url:
      defaultZone: http://${user-security.name}:${user-security.passwd}@${user-security.hostname}:${user-security.port1}/eureka/,http://${user-security.name}:${user-security.passwd}@${user-security.hostname}:${user-security.port1}/eureka/
    registry-fetch-interval-seconds: 3 #定义去eureka服务端获取服务列表的时间间隔

启动类配置如下:

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

启动后如下图:
clipboard.png


苦乐
0 声望0 粉丝