准备:所需工程:micro-weather-eureka-client(被调用工程),micro-weather-eureka-client-zuul(feign反向代理),micro-weather-eureka-server(服务)。
micro-weather-eureka-client-zuul:首先配置成client服务然后改成zuul反向代理:
依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zuul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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-web</artifactId>
</dependency>
</dependencies>
启动类配置:
package com.example.microweathereurekaclient;
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;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class MicroWeatherEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(MicroWeatherEurekaClientApplication.class, args);
}
}
配置文件:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: micro-weather-eureka-client-zuul
zuul:
routes:
hi: #对应path的hi,相互关联,一起更改
path: /hi/** #表示访问路径,/hi/hello表示访问client的hello方法
serviceId: micro-weather-eureka-client //调用的client
server:
port: 8086
client:仅有HelloController下的hello方法,返回“hello”。执行效果:
zuul反向代理访问:
成功调用