hooyantsing's Blog

第127次课程_SpringCloud_Helloworld_服务注册与发现

字数统计: 1.8k阅读时长: 9 min
2020/03/18

源辰76班

第127次课程

2020.03.18

内容

SpringCloud_Helloworld_服务注册与发现[廖彦]

项目路径:

1.集群、分布式和微服务

**57da03486e1efa9eee04c1e962dbc7cf.png

**

2.抽取pom.xml内的公共内容,作为父pom.xml

[pom文件的继承关系]

关系树状图

4a4d6dcab00a1fbf2eda04e22d6ce1eb.png

pom:不作为具体开发,父工程pom.xml

0d09e99aa8cbcc76ac8bd8690286e14f.png

将所有子工程都需要的配置作为 C76-S3-Ply-sc-parent1/pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<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  https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.yc</groupId>
     <artifactId>C76-S3-Ply-sc-parent1</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>pom</packaging>
     <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>2.2.2.RELEASE</version>
           <relativePath /> <!-- lookup parent from repository  -->
     </parent>
     <properties>
           <java.version>1.8</java.version>
           <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
     </properties>
     <dependencies>
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
           </dependency>
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                     <exclusion>
                           <groupId>org.junit.vintage</groupId>
                           <artifactId>junit-vintage-engine</artifactId>
                     </exclusion>
                </exclusions>
           </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>
     <build>
           <plugins>
                <plugin>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
           </plugins>
     </build>
</project>

将所有应用模块工程都需要的配置作为 C76-S3-Ply-sc-parent2/pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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  https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <artifactId>C76-S3-Ply-sc-parent2</artifactId>
     <packaging>pom</packaging>
     
     <!-- 父pom.xml是C76-S3-Ply-sc-parent1/pom.xml -->
     <parent>
           <groupId>com.yc</groupId>
           <artifactId>C76-S3-Ply-sc-parent1</artifactId>
           <version>0.0.1-SNAPSHOT</version>
           <relativePath /> <!-- lookup parent from repository  -->
     </parent>
</project>

3.每个工程设定不同端口,可同时启动

application.yml

1
2
server:
  port: 8001

**4.服务互访 **RestTemplate

IndexApplication.java

通过@Bean,创建RestTemplate类对象****并注入IOC容器,方便其他xxxAction引用这个对象。

访问地址: http://127.0.0.1/user/way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.yc.index;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class IndexApplication {

    public static void main(String[] args) {
        SpringApplication.run(IndexApplication.class, args);
    }
    
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

}

IndexAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.yc.index.web;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class IndexAction {
    
    @Resource
    private RestTemplate restTemplate;
    
    @GetMapping("index/way")
    public String index() {
        return "index";
    }
    
    @GetMapping("user/way")
    public String user() {
        String url = "http://127.0.0.1:8001/user/way";
        String ret = restTemplate.getForObject(url, String.class);
        return ret;
    }

}

UserAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.yc.user.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserAction {
    
    @GetMapping("user/way")
    public String user() {
        return "user";
    }

}

5.服务注册与发现 Eureka服务器

名词解释:

服务端****:指的是server,用于建立eureka服务器

客户端****:指的是业务模块(index order user),启动后将自动注册到server里。

5.1 分布式

配置eureka服务器依赖

C76-S3-Ply-sc-server/pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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  https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <!-- 父pom.xml是C76-S3-Ply-sc-parent1/pom.xml -->
     <parent>
           <groupId>com.yc</groupId>
           <artifactId>C76-S3-Ply-sc-parent1</artifactId>
           <version>0.0.1-SNAPSHOT</version>
           <relativePath /> <!-- lookup parent from repository  -->
     </parent>
     <artifactId>C76-S3-Ply-sc-server</artifactId>
     <name>C76-S3-Ply-sc-server</name>
     <description>Demo project for Spring Boot</description>
     <dependencies>
           <!-- eureka-server:eureka服务端 -->
           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
           </dependency>
           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
           </dependency>
     </dependencies>
     
</project>

C76-S3-Ply-sc-parent2/pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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  https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <artifactId>C76-S3-Ply-sc-parent2</artifactId>
     <packaging>pom</packaging>
     
     <!-- 父pom.xml是C76-S3-Ply-sc-parent1/pom.xml -->
     <parent>
           <groupId>com.yc</groupId>
           <artifactId>C76-S3-Ply-sc-parent1</artifactId>
           <version>0.0.1-SNAPSHOT</version>
           <relativePath /> <!-- lookup parent from repository  -->
     </parent>
     
     <dependencies>
           <!-- eureka-client:eureka客户端 -->
           <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>

服务器

ServerApplication.java

server启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.yc.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
//使能eureka服务器
@EnableEurekaServer
public class ServerApplication {

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

application.yml

1
2
3
4
5
6
7
8
9
10
# 工程配置 ==> 默认的服务名
spring:
   application:
      name: sc-server
server:
   port: 8000
eureka:
   client:
      registerWithEureka: false
      fetchRegistry: false

客户端

各个业务模块(Index Order User)工程配置

以Index业务模块工程为例:

IndexApplication.java

index启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.yc.index;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class IndexApplication {

    public static void main(String[] args) {
        SpringApplication.run(IndexApplication.class, args);
    }
    
    //负载均衡
    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

application.yml

1
2
3
4
5
6
7
8
9
10
11
server:
   port: 80
# 名称
spring:
   application:
      name: sc-index
eureka:
   client:
      serviceUrl:
         # 注册服务器的地址
         defaultZone: http://127.0.0.1:8000/eureka

5.2 集群

客户端

以user业务模块工程为例:

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 没有指定  spring.profiles 是默认的分区配置
server:
   port: 8001
spring:
   application:
      name: sc-user
eureka:
   client:
      serviceUrl:
         # 注册服务器的地址
         defaultZone: http://127.0.0.1:8000/eureka
# java 运行参数:--spring.profiles.active=cu1
#  mvn 运行参数:-Dspring.profiles.active=cu1
# yml 分区配置
---
# 分区配置名称
spring:
  profiles: cu1
server:
  port: 8005
---
spring:
  profiles: cu2
server:
  port: 8006

同一个启动类如何多次启动?

第一步:

a967ac028ebe504b6e09ed04273fec28.png

第二步:

java 运行参数:–spring.profiles.active=cu1

mvn 运行参数:-Dspring.profiles.active=cu1

1ef071ce45f5dcc27e2a9bd99d013e96.png

分布式和集群区别

图取自eureka服务器后台,即server默认页。

16be4a81be3a4bd2098b3ce5209c4c63.png

CATALOG
  1. 1. java 运行参数:–spring.profiles.active=cu1
  2. 2. mvn 运行参数:-Dspring.profiles.active=cu1