spring-cloud-starter-netflix-zuul(原spring-clo。。。
1.基于为服务注册中⼼的zuul。
⾸先我们需要搭建⼀个eureka服务,关于这⼀步可以参考我的另⼀篇⽂章,在本篇⽂章也会⽤到⾥⾯的⼯程
下⾯我们新建⼯程
1<groupId>com.wl.springcloud</groupId>
2<artifactId>zuul</artifactId>
3<version>1.0-SNAPSHOT</version>
1<?xml version="1.0" encoding="UTF-8"?>
2
3<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
4 xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.wl.springcloud</groupId>
8 <artifactId>zuul</artifactId>
9 <version>1.0-SNAPSHOT</version>
10
11 <name>zuul</name>
12 <!-- FIXME change it to the project's website -->
13 <url>ample</url>
14
15 <properties>
16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17 <mavenpiler.source>1.8</mavenpiler.source>
18 <mavenpiler.target>1.8</mavenpiler.target>
19 <spring-cloud-config-version>2.0.3.RELEASE</spring-cloud-config-version>
20 <spring-cloud-eureka-server>2.0.3.RELEASE</spring-cloud-eureka-server>
21 <spring-boot-version>2.0.8.RELEASE</spring-boot-version>
22 <spring-cloud-zuul-version>2.0.3.RELEASE</spring-cloud-zuul-version>
23
24 <spring-cloud-eureka-client-version>2.0.3.RELEASE</spring-cloud-eureka-client-version>
25 <MainClass>com.wl.springcloud.zuul.ZuulApplication</MainClass>
26 </properties>
27
28 <dependencies>
29 <dependency>
30 <groupId>org.springframework.boot</groupId>
31 <artifactId>spring-boot-starter-web</artifactId>
32 <version>${spring-boot-version}</version>
33 </dependency>
34 <dependency>
35 <groupId>org.springframework.boot</groupId>
36 <artifactId>spring-boot-autoconfigure</artifactId>
37 <version>${spring-boot-version}</version>
38 </dependency>
39
40 <!-- mvnrepository/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
41 <dependency>
42 <groupId>org.springframework.cloud</groupId>
43 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
44 <version>${spring-cloud-eureka-client-version}</version>
45 </dependency>
46 <dependency>
47 <groupId>org.springframework.boot</groupId>
48 <artifactId>spring-boot-starter-actuator</artifactId>
49 <version>${spring-boot-version}</version>
49 <version>${spring-boot-version}</version>
50 </dependency>
51 <dependency>
52 <groupId>org.springframework.cloud</groupId>
53 <artifactId>spring-cloud-starter-config</artifactId>
54 <version>${spring-cloud-config-version}</version>
55 </dependency>
56
57 <!-- mvnrepository/artifact/org.springframework.cloud/spring-cloud-starter-netflix-zuul -->
58 <dependency>
59 <groupId>org.springframework.cloud</groupId>
60 <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
61 <version>${spring-cloud-zuul-version}</version>
62 </dependency>
63
64 <dependency>
65 <groupId>org.springframework.boot</groupId>
66 <artifactId>spring-boot-starter-test</artifactId>
67 <version>${spring-boot-version}</version>
68 <scope>test</scope>
69 </dependency>
70 </dependencies>
71
72 <!-- Package as an executable jar -->
73 <build>
74 <plugins>
75 <plugin>
76 <groupId>org.springframework.boot</groupId>
77 <artifactId>spring-boot-maven-plugin</artifactId>
78 <version>${spring-boot-version}</version>
79 <configuration>
80 <mainClass>${MainClass}</mainClass>
81 <layout>JAR</layout>
82 </configuration>
83 <!-- repackage ⽣成两个 iginal -->
84 <executions>
85 <execution>
86 <goals>
87 <goal>repackage</goal>
88 </goals>
89 </execution>
90 </executions>
91 </plugin>
92 <!-- 指定maven 打包java 版本 -->
93 <plugin>
94 <groupId>org.apache.maven.plugins</groupId>
95 <artifactId>maven-compiler-plugin</artifactId>
96 <version>3.1</version>
97 <configuration>
98 <source>1.8</source>
99 <target>1.8</target>
100 </configuration>
101 </plugin>
102 </plugins>
103 <!-- maven 编译打包resource 和 java ⽬录下所有⽂件 maven默认资源路径是resources -->
104 <resources>
105 <resource>
106 <directory>src/main/resources</directory>
107 <includes>
108 <include>**/*.*</include>
109 <include>*.*</include>
110 </includes>
111 </resource>
112 <resource>
113 <directory>src/main/java</directory>
114 <includes>
114 <includes>
115 <include>**/*.*</include>
116 <include>*.*</include>
117 </includes>
118 </resource>
119 </resources>
120 </build>
121</project>
application.properties
1server.port=8080
2spring.application.name=zuul
3
sume.url=localhost:8763
sume.serviceId=consume-client
9
utes.provider.url=localhost:8764
utes.provider.serviceId=provider-clientspringcloud难学吗
15
sume.path、utes.provider.path中consume和provider可以为任意的字符,只是作为⼀个标识。/consume/**表⽰代理路径为 /consume/**的路径并转发到serviceId为consume-client的服务
sume.url 可以是全路径也可以是serviceId,这⾥配置的为服务id(即spring.application.name对应的值),与
启动类
依次启动eureka、provider-client、consume-client、zuul 2.基于zookeeper作为服务注册与发现中⼼的zuul的使⽤
1
package com.wl.springcloud.zuul;2
3
import org.slf4j.Logger;4
import org.slf4j.LoggerFactory;5
import org.springframework.boot.SpringApplication;6
import org.springframework.boot.WebApplicationType;7
import org.springframework.boot.autoconfigure.SpringBootApplication;8
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;9
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;10
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;11
import org.springframework.jpa.HibernateJpaAutoConfiguration;12
import org.springframework.cloudflix.zuul.EnableZuulProxy;13
14
/**15
* Created by Administrator on 2019/3/28.16
*/17
@SpringBootApplication(exclude = {18
DataSourceAutoConfiguration.class,19
DataSourceTransactionManagerAutoConfiguration.class,20
HibernateJpaAutoConfiguration.class, //不使⽤数据库21
GsonAutoConfiguration.class //spring-boot2.0.0以上版本需要引⼊⾼版本的gson 依赖,如果不引⽤gson 依赖需要加此属性22
},scanBasePackages = "com.wl")23
@EnableZuulProxy 24
public class ZuulApplication {25
private static final Logger logger = Logger(ZuulApplication.class);26
27
public static void main(String[] args) {28
SpringApplication app = new SpringApplication(ZuulApplication.class);29
app.setWebApplicationType(WebApplicationType.SERVLET);30
app.run(args);31
logger.info("application init success");32
}33 34}
使⽤zookeeper作为服务注册中⼼参考我的这篇⽂章(下⾯会使⽤到该⽂章所建⼯程)
修改zuul⼯程的l如下(加⼊spring-cloud-starter-zookeeper-discovery依赖,移除spring-cloud-starter-netflix-eureka-client 依赖)
1<?xml version="1.0" encoding="UTF-8"?>
2
3<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
4 xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.wl.springcloud</groupId>
8 <artifactId>zuul</artifactId>
9 <version>1.0-SNAPSHOT</version>
10
11 <name>zuul</name>
12 <!-- FIXME change it to the project's website -->
13 <url>ample</url>
14
15 <properties>
16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17 <mavenpiler.source>1.8</mavenpiler.source>
18 <mavenpiler.target>1.8</mavenpiler.target>
19 <spring-cloud-config-version>2.0.3.RELEASE</spring-cloud-config-version>
20 <spring-cloud-eureka-server>2.0.3.RELEASE</spring-cloud-eureka-server>
21 <spring-boot-version>2.0.8.RELEASE</spring-boot-version>
22 <spring-cloud-zuul-version>2.0.3.RELEASE</spring-cloud-zuul-version>
23
24 <spring-cloud-eureka-client-version>2.0.3.RELEASE</spring-cloud-eureka-client-version>
25 <MainClass>com.wl.springcloud.zuul.ZuulApplication</MainClass>
26 </properties>
27
28 <dependencies>
29 <dependency>
30 <groupId>org.springframework.boot</groupId>
31 <artifactId>spring-boot-starter-web</artifactId>
32 <version>${spring-boot-version}</version>
33 </dependency>
34 <dependency>
35 <groupId>org.springframework.boot</groupId>
36 <artifactId>spring-boot-autoconfigure</artifactId>
37 <version>${spring-boot-version}</version>
38 </dependency>
39
40 <!-- mvnrepository/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
41 <!--<dependency>-->
42 <!--<groupId>org.springframework.cloud</groupId>-->
43 <!--<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
44 <!--<version>${spring-cloud-eureka-client-version}</version>-->
45 <!--</dependency>-->
46 <dependency>
47 <groupId>org.springframework.boot</groupId>
48 <artifactId>spring-boot-starter-actuator</artifactId>
49 <version>${spring-boot-version}</version>
50 </dependency>
51 <dependency>
52 <groupId>org.springframework.cloud</groupId>
53 <artifactId>spring-cloud-starter-config</artifactId>
54 <version>${spring-cloud-config-version}</version>
55 </dependency>
56
57 <dependency>
58 <groupId>org.springframework.cloud</groupId>
59 <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
60 <version>2.0.0.RELEASE</version>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论