配置高可用的springcloud configserver

这两天业余时间学习如何搭建springcloud 高可用的configServer 和client,做些笔记

原理:将我们的configserver和 configclient都注册到eureka上,实现高可用(springboot)

一.创建eureka项目

   pom.xml

<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-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2.application.yml

server:
  port: 9010

spring:
  application:
    name: server-eureka
    
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:9010/eureka/

3.在启动类上加上@EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class SpringcloudServerEurekaApplication {

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

}

二.创建config server

1.pom.xml

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<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-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2.application.yml

server:
  port: 10030
  
spring:
  application:
    name: spring-server
  cloud:
    config:
      server:
        git:
          uri:  git地址
          username: 用户名
          password: 密码
          default-label: master
          search-paths: respo 
          
eureka: 
  client:
    healthcheck:
      enabled: true
    serviceUrl:
     defaultZone: http://localhost:10010/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
          

3.启动类

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class SpringcloudServer2Application {

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

}

三.configclient

1.pom.xml

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<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-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2.bootstrap.yml

必须是bootstrap.yml而不是application.yml,因为bootstrap文件会优先加载,service-id:是服务端注册到eureka上的application.name的名字

server:
  port: 10040
spring:
  cloud:
    config:
      #uri: http://localhost:8888
      profile: dev
      label: master   
      discovery:
        enabled: true
        service-id: spring-server
  application:
    name: config-client

eureka: 
  client:
    serviceUrl:
     defaultZone: http://localhost:10010/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
          

3.启动类

@SpringBootApplication
@EnableEurekaClient
@RestController
public class SpringcloudConfigclientApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudConfigclientApplication.class, args);
	}
	
	@Value("${foo}")
	String foo;
	
	@RequestMapping(value = "/foo")
	public String hi(){
		return foo;
	}

}

猜你喜欢

转载自blog.csdn.net/guangyingposuo/article/details/85274855