 SpringCloud之服务注册与发现
SpringCloud之服务注册与发现
 # SpringCloud之服务注册与发现

# 创建服务端程序
(1)新建一个SpringBoot项目(Spring Cloud 基于 Spring Boot)
依赖:
<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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(2)启动类上加上注解 @EnableEurekaServer
使用该注解,可以将项目作为SpringCloud中的注册中心

(3)配置SpringCloud
server.port=8761
#服务名
spring.application.name=eureka-server
#表示是否从Eureka Server获取注册的服务信息
eureka.client.fetch-registry=false
#服务注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#表示是否将自己注册到Eureka Server
eureka.client.register-with-eureka=false
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
(4)启动服务

# 创建客户端服务
# 新建一个SpringBoot项目,引入cloud相关依赖:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 启动类添加注解@EnableDiscoveryClient

@EnableDiscoveryClient和@EnableEurekaClient的区别
- 前者是基于spring-cloud-commons依赖,并且在classpath中实现; 
- 后者是基于spring-cloud-netflix依赖,只能为eureka作用; 
为什么有两个注解,spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等),而后者只针对Eureka,简单说如果你注册中心选用Eureka,那么可以用@EnableEurekaClient,选其他的话用@EnableDiscoveryClient
# 模拟一个订单接口
/**
 * @Author Mr.Fire
 * @Desc 订单服务
 * @Date 2021/12/14
 */
@RestController
public class OrderService {
    @Autowired
    private Environment environment;
    @RequestMapping("/order")
    public String order(){
        String port = environment.getProperty("server.port");
        String order = "hello ,this is a order,port="+port;
        return order;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 添加配置
#服务名
spring.application.name=cloud-client
#服务端口
server.port=8081
#是否检索服务
eureka.client.fetch-registry=false
#指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#是否向服务中心注册自己
eureka.client.register-with-eureka=true
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 启动服务,访问Eureka注册中心

可以看到服务已经注册到服务中心,服务名为cloud_cilent,端口8080
最后更新时间: 2024/03/15, 17:35:22
