什么是服务治理?

在传统 rpc 远程调用中,服务与服务依赖关系,管理比较复杂,所以需要使用服 务治理,管理服务与服务之间依赖关系,可以实现服务调用、负载均衡、容错等, 实现服务发现与注册。

服务注册与发现

在服务注册与发现中,有一个注册中心,当服务器启动的时候,会把当前自己 服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。 另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服 务通讯地址,让后在实现本地 rpc 调用远程。

搭建注册中心

1、引入pom依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent> <!-- 管理依赖 --> <dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.M7</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement> <dependencies> <!--SpringCloud eureka-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories>
<repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/libs-milestone</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>
</repositories>

2、添加application.yml

###服务端口号
server:
  port: 8100
##定义服务名称
spring:
  application:
    name: app-cjbdi-eureka
eureka:
  instance:
    ###注册中心 ip 地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
    ##注册地址
      defaultZone: http://${eureka.instance.hostname}:9100/eureka/
    ####因为自己是注册中心,是否需要将自己注册给自己的注册中心(集群的时候是需要是为 true)
    register-with-eureka: false
    ###因为自己是注册中心, 不需要去检索服务信息
    fetch-registry: true

3、启动 Eureka 服务

@EnableEurekaServer 
@SpringBootApplication 
public class AppEureka { 
	public static void main(String[] args) { 	
		SpringApplication.run(AppEureka.class, args);
	}
}

4、打开浏览器输入localhost:8100查看自己的注册中心

5、注册服务到Eureka,创建服务,引入pom依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent> <!-- 管理依赖 --> <dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.M7</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement> <dependencies> <!-- SpringBoot 整合 Web 组件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- SpringBoot 整合 eureka 客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
        <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories>
<repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/libs-milestone</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>
</repositories>

6、注册服务到Eureka,引入application.yml

###会员项目的端口号
server:
  port: 8000
  ###服务别名----服务注册到注册中心名称
spring:
  application:
  name: app-cjbdi-member
eureka:
  client:
    service-url:
    ##### 当前会员服务注册到 eureka 服务地址
      defaultZone: http://localhost:8100/eureka

7、启动服务

@SpringBootApplication 
@EnableEurekaClient 
public class AppMember { 
	public static void main(String[] args) { 
		SpringApplication.run(AppMember.class, args); 
     	}
 }

@EnableEurekaClient 注解是代表客户端注册到注册中心eureka

8、打开浏览器输入localhost:8100查看自己的注册中心eureka是否有注册进去服务