父類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.gyjf.czl</groupId>
<artifactId>cloud-2020-10</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-registry-01</module>
<module>eureka-service-consumer</module>
<module>eureka-service-provider-01</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
一.服務注冊中心
pom檔案
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
配置檔案
server:
port: 8080
spring:
application:
name: eureka-registry-01
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
啟動類
@SpringBootApplication
@EnableEurekaServer
public class EurekaRegistry01MainApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRegistry01MainApplication.class, args);
}
}
二.消費端服務
pom檔案
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
配置檔案
server:
port: 8081
spring:
application:
name: service-consumer
feign :
hystrix:
enabled: true
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8080/eureka/
啟動類
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
}
配置類
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Feign
@Component
@FeignClient(value = "service-provider",fallback = ServiceProvider03Fallback.class)
public interface ServiceProvider03Feign {
@GetMapping("/hello")
public String hello();
}
Fallback
@Component
public class ServiceProvider03Fallback implements ServiceProvider03Feign {
@Override
public String hello() {
return "連接配接斷開";
}
}
TestController
@RestController
public class TestController {
@Resource
private ServiceProvider03Feign serviceProvider03Feign;
@GetMapping("/consumer/other")
public String consumerOther() {
return serviceProvider03Feign.hello();
}
}
三.生産端服務
pom檔案
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
配置檔案
server:
port: 8082
spring:
application:
name: service-provider
feign :
hystrix:
enabled: true
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8080/eureka/
啟動類
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceProvider01MainApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProvider01MainApplication.class, args);
}
}
TestController
@RestController
public class TestController {
@Value("${server.port}")
private String port;
@GetMapping("/hello")
public String hello() {
return "This message is from server port:" + port;
}
public void setPort(String port) {
this.port = port;
}
}