SpringCloud-Eureka叢集搭建
1.SpringCloud環境搭建
-
建立一個maven專案(刪除src目錄):父工程
-
匯入相關依賴(jar包)
<!--打包方式--> <packaging>pom</packaging> <!-- <properties>--> <!-- <!–引入版本號–>--> <!-- <junit.version>4.12</junit.version>--> <!-- <lombok.version>1.18.10</lombok.version>--> <!-- <log4j.version>1.2.17</log4j.version>--> <!-- </properties>--> <dependencyManagement> <dependencies> <!--配置SpringCloud的依賴--> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR3</version> <type>pom</type> <scope>import</scope> </dependency> <!--配置SpringBoot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--配置資料庫--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <!--SpringBoot啟動器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </dependencyManagement>
-
在建立一個Maven專案:子工程(實體類層)
1.引入相關jar包
<!--當前的Module自己需要的依賴,如果父類依賴中配置了版主,這裡就不用寫了--> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
2.連線資料庫(mysql)
3.建立實體類:Dept
package com.ddf.springcloud.pojo; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; //實體類需要序例化 @Data @NoArgsConstructor @Accessors(chain = true) //鏈式寫法 public class Dept implements Serializable { //Dept實體類, orm 類表關係對映 private Long deptno; //主鍵 private String dname; //這個資料庫存在哪個資料庫的欄位 微服務,一個服務對應一個資料庫,同一個資訊可能存在不同的資料庫 private String db_source; public Dept(String dname) { this.dname = dname; } /* * 鏈式寫法 * Depet dept = new Dept(); * dept.setDeptNo(11).setDname('ssss').setDb.source('001') * * */ }
-
在建立一個Maven專案:子工程(供應者)
1.引入相關jar包
<dependencies> <!--我們需要拿到實體類,所有要配置api module--> <dependency> <groupId>org.example</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--引用父類的jar包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jetty--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <!--熱部署工具,可以不用頻繁的重啟專案--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
2.建立application.yml檔案,配置相關的配置
server: port: 8001 #mybatis配置 mybatis: type-aliases-package: com.ddf.springcloud.pojo configuration-properties: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mapper/*.xml #spring的配置 spring: application: name: springcloud-provider-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db01?userUnicode=true&characterEncoding=UTF-8 username: root password: 123
3.建立對應的mybatis和mapper檔案目錄,配置mybatis-config.xml檔案
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--開啟二級快取--> <setting name="cacheEnabled" value="true"/> </settings> </configuration>
DeptDao.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ddf.springcloud.dao.DeptDao"> <insert id="addDept" parameterType="Dept"> insert into dept(dname,db_source) values(#{dname},DATABASE()); </insert> <select id="findById" resultType="Dept" parameterType="Long"> select * from dept where deptno = #{deptno} </select> <select id="findAll" resultType="Dept"> select * from dept; </select> </mapper>
4.建立dao介面類:DeptDao
package com.ddf.springcloud.dao; import com.ddf.springcloud.pojo.Dept; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface DeptDao { //新增 public boolean addDept(Dept dept); //通過id查詢 public Dept findById(Long id); //查詢所有 public List<Dept> findAll(); }
5.建立service層:DeptService和DeptServiceImpl
package com.ddf.springcloud.service; import com.ddf.springcloud.pojo.Dept; import java.util.List; public interface DeptService { //新增 public boolean addDept(Dept dept); //通過id查詢 public Dept findById(Long id); //查詢所有 public List<Dept> findAll(); }
package com.ddf.springcloud.service; import com.ddf.springcloud.dao.DeptDao; import com.ddf.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DeptServiceImpl implements DeptService { @Autowired private DeptDao deptDao; public boolean addDept(Dept dept) { return deptDao.addDept(dept); } public Dept findById(Long id) { return deptDao.findById(id); } public List<Dept> findAll() { return deptDao.findAll(); } }
6.建立controller層:DeptController
package com.ddf.springcloud.controller; import com.ddf.springcloud.pojo.Dept; import com.ddf.springcloud.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; //提供Restful服務 @RestController public class DeptController { @Autowired private DeptService deptService; @PostMapping("/dept/add") public boolean addDept(Dept dept){ return deptService.addDept(dept); } @GetMapping("/dept/get/{id}") public Dept getDept(@PathVariable Long id){ return deptService.findById(id); } @GetMapping("/dept/list") public List<Dept> findAll(){ return deptService.findAll(); } }
7.建立測試類,測試專案是否能夠正常執行
package com.ddf.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //設定啟動類 @SpringBootApplication public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class,args); } }
-
在建立一個Maven專案(子專案)消費者
1.引入相關需要的jar包
<!--引入實體類+web--> <dependencies> <dependency> <groupId>org.example</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
2.建立application.yml檔案配置埠號
server: port: 80
3.建立Controller層:DeptConsumerController
package com.ddf.springcloud.controller; import com.ddf.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class DeptConsumerController { //理解:消費者,應該有service層 //RestTemplate 供我們直接呼叫就可以了,註冊到Spring中 @Autowired private RestTemplate restTemplate; //通過restTemplate去獲取服務端的對應方法 //定義服務端請求路徑 private static final String REST_URL_PREFIX="http://localhost:8001"; @GetMapping("/comsumer/dept/add") public Boolean add(Dept dept){ //(url,實體:map,class<T> responseType) //url:請求路徑 //實體:引數 //responseType:請求的型別 return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add",dept,Boolean.class); } @GetMapping("/comsumer/dept/get/{id}") public Dept get(@PathVariable Long id){ return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id,Dept.class); } @GetMapping("/comsumer/dept/list") public List<Dept> list(){ return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list",List.class); } }
4.建立config層:configBean(注入RestTemplate供我們使用)
package com.ddf.springcloud.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; //@Configuration類似於我們之前用spring的application.xml檔案 @Configuration public class configBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
5.建立測試類,看是否能夠訪問到8001埠(先啟動8001在啟動80埠)
package com.ddf.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //設定啟動類 @SpringBootApplication public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class,args); } }
8001埠訪問:http://localhost:8001/dept/get/1
80埠訪問:http://localhost/comsumer/dept/get/1;就可以訪問到8001埠的資料
2.配置Eureka
-
在建立一個子專案
-
引入相關jar包
<!--導包--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--熱部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
-
配置application.yml檔案
server: port: 7001 #配置Eureka eureka: instance: hostname: localhost #Eureka伺服器的例項名稱 client: register-with-eureka: false #表示是否想eureka註冊中心註冊自己 fetch-registry: false #fetch-registry如果為false,則表示自己為註冊中心 service-url: #監控頁面 defaultZon: http://${eureka.instance.hostname}:${server.port}/eureka/
-
建立測試類
package com.ddf.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //EnableEurekaServer 服務端的啟動類,可以接受別人註冊進來 public class EurekaServer_7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer_7001.class, args); } }
?訪問:http://localhost:7001/;就能進入到Eureka的頁面
-
注意事項
在這裡需要降一下springboot的版本,要不然會報錯:把它降到Springboot2.1.6就好了
3.把Eureka配置到8001埠中
-
匯入相關jar
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency>
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-8EjgErTl-1604756922767)(C:\Users\11111\AppData\Roaming\Typora\typora-user-images\image-20201021212352457.png)]
-
在8001專案中的application.yml配置Eureka
#eureka的配置 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/
-
在測試類中開啟Eureka
package com.ddf.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; //設定啟動類 @SpringBootApplication @EnableEurekaClient //在服務啟動後自動註冊到Eureka中,開啟Eureka的連線 public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class,args); } }
先啟動7001專案然後在啟動8001專案,然後訪問:http://localhost:7001/我們就可以看到我們註冊的8001埠號
-
完善Eureka的監控資訊
- 在8001專案引入jar包
<!--actuator完善監控資訊--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- 在8001專案中的application.yml配置Eureka
#eureka的配置 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: springcloud-provider-dept8001 #修改eureka上的Status #配置Status的資訊 info: app.name: ddf-springcloud company.name: blog.ddf.com
然後訪問:http://localhost:7001/;點選Status下的路徑,我們就可以看到我們配置的資訊
-
Eureka自我保護機制
出現這個就是Eureka自我保護機制,說明某個埠號出現了問題斷開了連線,需要重新開啟才可以連線
-
注入DiscoveryClient實現服務註冊與發現
1.在8001專案DeptController注入DiscoveryClient
package com.ddf.springcloud.controller; import com.ddf.springcloud.pojo.Dept; import com.ddf.springcloud.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; //提供Restful服務 @RestController public class DeptController { @Autowired private DeptService deptService; //獲取一些配置的資訊,得到具體的微服務 @Autowired private DiscoveryClient client; @PostMapping("/dept/add") public boolean addDept(Dept dept){ return deptService.addDept(dept); } @GetMapping("/dept/get/{id}") public Dept getDept(@PathVariable Long id){ return deptService.findById(id); } @GetMapping("/dept/list") public List<Dept> findAll(){ return deptService.findAll(); } //通過註冊進來的微服務,去獲取一些訊息 @GetMapping("/dept/discovery") public Object discovery(){ //獲取微服務列表的清單 List<String> services = client.getServices(); System.out.println("獲取到了微服務的資訊:" + services); //得到一個具體的微服務新,通過具體的微服務id,application List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT"); //列印出相關資訊 for(ServiceInstance instance : instances){ System.out.println(instance.getHost()); System.out.println(instance.getPort()); System.out.println(instance.getUri()); System.out.println(instance.getServiceId()); } return this.client; } }
- 在啟動類中新增@EnableDiscoveryClient註解
package com.ddf.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; //設定啟動類 @SpringBootApplication @EnableEurekaClient //在服務啟動後自動註冊到Eureka中,開啟Eureka的連線 @EnableDiscoveryClient //服務發現 public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class,args); } }
- 執行8001專案:訪問http://localhost:8001/dept/discovery,就會得到這個資訊
相關文章
- 搭建zookeeper叢集(偽叢集)
- zookeeper叢集及kafka叢集搭建Kafka
- linux下搭建ZooKeeper叢集(偽叢集)Linux
- Redis系列:搭建Redis叢集(叢集模式)Redis模式
- 搭建ELK叢集
- Ambari叢集搭建
- kafka叢集搭建Kafka
- Hadoop搭建叢集Hadoop
- zookeeper 叢集搭建
- 搭建 Redis 叢集Redis
- nacos 叢集搭建
- mysql叢集搭建MySql
- redis叢集搭建Redis
- Hadoop叢集搭建Hadoop
- Zookeeper叢集搭建
- RabbitMQ叢集搭建MQ
- HBASE叢集搭建
- 【環境搭建】RocketMQ叢集搭建MQ
- 4.4 Hadoop叢集搭建Hadoop
- Redis(5.0) 叢集搭建Redis
- MySQL 5.7 叢集搭建MySql
- 搭建spark on yarn 叢集SparkYarn
- ZooKeeper 搭建 solr 叢集Solr
- 搭建Redis原生叢集Redis
- 搭建MongoDB分片叢集MongoDB
- MySQL MGR 叢集搭建MySql
- 【greenplum】greenplum叢集搭建
- Kubernetes 叢集搭建(上)
- Kubernetes 叢集搭建(下)
- MongoDB 分片叢集搭建MongoDB
- ElasticSearch 7.8.1叢集搭建Elasticsearch
- Redis--叢集搭建Redis
- Docker 搭建叢集 MongoDBDockerMongoDB
- zookeeper叢集的搭建
- Hadoop叢集搭建(一)Hadoop
- Kubernetes叢集搭建(vagrant)
- ONOS叢集的搭建
- 搭建redis cluster叢集Redis