SpringCloud 学习笔记-06--EnableEurekaClient客户端-service-zuul路由替代Nginx

根据前面的系列,

ServiceZuulApplication配置路由服务就好,其他的配置都在配置文件里面配置

package com.ml0115;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ServiceZuulApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceZuulApplication.class, args);
	}

}

application.yml配置路由信息

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8769
spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-member/**
      service-id: service-member
    api-b:
      path: /api-order/**
      service-id: service-order

pom.xml配置信息

<?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>2.2.2.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.test</groupId>
	<artifactId>service-zuul</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>service-zuul</name>
	<description>目前状态不要</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
			<version>2.2.1.RELEASE</version>
		</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>

测试网址:

http://localhost:8888/testString
http://localhost:8888/
http://localhost:8765/getOrderByUserList
http://localhost:8769/actuator/info
http://127.0.0.1:8769/api-member/getUserList?token=1224
http://localhost:8764/getOrderByUserList
http://localhost:8762/getUserList

SpringCloud里面有一个定时任务的执行:

 

/**      
 *   Create on 2020年1月17日下午4:40:48    
 * @author 脚踏实地,2020涅槃重生     
 * @version 1.0 
 * @Description:     
 */
@Component
@EnableScheduling
public class ScheduledTasks {
	
	private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
	
	
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSS");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
    	log.info("ScheduledTasks---fixedRate = 5000>"+"现在时间:" + dateFormat.format(new Date()));
    }
    @Scheduled(fixedDelay = 5000)
    public void reportfixedDelayCurrentTime() {
    	log.info("reportfixedDelayCurrentTime--->fixedDelay = 5000"+"现在时间:" + dateFormat.format(new Date()));
    }
}

运行结果:

2020-01-17 17:29:20.852  INFO 26044 --- [   scheduling-1] com.ml0115.controller.ScheduledTasks     : ScheduledTasks---fixedRate = 5000>现在时间:2020-01-17 17:29:2000852
2020-01-17 17:29:20.889  INFO 26044 --- [   scheduling-1] com.ml0115.controller.ScheduledTasks     : reportfixedDelayCurrentTime--->fixedDelay = 5000现在时间:2020-01-17 17:29:2000889
2020-01-17 17:29:25.851  INFO 26044 --- [   scheduling-1] com.ml0115.controller.ScheduledTasks     : ScheduledTasks---fixedRate = 5000>现在时间:2020-01-17 17:29:2500851
2020-01-17 17:29:25.889  INFO 26044 --- [   scheduling-1] com.ml0115.controller.ScheduledTasks     : reportfixedDelayCurrentTime--->fixedDelay = 5000现在时间:2020-01-17 17:29:2500889
2020-01-17 17:29:30.850  INFO 26044 --- [   scheduling-1] com.ml0115.controller.ScheduledTasks     : ScheduledTasks---fixedRate = 5000>现在时间:2020-01-17 17:29:3000850
2020-01-17 17:29:30.890  INFO 26044 --- [   scheduling-1] com.ml0115.controller.ScheduledTasks     : reportfixedDelayCurrentTime--->fixedDelay = 5000现在时间:2020-01-17 17:29:3000890
2020-01-1

 

 

 

 

 

发布了216 篇原创文章 · 获赞 15 · 访问量 5万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 终极编程指南 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览