From 6410486b3bf1ad4628152cb2d8ba9a4eba7a5b0e Mon Sep 17 00:00:00 2001 From: mengyxu Date: Sun, 27 Feb 2022 10:42:13 +0800 Subject: [PATCH] update --- eureka-feign/pom.xml | 68 ++++++++++++++---- .../eureka/client/SchedualServiceHi.java | 2 +- eureka-hi/pom.xml | 30 +++++++- .../src/main/resources/application.properties | 2 +- eureka-ribbon/pom.xml | 45 +++++++++++- .../learn/eureka/client/HelloService.java | 2 +- .../resources => }/application.properties | 2 +- eureka-server/pom.xml | 52 ++++++++++++-- eureka-zuul/.gitignore | 8 +++ eureka-zuul/pom.xml | 72 +++++++++++++++++++ .../xumy/learn/eureka/client/MyFilter.java | 65 +++++++++++++++++ .../eureka/client/ServiceZuulApplication.java | 25 +++++++ .../src/main/resources/application.properties | 10 +++ parent/pom.xml | 3 + 14 files changed, 355 insertions(+), 31 deletions(-) rename eureka-server/{src/main/resources => }/application.properties (93%) create mode 100644 eureka-zuul/.gitignore create mode 100644 eureka-zuul/pom.xml create mode 100644 eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/MyFilter.java create mode 100644 eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/ServiceZuulApplication.java create mode 100644 eureka-zuul/src/main/resources/application.properties diff --git a/eureka-feign/pom.xml b/eureka-feign/pom.xml index 588e25d..7a20e86 100644 --- a/eureka-feign/pom.xml +++ b/eureka-feign/pom.xml @@ -3,31 +3,71 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - vip.xumy.learn - spring-cloud-demo - 1.0.0.RELEASE + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE eureka-client-feign 1.0.0 eureka-client-feign + jar + + + UTF-8 + UTF-8 + 1.8 + Finchley.RELEASE + - - - org.springframework.cloud - spring-cloud-starter-netflix-eureka-client - - + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + org.springframework.boot spring-boot-starter-web - + - org.springframework.cloud - spring-cloud-starter-openfeign - + org.springframework.cloud + spring-cloud-starter-openfeign + - + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + feign + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + \ No newline at end of file diff --git a/eureka-feign/src/main/java/vip/xumy/learn/eureka/client/SchedualServiceHi.java b/eureka-feign/src/main/java/vip/xumy/learn/eureka/client/SchedualServiceHi.java index 39b9d0a..1955289 100644 --- a/eureka-feign/src/main/java/vip/xumy/learn/eureka/client/SchedualServiceHi.java +++ b/eureka-feign/src/main/java/vip/xumy/learn/eureka/client/SchedualServiceHi.java @@ -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); diff --git a/eureka-hi/pom.xml b/eureka-hi/pom.xml index d94cb3a..e818c71 100644 --- a/eureka-hi/pom.xml +++ b/eureka-hi/pom.xml @@ -3,13 +3,21 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - vip.xumy.learn - spring-cloud-demo - 1.0.0.RELEASE + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE eureka-client-hi 1.0.0 eureka-client-hi + jar + + + UTF-8 + UTF-8 + 1.8 + Finchley.RELEASE + @@ -25,12 +33,28 @@ + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + hi org.springframework.boot spring-boot-maven-plugin + + true + diff --git a/eureka-hi/src/main/resources/application.properties b/eureka-hi/src/main/resources/application.properties index e9d302d..ddca29e 100644 --- a/eureka-hi/src/main/resources/application.properties +++ b/eureka-hi/src/main/resources/application.properties @@ -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 diff --git a/eureka-ribbon/pom.xml b/eureka-ribbon/pom.xml index 49f546c..38826bb 100644 --- a/eureka-ribbon/pom.xml +++ b/eureka-ribbon/pom.xml @@ -3,16 +3,30 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - vip.xumy.learn - spring-cloud-demo - 1.0.0.RELEASE + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE eureka-client-ribbon 1.0.0 eureka-client-ribbon + jar + + + UTF-8 + UTF-8 + 1.8 + Finchley.RELEASE + + + org.springframework.boot + spring-boot-starter-test + test + + org.springframework.cloud spring-cloud-starter-netflix-eureka-client @@ -35,5 +49,30 @@ + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + ribbon + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + \ No newline at end of file diff --git a/eureka-ribbon/src/main/java/vip/xumy/learn/eureka/client/HelloService.java b/eureka-ribbon/src/main/java/vip/xumy/learn/eureka/client/HelloService.java index 5f2d4df..4b0b136 100644 --- a/eureka-ribbon/src/main/java/vip/xumy/learn/eureka/client/HelloService.java +++ b/eureka-ribbon/src/main/java/vip/xumy/learn/eureka/client/HelloService.java @@ -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) { diff --git a/eureka-server/src/main/resources/application.properties b/eureka-server/application.properties similarity index 93% rename from eureka-server/src/main/resources/application.properties rename to eureka-server/application.properties index c8bc83c..a9c2f9e 100644 --- a/eureka-server/src/main/resources/application.properties +++ b/eureka-server/application.properties @@ -1,4 +1,4 @@ -server.port=8761 +server.port=8080 eureka.instance.hostname=localhost eureka.client.registerWithEureka=false diff --git a/eureka-server/pom.xml b/eureka-server/pom.xml index bfeb0ee..12003f7 100644 --- a/eureka-server/pom.xml +++ b/eureka-server/pom.xml @@ -3,22 +3,60 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - vip.xumy.learn - spring-cloud-demo - 1.0.0.RELEASE + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE eureka-server-demo 1.0.0 eureka-server-demo jar + + UTF-8 + UTF-8 + 1.8 + Finchley.RELEASE + + - - - org.springframework.cloud - spring-cloud-starter-netflix-eureka-server + + + org.springframework.boot + spring-boot-starter-test + test + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + eureka + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + \ No newline at end of file diff --git a/eureka-zuul/.gitignore b/eureka-zuul/.gitignore new file mode 100644 index 0000000..338ed4c --- /dev/null +++ b/eureka-zuul/.gitignore @@ -0,0 +1,8 @@ +/.classpath +/.project +/.settings +/bin/ +/logs +/target +*.log +/src/main/resources/static \ No newline at end of file diff --git a/eureka-zuul/pom.xml b/eureka-zuul/pom.xml new file mode 100644 index 0000000..3b50128 --- /dev/null +++ b/eureka-zuul/pom.xml @@ -0,0 +1,72 @@ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE + + eureka-client-zuul + 1.0.0 + eureka-client-zuul + jar + + + UTF-8 + UTF-8 + 1.8 + Finchley.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.cloud + spring-cloud-starter-netflix-zuul + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + zuul + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + \ No newline at end of file diff --git a/eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/MyFilter.java b/eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/MyFilter.java new file mode 100644 index 0000000..a4039b8 --- /dev/null +++ b/eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/MyFilter.java @@ -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:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。 + */ + @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; + } +} diff --git a/eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/ServiceZuulApplication.java b/eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/ServiceZuulApplication.java new file mode 100644 index 0000000..55e43cb --- /dev/null +++ b/eureka-zuul/src/main/java/vip/xumy/learn/eureka/client/ServiceZuulApplication.java @@ -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 ); + } + +} + + + + + + diff --git a/eureka-zuul/src/main/resources/application.properties b/eureka-zuul/src/main/resources/application.properties new file mode 100644 index 0000000..e71ba81 --- /dev/null +++ b/eureka-zuul/src/main/resources/application.properties @@ -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 diff --git a/parent/pom.xml b/parent/pom.xml index 5b91992..a902a94 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -53,6 +53,9 @@ org.springframework.boot spring-boot-maven-plugin + + true +