SpringCloud服務註冊中心叢集搭建(二)
阿新 • • 發佈:2019-01-12
springcloud學習總結
2、服務註冊中心叢集搭建
一、新建服務註冊中心eureka7002模組,拷貝eureka7001模組的pom以及yml
修改yml檔案
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服務端的例項名稱
client:
register-with-eureka: false #false表示不向註冊中心註冊自己。
fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務
service- url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
編寫主啟動類。並標註@EnableEurekaServer
二、新建服務註冊中心eureka7002模組,拷貝eureka7001模組的pom以及yml
修改yml檔案
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #eureka服務端的例項名稱
client:
register-with-eureka: false #false 表示不向註冊中心註冊自己。
fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務
service-url:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
編寫主啟動類。並標註@EnableEurekaServer
三、修改eureka7001的application.yml檔案
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服務端的例項名稱
client:
register-with-eureka: false #false表示不向註冊中心註冊自己。
fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務
service-url:
#defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
四、修改provider8001的application.yml檔案
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置檔案所在路徑
type-aliases-package: com.atguigu.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: com.mysql.jdbc.Driver # mysql驅動包
url: jdbc:mysql://localhost:3306/cloudDB01 # 資料庫名稱
username: root
password: 123321
dbcp2:
min-idle: 5 # 資料庫連線池的最小維持連線數
initial-size: 5 # 初始化連線數
max-total: 5 # 最大連線數
max-wait-millis: 200 # 等待連接獲取的最大超時時間
eureka:
client: #客戶端註冊進eureka服務列表內
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: microservicecloud-dept8001 #自定義服務名稱資訊
prefer-ip-address: true #訪問路徑可以顯示IP地址
#
info:
app.name: atguigu-microservicecloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
五、修改consumer80的application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
修改服務處理類
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://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);
}
}
啟動三個服務註冊中心,啟動服務提供者與服務消費者,傳送消費請求檢視。
後續:服務發現與負載均衡ribbon…