springcloud(一)-集成Eureka 服务注册与发现(慕课网廖师兄SpringCloud微服务实战)...

1.服务中心

  • 核心依赖
<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
	</dependencies>


	<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>
复制代码
  • 启用Eureka Server

在启动类或者配置类上添加@EnableEurekaServer 注解

/**
 * Eureka 启动类
 * @author gaowenfeng
 *
 * @EnableEurekaServer  启用Eureka Server
 */
@SpringBootApplication
@EnableEurekaServer
public class MicroWeatherEurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(MicroWeatherEurekaServerApplication.class, args);
	}
}
复制代码
  • 配置

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    # 静止注册成客户端
    register-with-eureka: false
    fetch-registry: false
    # 服务的url
    service-url:
      defaultZone: http://{eureka.instance.hostname}:{server.port}/eureka
复制代码
  • 检查

启动项目后访问 http://{host}:{serverport}

2. 服务注册

  • 核心依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <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>
复制代码
  • 注册服务相关代码实现

启动类或者配置类添加@EnableDiscoveryClient注解

/**
 * 启动类
 * @author gaowenfeng
 */
@SpringBootApplication
@EnableDiscoveryClient
public class MsaWeatherCityServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(MsaWeatherCityServerApplication.class, args);
	}
}
复制代码
  • 相关配置

spring:
  application:
    name: msa-weather-city-server

eureka:
    # 服务中心的url
    service-url:
      # 这里可以是一个数组,也就是可以注册多多个服务端,用逗号隔开
      defaultZone: http://localhost:8761/eureka/
      instance:
           # 自定义服务的域名
           hostname: clientName
server:
  port: 9991
复制代码
  • 检查

启动实例,即可在服务中心界面以及控制台看到相关注册信息

  • Eureka高可用

让多个Eureka服务端两两注册,即一个Eureka做为另一个Eureka的客户端,然后让Client注册到每一个Eureka服务端上,这样,当一个服务端挂掉以后,在其他的Eureka服务端也可以发现注册的Client

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值