spring cloud 集群guid

多环境运行配置命令


java -jar server3.jar --spring.profiles.active=eureka2

springcloud的注册中心Eureka开发:
  一: Server端:
     1.  build.gradle文件配置:

buildscript {
    ext {
        springBootVersion = '2.1.2.RELEASE'
    }
    repositories {
//        mavenCentral()
        maven {url "http://maven.aliyun.com/nexus/content/groups/public/" }

    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'springcloud.eureka'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    //mavenCentral()
    maven {url "http://maven.aliyun.com/nexus/content/groups/public/" }

}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    // https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-config', version: '2.0.2.RELEASE'

    // https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka-server', version: '1.4.6.RELEASE'


}

2.  (Eureka服务器配置为两个节点的集群模式)


application-eureka1.yml配置内容:
spring:
  application:
    name: Eureka-Server-1


eureka:
  instance:
    hostname: eureka1
  client:
    service-url:
      defaultZone: http://eureka2:8012/eureka/
#    register-with-eureka: false
#    fetch-registry: false
server:
  port: 8012


application-eureka1.yml配置内容:


spring:
  application:
    name: Eureka-Server-2


eureka:
  instance:
    hostname: eureka2
  client:
    service-url:
      defaultZone: http://eureka1:8012/eureka/
#    register-with-eureka: false
#    fetch-registry: false
server:
  port: 8012


3. 需要加入logbak.xml配置文件!!!


<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
  </appender>
  
  <logger name="com.base22" level="TRACE"/>
  

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>


4. 服务入口类:


@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

}


5.将项目打成jar包,传到充当服务器的主机,执行


    java -jar server3.jar --spring.profiles.active=eureka2

6. 在客户端主机用游览器访问Eureka.

二 编写部署生产者端
    1. build.gradle配置内容如server端,不同处:

// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: '1.4.6.RELEASE'

    2. application.yml配置内容


spring:
  application:
    name: Eureka-Provider


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8014/eureka/,http://localhost:8013/eureka/
#    register-with-eureka: false
#    fetch-registry: false
server:
  port: 9090


    3. 启动类:


@EnableEurekaClient
@SpringBootApplication
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

}

    
    4. 提供的服务内容:


@RestController
public class ShowUserController {

    @RequestMapping("/user")
    public List<User> listUser(){
        List<User> users=new ArrayList<User>();
        users.add(new User("wanger","110",21));
        users.add(new User("zhangsan","120",23));
        users.add(new User("lisi","130",26));
        return  users;
    }

}


    5. 游览器验证运行状态,server查看加入否。

三. 消费者端配置和部署


    1. build.gradle配置内容如server端,不同处:

// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: '1.4.6.RELEASE'

    2. application.yml配置内容
spring:
  application:
    name: Eureka-Consumer


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8014/eureka/,http://localhost:8013/eureka/
#    register-with-eureka: false
#    fetch-registry: false
server:
  port: 8888


    3. 启动类:


@EnableEurekaClient
@SpringBootApplication
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

}


    4. 与生产者提供的服务对接并使用:

@Service
public class UserService {
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    public List<User> listUser(){
        ServiceInstance si=this.loadBalancerClient.choose("Eureka-Provider");
        StringBuffer path=new StringBuffer();
        path.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/user");
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^ ServiceInstance get URI : "+si.getUri());

        RestTemplate rt=new RestTemplate();

        ParameterizedTypeReference<List<User>> type=new ParameterizedTypeReference<List<User>>(){};

        ResponseEntity<List<User>> responseEntity=rt.exchange(path.toString(), HttpMethod.GET,null,type );

        List<User> user=responseEntity.getBody();

        return user;

    }
}

    5. 运行,验证。

三. 额外的配置
    1. 开启/关闭Eureka的自保护
默认开启自保护

关闭自保护
    修改Eureka Server配置文件
eureka:
  server:
    enableSelfPreservation=false
    eviction
    interval-time-in-ms: 6000
#单位为毫秒
    2.优雅停服

发送关闭(shutdown)命令
    配置文件:
        #启用shutdown
            endpoints.shutdown.enabled=true
        #禁用密码验证
            endpoints.shutdown.sensitive=fales
    必须用post方式发送
    http://eureka-abc:9090/shutdown

    3.加强Eureka注册中心的安全认证
在Eureka Server中添加security包
    spring-boot-starter-security
修改Eureka Server配置文件
    #开启http basic的安全认证
    security.basic.enabled=true
    security.user.name=user
    security.user.password=123456
    #指向另外的Server要用下面这样带用户名 密码的格式
    eureka.client.service-Url.defaultZone=http://user:123456@eureka:8761/eureka/

猜你喜欢

转载自blog.csdn.net/wangxudongx/article/details/86927550
今日推荐