接上篇文章:https://blog.csdn.net/qq_33333654/article/details/103182042
demo代码地址:https://download.csdn.net/download/qq_33333654/12014918
首先准备好自己的服务中心。
创建对应的server项目并注册到注册中心,供client调用。我的server项目配置如下:
server: port: 9021 eureka: client: service-url: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/ spring: application: name: spring-cloud-config-server profiles: active: native rabbitmq: host: localhost port: 5672 username: admin password: 123456 management: security: enabled: false
注:这里我添加了配置中心与rabbitmq,如果不需要的话可以注释掉。
server项目的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-cloud-config-server</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
resources下创建native-mysql.properties文件,内容为:
native.hello=hello_i_im_mysql_update1
接下来就是需要一个client客户端提供我们调用server同时能够让zuul管理。
创建client项目,我的项目配置bootstrap.yml 文件:
spring: cloud: config: discovery: enabled: true serviceId: spring-cloud-config-server # uri: http://localhost:9021/ name: native profile: mysql lable: master eureka: client: service-url: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/
application.yml文件:
server: port: 9031 spring: application: name: spring-cloud-config-client rabbitmq: host: localhost port: 5672 username: admin password: 123456 virtual-host: / cloud: bus: trace: enabled: true management: security: enabled: false #springboot 1.5.X以上默认开通了安全认证
注:我这里使用了配置中心和rabbitmq。如果不需要可以自行创建,直接使用 application.yml一个配置文件,将bootstrap.yml配置文件中的配置迁移进来即可。
client项目的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>spirng-cloud-config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spirng-cloud-config-client</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</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-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建一个controller类:
@RestController @RefreshScope // 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。 public class HelloController { @Value("${native.hello}") private String hello; @RequestMapping("/hello") public String from() { return this.hello; } }
好所需的项目已准备。接下来就是zuul使用。
1、简单使用
先用最简单的方式,首先创建Zuul项目
pom文件添加核心依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>gateway-service-zuul-simple</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway-service-zuul-simple</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yml文件添加配置:
server: port: 8888 eureka: client: service-url: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/ spring: application: name: gateway-service-zuul zuul: routes: baidu: path: /it/** url: https://blog.csdn.net/qq_33333654/category_9436879.html
启动类添加核心注解:
@EnableZuulProxy
接下来测试。
请先启动注册中心,然后启动zuul项目。浏览器访问:http://localhost:8888/it/test
看看是不是到了该界面:
史上最简单的zuul使用完成。
2、基础使用
是否可以调用其他项目呢?当然可以。
首先启动注册中心,启动rabbitmq(如果你有配置的话),然后分别启动刚才创建的server项目与client项目。
然后修改zuul项目yml文件配置:
server: port: 8888 eureka: client: service-url: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/ spring: application: name: gateway-service-zuul zuul: routes: hello: path: /hello/** url: http://localhost:9031/
启动zuul项目,测试。
访问浏览器http://localhost:8888/hello/hello
返回的是你client项目中的hello接口的内容:
hello_i_im_mysql_update1
这是基础使用,接下来是进行服务化操作。
3、服务化
首先复制client项目,修改yml配置文件的端口号为:9032
分别在两个client项目中创建一个接口:
@RequestMapping("/helloByMsg") public String helloByMsg(@RequestParam String name) { return "hello "+name+",this is two messge"; }
return的内容最好是不一样的。一个是one,一个是two
接下来修改zuul配置文件:
server: port: 8888 eureka: client: service-url: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/ spring: application: name: gateway-service-zuul zuul: routes: api-a: path: /client/** serviceId: spring-cloud-config-client
启动server、client两个项目、zuul项目。
浏览器访问:http://localhost:8888/client/helloByMsg?name=冒菜
多次访问,观察内容变化分别为:
hello 冒菜,this is one messge
hello 冒菜,this is two messge
hello 冒菜,this is one messge
hello 冒菜,this is two messge
...
说明通过zuul成功调用了client服务并且做了均衡负载。
网关的默认路由规则
但是如果后端服务多达十几个的时候,每一个都这样配置也挺麻烦的,spring cloud zuul已经帮我们做了默认配置。默认情况下,Zuul会代理所有注册到Eureka Server的微服务,并且Zuul的路由规则如下:http://ZUUL_HOST:ZUUL_PORT/微服务在Eureka上的serviceId/**
会被转发到serviceId对应的微服务。
修改zuul配置,注释掉所有zuul配置内容:
server: port: 8888 eureka: client: service-url: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/ spring: application: name: gateway-service-zuul #zuul: # routes: # api-a: # path: /client/** # serviceId: spring-cloud-config-client # hello: # path: /hello/** # url: http://localhost:9031/ # baidu: # path: /it/** # url: https://blog.csdn.net/qq_33333654/category_9436879.html
重启zuul项目,浏览器访问:
http://localhost:8888/spring-cloud-config-client/helloByMsg?name=冒菜
观察内容跟删次一样
OK完成