建立服务的注册与发现 Eureka (四)

1、eureka注册中心

一、建立一个工程

工程名:microservicecloud-eureka-7001mysql

二、向pom文件中增长以下:

<dependencies>
        <!--eureka-server服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
</dependencies>

三、添加application.yml文件

server:
  port: 7001
eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    register-with-eureka: false #false表示不向注册中心注册本身。Eureka不响本身注册
    fetch-registry: false       #false表示本身端就是注册中心,个人职责就是维护服务实例,并不须要去检索服务
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ (单机)
      #设置与Eureka Server交互的地址查询服务和注册服务都须要依赖这个地址(单机)。
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

四、增长服务启动类,并在类上增长  @EnableEurekaServer 注解, 以下所示:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer7001.class, args); } }

 

五、启动服务,在浏览器中打开 http://localhost:7001,显示以下图

 

2、服务注册到Eureka

一、建立工程

工程名:microservicecloud-provider-dept-8001spring

二、在pom.xml文件,新增Eureka的客户端依赖,以下:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
</dependency>    

三、配置文件application.yaml,新增以下内容:

eureka:
  client:  # 客户端注册进eureka内
    service-url:
      defaultZone: http://localhost:7001/eureka/

application.yaml中的所有内容以下:

server:
  port: 8001

mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.yufeng.springcloud.entities    # 全部Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件

spring: application: name: microservicecloud-dept
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操做类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://192.168.172.20:3306/cloudDB01         # 数据库名称
    username: root
    password: root
    dbcp2:
      min-idle: 5                                           # 数据库链接池的最小维持链接数
      initial-size: 5                                       # 初始化链接数
      max-total: 5                                          # 最大链接数
      max-wait-millis: 200                                  # 等待链接获取的最大超时时间


eureka:
  client:  # 客户端注册进eureka内
    service-url:
      defaultZone: http://localhost:7001/eureka/

 

四、代码的启动类中增长 @EnableEurekaClient 注解;

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

五、按照顺序启动服务,先启动 eureka7001 服务,接着启动 microservicecloud-provider-dept-8001 服务,浏览器打开: http://localhost:7001 

 

3、服务的发现

对于注册到Eureka里面的微服务,能够经过服务发现来得到该服务的信息。sql

一、在服务提供的微服务的controller注入DiscoveryClient类

@RestController public class DeptController { @Autowired //@Resource(name = "deptServiceImpl")
    private DeptService service;  @Autowired private DiscoveryClient discoveryClient; @RequestMapping(value = "/dept/add", method = RequestMethod.POST) public boolean add(@RequestBody Dept dept) { return service.add(dept); } @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET) public Object discovery() { List<String> list = discoveryClient.getServices(); //发现eureka中的微服务列表 System.out.println("eureka中全部的微服务的name列表" + list); //从eureka中获取指定name的微服务的信息(yml文件中配置的 spring.application.name) List<ServiceInstance> instances = discoveryClient.getInstances(list.get(0)); for(ServiceInstance instance : instances) { System.out.println(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri()); } return this.discoveryClient; } }

二、启动类中增长 @EnableDiscoveryClient 注解

@SpringBootApplication @EnableEurekaClient //本服务启动后自动注册到eureka中
@EnableDiscoveryClient  //服务的发现, 暴露出来
public class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.class, args); } }
相关文章
相关标签/搜索