天天看點

spring-cloud學習(一)———eureka叢集服務注冊于與發現

Eureka

1.服務端-叢集

1.叢集主機1
server:
      port: 7001
    
    eureka:
      instance:
        hostname: eureka7001.com  #eureka服務端的執行個體名稱  host檔案映射本機
      client:
        register-with-eureka: false     #false表示不向注冊中心注冊自己。
        fetch-registry: false         #false表示自己端就是注冊中心,我的職責就是維護服務執行個體,并不需要去檢索服務
        service-url:
          #單機 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #設定與Eureka Server互動的位址查詢服務和注冊服務都需要依賴這個位址(單機)。
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
          

           
2.叢集主機2
server:
      port: 7002
    
    eureka:
      instance:
        hostname: eureka7002.com  #eureka服務端的執行個體名稱  host檔案映射本機
      client:
        register-with-eureka: false     #false表示不向注冊中心注冊自己。
        fetch-registry: false           #false表示自己端就是注冊中心,我的職責就是維護服務執行個體,并不需要去檢索服務
        service-url:
          #單機 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #設定與Eureka Server互動的位址查詢服務和注冊服務都需要依賴這個位址(單機)。
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
    


           
3.叢集主機3
server:
      port: 7003
    
    eureka:
      instance:
        hostname: eureka7003.com #eureka服務端的執行個體名稱  host檔案映射本機
      client:
        register-with-eureka: false     #false表示不向注冊中心注冊自己。
        fetch-registry: false           #false表示自己端就是注冊中心,我的職責就是維護服務執行個體,并不需要去檢索服務
        service-url:
          #單機 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #設定與Eureka Server互動的位址查詢服務和注冊服務都需要依賴這個位址(單機)。
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
    

           
4.pom
<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
           

5.主啟動類

@SpringBootApplication
    @EnableEurekaServer
           

2.服務提供(注冊)

1.服務提供者1
server:
      port: 8001
    
    mybatis:
      type-aliases-package: com.qin.eurekaprovider2.entities    # 所有Entity别名類所在包
      mapper-locations:
        - classpath:mybatis/mapper/**/*.xml                     # mapper映射檔案
    
    spring:
      application:
        name: eureka-provider
      datasource:
        url: jdbc:mysql://localhost:3306/cloudDB01              # 目前資料庫
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    
    eureka:
      client: #用戶端注冊進eureka服務清單内
        service-url:
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: eureka-provider8001
        prefer-ip-address: true     #通路路徑可以顯示IP位址
 
           
2.服務提供者2
server:
      port: 8002
    
    mybatis:
      type-aliases-package: com.qin.eurekaprovider2.entities    # 所有Entity别名類所在包
      mapper-locations:
        - classpath:mybatis/mapper/**/*.xml                       # mapper映射檔案
    
    spring:
      application:
        name: eureka-provider
      datasource:
        url: jdbc:mysql://localhost:3306/cloudDB02             # 目前資料庫
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
      mvc:
        view:
          prefix: /templates/
          suffix: .html
    
    eureka:
      client: #用戶端注冊進eureka服務清單内
        service-url:
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: eureka-provider8002
        prefer-ip-address: true     #通路路徑可以顯示IP位址
    
    
           
3.服務提供者3
server:
      port: 8003
    
    mybatis:
      type-aliases-package: com.qin.eurekaprovider3.entities    # 所有Entity别名類所在包
      mapper-locations:
        - classpath:mybatis/mapper/**/*.xml                        # mapper映射檔案
    
    spring:
      application:
        name: eureka-provider
      datasource:
        url: jdbc:mysql://localhost:3306/cloudDB03               # 目前資料庫
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    
    eureka:
      client: #用戶端注冊進eureka服務清單内
        service-url:
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: eureka-provider8003
        prefer-ip-address: true     #通路路徑可以顯示IP位址

           
4.pom
<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
           
5.主啟動類
@SpringBootApplication
    @EnableEurekaClient //eureka用戶端
    @EnableDiscoveryClient  //發現服務  包括eureka 
           

3.消費者

3.1.yaml
server:
      port: 6767
    
    
    eureka:
      client:
        register-with-eureka: false
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/   

           
3.2.pom
<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

           
3.3.主啟動類
package com.qin.eurekaconsumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class EurekaConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaConsumerApplication.class, args);
        }
    
        @Bean
        @LoadBalanced  //負載均衡
        public RestTemplate getRestTemplate()  
        {
            return new RestTemplate();
        }
    }
  

           
3.4.controller
package com.atguigu.springcloud.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import com.atguigu.springcloud.entities.Dept;
    
    @RestController
    public class DeptController_Consumer
    {
    
    	//private static final String REST_URL_PREFIX = "http://localhost:8001";
        //通過服務名來擷取服務消費
    	private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
    
    	/**
    	 * 使用 使用restTemplate通路restful接口非常的簡單粗暴無腦。 (url, requestMap,
    	 * ResponseBean.class)這三個參數分别代表 REST請求位址、請求參數、HTTP響應轉換被轉換成的對象類型。
    	 */
    	@Autowired
    	private RestTemplate restTemplate;
    
    	@RequestMapping(value = "/consumer/dept/add")
    	public boolean add(Dept dept)
    	{
    		return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    	}
    
    	@RequestMapping(value = "/consumer/dept/get/{id}")
    	public Dept get(@PathVariable("id") Long id)
    	{
    		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    	}
    
    	@SuppressWarnings("unchecked")
    	@RequestMapping(value = "/consumer/dept/list")
    	public List<Dept> list()
    	{
    		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    	}
    
    	// 測試@EnableDiscoveryClient,消費端可以調用服務發現
    	@RequestMapping(value = "/consumer/dept/discovery")
    	public Object discovery()
    	{
    		return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
    	}
    
    }