在windows環境下搭建consul,可以參考我的上一篇博客:https://blog.csdn.net/j903829182/article/details/80960802
在這裏進行一個小實戰,使用consul作爲我們的註冊和配置中心,並實現RPC調用,利用Feign組件來實現RPC調用。
一:創建一個maven項目作爲api
使用IDEA創建一個maven項目,這個項目裏面主要作爲接口api,接口代碼如下:
package com.jack.api;
/**
* create by jack 2018/7/8
*/
public interface Hello {
public String sayHello(String name);
}
pom.xml代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud4</artifactId>
<groupId>com.jack</groupId>
<version>pom</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>consul-api</artifactId>
<version>1.0.0</version>
</project>
二:服務提供者
1,創建一個springboot項目,pom.xml代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jack</groupId>
<artifactId>consul_study2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consul_study2</name>
<description>Demo project for Spring Boot</description>
<!--<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>-->
<parent>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-parent -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Finchley.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--feign依賴 配置-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<!--consul中健康檢查需要用到actuator,不添加會check failing-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jack</groupId>
<artifactId>consul-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2,配置文件代碼如下:
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
#healthCheckPath: ${management.contextPath}/health
healthCheckPath: /health
healthCheckInterval: 15s
instance-id: consul2
enabled: true
enabled: true
application:
name: consul2
server:
port: 8082
3,接口實現代碼:
package com.jack.consul_study2.api;
import com.jack.api.Hello;
import org.springframework.stereotype.Service;
/**
* create by jack 2018/7/8
*/
@Service
public class ChineseHelloImpl implements Hello {
@Override
public String sayHello(String name) {
return "我是中國人,我說漢語,我的名字是:"+name;
}
}
4,開啓服務註冊和發現
package com.jack.consul_study2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
//@EnableFeignClients
public class ConsulStudy2Application {
public static void main(String[] args) {
SpringApplication.run(ConsulStudy2Application.class, args);
}
}
上面使用的註解時@EnableDiscoveryClient來實現服務註冊與發現的
5,編寫一個consul健康檢查的控制器,不然進行服務調用會不成功,代碼如下:
package com.jack.consul_study2.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by jack 2018/7/8
*/
@RestController
public class HealthController {
@RequestMapping("/health")
public String health(){
return "health";
}
}
6,服務提供的控制器代碼如下:
package com.jack.consul_study2.controller;
import com.jack.api.Hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by jack 2018/7/8
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private Hello hello;
@RequestMapping("/say")
public String sayHello(String name){
return hello.sayHello(name);
}
}
到這裏,服務提供者的基本代碼就完成了,下面進行服務消費,即服務調用的開發
三:服務調用
1,創建另外一個springboot項目,pom.xml代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jack</groupId>
<artifactId>consul_study1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consul_study1</name>
<description>Demo project for Spring Boot</description>
<!--<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>-->
<parent>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-parent -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Finchley.RELEASE</version>
<relativePath/>
</parent>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<!--<version>2.0.0.RELEASE</version>-->
</dependency>
<!--feign依賴 配置-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.jack</groupId>
<artifactId>consul-api</artifactId>
<version>1.0.0</version>
</dependency>
<!--consul中健康檢查需要用到actuator,不添加會check failing-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2,啓動主類的代碼如下:
package com.jack.consul_study1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsulStudy1Application {
public static void main(String[] args) {
SpringApplication.run(ConsulStudy1Application.class, args);
}
}
主要是使用了兩個註解:
@EnableDiscoveryClient:服務註冊與發現
@EnableFeignClients:開啓Feign調用的功能
3,配置文件如下:
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
#healthCheckPath: ${management.contextPath}/health
healthCheckPath: /health
healthCheckInterval: 15s
instance-id: consul1
enabled: true
enabled: true
application:
name: consul1
server:
port: 8081
4,遠程調用的接口如下:
package com.jack.consul_study1.api;
import com.jack.api.Hello;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* create by jack 2018/7/8
*/
@FeignClient("consul2")
public interface Chinese extends Hello {
@RequestMapping(value = "/hello/say")
@Override
String sayHello(String name);
}
5,健康檢查的控制器代碼如下:
package com.jack.consul_study1.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by jack 2018/7/8
*/
@RestController
public class HealthController {
@RequestMapping("/health")
public String health(){
return "health";
}
}
6,測試的控制器代碼如下:
package com.jack.consul_study1.controller;
import com.jack.consul_study1.api.Chinese;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by jack 2018/7/8
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private Chinese chinese;
@RequestMapping("/hello")
public String testHello(String name){
return chinese.sayHello(name);
}
}
到此,代碼基本已經開放完成了,下面進行測試
四,測試
1,啓動兩個springboot項目,在consul的界面顯示如下:
2,測試接口
在postman輸入:http://localhost:8081/test/hello?name=jack
輸出如下:
源代碼地址:
https://github.com/wj903829182/springcloud5/tree/master/consulapi
https://github.com/wj903829182/springcloud5/tree/master/consul_study2
https://github.com/wj903829182/springcloud5/tree/master/consul_study1
總結:
1,consul的健康檢查還有點疑問,網上找資料也是說可以自定義一個控制器路徑來完成健康檢查,但是理論上,覺得應該不需要,還需深入的研究。
2,這裏基本實現了consul作爲服務的註冊與發現,但是是在windows的單機下進行的,沒有做consul集羣,consul集羣和在linux下安裝consul另外分享。
3,consul還可以用來作爲配置中心,相當於springcloud的config,後續也會進行分享
歡迎加羣:331227121,一起學習交流