Browse Source

update

master
许孟阳 3 years ago
parent
commit
6410486b3b
  1. 68
      eureka-feign/pom.xml
  2. 2
      eureka-feign/src/main/java/vip/xumy/learn/eureka/client/SchedualServiceHi.java
  3. 30
      eureka-hi/pom.xml
  4. 2
      eureka-hi/src/main/resources/application.properties
  5. 45
      eureka-ribbon/pom.xml
  6. 2
      eureka-ribbon/src/main/java/vip/xumy/learn/eureka/client/HelloService.java
  7. 2
      eureka-server/application.properties
  8. 52
      eureka-server/pom.xml
  9. 8
      eureka-zuul/.gitignore
  10. 72
      eureka-zuul/pom.xml
  11. 65
      eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/MyFilter.java
  12. 25
      eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/ServiceZuulApplication.java
  13. 10
      eureka-zuul/src/main/resources/application.properties
  14. 3
      parent/pom.xml

68
eureka-feign/pom.xml

@ -3,31 +3,71 @@ @@ -3,31 +3,71 @@
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>
<parent>
<groupId>vip.xumy.learn</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0.0.RELEASE</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<artifactId>eureka-client-feign</artifactId>
<version>1.0.0</version>
<name>eureka-client-feign</name>
<packaging>jar</packaging>
<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>Finchley.RELEASE</spring-cloud.version>
</properties>
<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-test</artifactId>
<scope>test</scope>
</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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</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>
<finalName>feign</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>

2
eureka-feign/src/main/java/vip/xumy/learn/eureka/client/SchedualServiceHi.java

@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.RequestMapping; @@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "service-hi", fallback = SchedualServiceHiHystric.class)
@FeignClient(value = "eureka-hi", fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);

30
eureka-hi/pom.xml

@ -3,13 +3,21 @@ @@ -3,13 +3,21 @@
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>
<parent>
<groupId>vip.xumy.learn</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0.0.RELEASE</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<artifactId>eureka-client-hi</artifactId>
<version>1.0.0</version>
<name>eureka-client-hi</name>
<packaging>jar</packaging>
<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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
@ -25,12 +33,28 @@ @@ -25,12 +33,28 @@
</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>
<finalName>hi</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<configuration>
<executable>true</executable>
</configuration>
</plugins>
</build>

2
eureka-hi/src/main/resources/application.properties

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
server.port=8762
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.client.serviceUrl.defaultZone=http://eureka.xumy.vip/eureka/
spring.application.name=eureka-hi

45
eureka-ribbon/pom.xml

@ -3,16 +3,30 @@ @@ -3,16 +3,30 @@
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>
<parent>
<groupId>vip.xumy.learn</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0.0.RELEASE</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<artifactId>eureka-client-ribbon</artifactId>
<version>1.0.0</version>
<name>eureka-client-ribbon</name>
<packaging>jar</packaging>
<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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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-netflix-eureka-client</artifactId>
@ -35,5 +49,30 @@ @@ -35,5 +49,30 @@
</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>
<finalName>ribbon</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>

2
eureka-ribbon/src/main/java/vip/xumy/learn/eureka/client/HelloService.java

@ -14,7 +14,7 @@ public class HelloService { @@ -14,7 +14,7 @@ public class HelloService {
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
return restTemplate.getForObject("http://EUREKA-HI/hi?name="+name,String.class);
}
public String hiError(String name) {

2
eureka-server/src/main/resources/application.properties → eureka-server/application.properties

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
server.port=8761
server.port=8080
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false

52
eureka-server/pom.xml

@ -3,22 +3,60 @@ @@ -3,22 +3,60 @@
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>
<parent>
<groupId>vip.xumy.learn</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0.0.RELEASE</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<artifactId>eureka-server-demo</artifactId>
<version>1.0.0</version>
<name>eureka-server-demo</name>
<packaging>jar</packaging>
<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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<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-netflix-eureka-server</artifactId>
</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>
<finalName>eureka</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>

8
eureka-zuul/.gitignore vendored

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
/.classpath
/.project
/.settings
/bin/
/logs
/target
*.log
/src/main/resources/static

72
eureka-zuul/pom.xml

@ -0,0 +1,72 @@ @@ -0,0 +1,72 @@
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<artifactId>eureka-client-zuul</artifactId>
<version>1.0.0</version>
<name>eureka-client-zuul</name>
<packaging>jar</packaging>
<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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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-netflix-eureka-client</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-netflix-zuul</artifactId>
</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>
<finalName>zuul</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>

65
eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/MyFilter.java

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
package vip.xumy.learn.eureka.client;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
@Component
public class MyFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(MyFilter.class);
/**
* filterType返回一个字符串代表过滤器的类型在zuul中定义了四种不同生命周期的过滤器类型具体如下 pre路由之前
* routing路由之时 post 路由之后 error发送错误调用
*/
@Override
public String filterType() {
return "pre";
}
/**
* filterOrder过滤的顺序
*/
@Override
public int filterOrder() {
return 0;
}
/**
* shouldFilter这里可以写逻辑判断是否要过滤true,永远过滤
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* run过滤器的具体逻辑可用很复杂包括查sqlnosql去判断该请求到底有没有权限访问
*/
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
Object accessToken = request.getParameter("token");
if (accessToken == null) {
log.warn("token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
} catch (Exception e) {
}
return null;
}
log.info("ok");
return null;
}
}

25
eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/ServiceZuulApplication.java

@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
package vip.xumy.learn.eureka.client;
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
@EnableEurekaClient
@EnableDiscoveryClient
@EnableZuulProxy
public class ServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceZuulApplication.class, args );
}
}

10
eureka-zuul/src/main/resources/application.properties

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
server.port=8080
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=eureka-zuul
zuul.routes.ribbon.path=/ribbon/**
zuul.routes.ribbon.serviceId=eureka-ribbon
zuul.routes.feign.path=/feign/**
zuul.routes.feign.serviceId=eureka-feign

3
parent/pom.xml

@ -53,6 +53,9 @@ @@ -53,6 +53,9 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>

Loading…
Cancel
Save