建立服務的註冊與發現 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); } }
相關文章
相關標籤/搜索