SpringCloud 使用consul作為微服務註冊中心
阿新 • • 發佈:2019-01-08
eureka宣佈閉源,使用consul作為服務註冊中心。
1、parent pom檔案
<?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.yyh</groupId> <artifactId>springcloudlearn</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <modules> <module>consulservice1</module> <module>consulservice2</module> <module>consulservice3</module> <module>consulclient1</module> <module>consulclient3</module> <module>consulclient4</module> <module>consulservice3interface</module> </modules> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
2、微服務介面pom
<?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>springcloudlearn</artifactId> <groupId>com.yyh</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>consulservice3interface</artifactId> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.3</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
3、微服務介面實體類
package com.yyh.entity; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class Customer { private int id; private String firstName; private String lastName; public Customer() { } public Customer(int id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } public int getId() { return id; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public void setId(int id) { this.id = id; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } }
4、微服務介面 HelloService介面
package com.yyh.entity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
public interface HelloService {
@RequestMapping(value = "say")
String say();
@RequestMapping(value = "s/{id}")
Customer s(@PathVariable("id") String id);
}
5、微服務提供者pom,在pom中依賴上一個介面
<?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>springcloudlearn</artifactId>
<groupId>com.yyh</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consulservice3</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 注意 依賴介面-->
<dependency>
<groupId>com.yyh</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>consulservice3interface</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--用於監控與管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul</artifactId>
</dependency>
<!--服務發現依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!--用於consul配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6、微服務提供者application.properties
spring.application.name=consulservice03
server.port=8003
spring.cloud.consul.discovery.instance-id=consulservice03
# 微服務健康監測url
spring.cloud.consul.discovery.health-check-path= /actuator/health
# consul註冊中心地址
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
7、微服務提供者具體實現HelloService介面
package com.yyh.springcloudlearn.consulservice3.controller;
import com.yyh.entity.Customer;
import com.yyh.entity.HelloService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2018/12/21 0021.
*/
@RestController
@RequestMapping("hello")
public class HelloController implements HelloService {
@Value("${spring.application.name}")
private String serviceName;
@Value("${server.port}")
private int servicePort;
@Override
public String say() {
return "hello world! I'm '" + serviceName + ":" + servicePort + "'";
}
@Override
public Customer s(@PathVariable("id") String id) {
Customer customer = new Customer();
customer.setLastName(id);
return customer;
}
}
8、微服務提供者啟動類
package com.yyh.springcloudlearn;
import com.yyh.entity.Customer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulService3App {
@Value("${spring.application.name}")
private String serviceName;
@Value("${server.port}")
private int servicePort;
@RequestMapping("/say")
public String say(){
return "hello world! I'm '" + serviceName + ":" + servicePort + "'";
}
@RequestMapping("/s/{id}")
public Customer s(@PathVariable("id") String id) {
Customer customer = new Customer();
customer.setLastName(id);
return customer;
}
public static void main(String[] args){
SpringApplication.run(ConsulService3App.class, args);
}
}
9、微服務消費者pom
<?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>springcloudlearn</artifactId>
<groupId>com.yyh</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consulclient4</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--注意 依賴介面 -->
<dependency>
<groupId>com.yyh</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>consulservice3interface</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--用於監控與管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul</artifactId>
</dependency>
<!--服務發現依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!--用於consul配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<!--Feign的依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--斷路器依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--斷路器面板-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
</project>
10、微服務消費者application.properties
spring.application.name=consulclient04
server.port=8014
feign.hystrix.enabled=true
# 微服務健康監測url
spring.cloud.consul.discovery.health-check-path= /actuator/health
# consul註冊中心地址
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#SpringCloud-Feign 第一次請求timeout問題
#索性禁用Feign的Hystrix
#hystrix.metrics.enabled=false
#SpringCloud-Feign 第一次請求timeout問題
feign.httpclient.connection-timeout=10000
11、微服務消費者FeignClient
package com.yyh.service;
import com.yyh.entity.HelloService;
import org.springframework.cloud.openfeign.FeignClient;
/**
* Created by Administrator on 2018/12/21 0021.
*/
@FeignClient(value = "consulservice03",fallback = FallbackHelloService.class)
public interface HelloServiceConsumer extends HelloService {
}
12、微服務消費者降級FallbackHelloService類內容
package com.yyh.service;
import com.yyh.entity.Customer;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
@Component
public class FallbackHelloService implements HelloServiceConsumer{
@Override
public String say() {
return "service not----- available";
}
@Override
public Customer s(@PathVariable("id") String id) {
Customer customer = new Customer();
customer.setFirstName("222");
customer.setId(987);
return customer;
}
}
13、微服務消費者Controller
package com.yyh.controller;
import com.yyh.entity.Customer;
import com.yyh.service.HelloServiceConsumer;
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;
/**
* Created by Administrator on 2018/12/21 0021.
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
HelloServiceConsumer helloServiceConsumer;
@RequestMapping("/say/{id}")
public Customer get(@PathVariable("id") String id){
return helloServiceConsumer.s(id);
}
}
14、微服務消費者啟動類
package com.yyh;
import com.yyh.entity.Customer;
import com.yyh.service.SayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//必不可少
@RestController
@EnableCircuitBreaker
@EnableHystrixDashboard
public class ConsulClient4App {
@Autowired
private SayService sayService;
@RequestMapping("say")
public String say() throws InterruptedException {
return sayService.say();
}
@RequestMapping("s/{id}")
public Customer s(@PathVariable("id")String id) throws InterruptedException {
return sayService.s(id);
}
public static void main(String[] args){
SpringApplication.run(ConsulClient4App.class, args);
}
}
15、啟動服務提供者和消費者,訪問
http://127.0.0.1:8014/hello/say/8564
結果如下
程式碼地址