微服务笔记(一) 搭建服务治理中心

参考《SpringCloud 微服务和分布式系统实践》学习
服务治理中心是微服务(分布式)架构中最基础和最核心的功能组件,它主要对各个服务实例进行管理,包括服务注册和服务发现等。

搭建服务治理中心


集成Eureka

  1. 首先新建一个module,并添加web和eureka依赖
<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-server</artifactId>
        </dependency>
  1. 添加注解

@SpringBootApplication
//驱动服务治理中心
@EnableEurekaServer
public class EurekaApplication {

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

  1. 配置yml文件
spring:
  application:
    name: eureka

server:
  port: 8001

eureka:
  client:
    # 取消注册自身,否则会一直报错
    register-with-eureka: false
    # 取消服务获取
    fetch-registry: false
    # 服务注册域地址
#    service-url:
#      defaultZone: http:192.168.1.100:8002/eureka
  instance:
    # 服务治理中心服务器IP
    hostname: 192.168.1.100


  1. 启动项目,打开localhost:8001

  2. 注意事项
    如果选择JDK 8(不含)以上的版本,可能会启动失败,这是因为SpringCloud的Netflix组件是依赖于JDK 8(含)之前的版本开发的,所以在新的JDK版本中会缺少一些包,因此我们需要引入新的依赖才能正常启动Eureka服务器,代码如下:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
posted @ 2021-11-26 21:31  两小无猜  阅读(105)  评论(0)    收藏  举报