转载地址: https://blog.csdn.net/forezp/article/details/70148833
1、搭建模块层次
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父级 pom 工程 -->
<groupId>com.vim</groupId>
<artifactId>cloud-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-server</module>
</modules>
<!-- springboot 版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<!-- springcloud 版本 -->
<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>
<!-- 使用阿里云仓库 -->
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories>
<!-- 设置编译环境 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、搭建 Eureka Server
- 配置启动类,添加 @EnableEurekaServer 注解
spring.application.name=eureka-server
server.port=8888
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
3、搭建 Eureka Client
- 配置启动类,添加 @EnableEurekaClient 注解
spring.application.name=service-one
server.port=9001
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/
<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-web</artifactId>
</dependency>
</dependencies>
4、开启 feign 和 hystrix
- 配置启动类,添加 @EnableFeignClients 注解
feign.hystrix.enabled=true
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- 配置业务层熔断类
package com.vim.modules.web.service;
import org.springframework.stereotype.Component;
@Component
public class TestServiceHystrix implements TestService{
@Override
public String test() {
return “”“”;
}
}
- 配置业务层 service
package com.vim.modules.web.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "service-one", fallback = TestServiceHystrix.class)
public interface TestService {
@RequestMapping(value = "/test",method = RequestMethod.GET)
String test();
}
- 配置控制层 controller
package com.vim.modules.web.controller;
import com.vim.modules.web.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "/test")
public String test(){
return testService.test();
}
}
5、单个工程开启多个实例
- Edit Configurations --> 去掉 Single instance only 勾选
- 修改 application.properties 端口,再启动即可
6、搭建 Zuul 网关
- 配置启动类,添加 @EnableZuulProxy 注解
zuul.routes.api-one.path=/api-one/**
zuul.routes.api-one.serviceId=service-one
zuul.routes.api-two.path=/api-two/**
zuul.routes.api-two.serviceId=service-two
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
- 添加过滤器
package com.vim.common.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class UserFilter extends ZuulFilter {
@Override
public String filterType() {
//pre:路由之前
//routing:路由之时
//post:路由之后
//error:错误之后
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
return null;
}
}
7、搭建 config 服务器
- 配置启动类,添加 @EnableConfigServer注解
spring.cloud.config.server.git.uri=https://github.com/bbbscxy/boot-config
spring.cloud.config.server.git.searchPaths=config
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 配置文件
#配置文件位置
https://github.com/bbbscxy/boot-config/blob/master/config/client-dev.properties
#直接访问方式
http://localhost:9030/client-dev.properties
- 文件名定义规则
项目名--环境.properties
8、搭建 config 客户端
- 配置依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 新建 bootstrap.properties
spring.application.name=service-one
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/
#此处可自定义,这样 spring.application.name 就可以取其他的名称
spring.cloud.config.name=client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=service-config
- 控制器获取属性
package com.vim.modules.web.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${param}")
private String param;
@RequestMapping(value = "/test")
public String test(){
return param;
}
}
9、注册中心高可用
- 新建三个文件,分别为 application-server2.properties、application-server3.properties、application-server4.properties
spring.application.name=eureka-server
server.port=8888
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:8889/eureka/,http://${eureka.instance.hostname}:8890/eureka/
spring.application.name=eureka-server
server.port=8889
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:8888/eureka/,http://${eureka.instance.hostname}:8890/eureka/
spring.application.name=eureka-server
server.port=8890
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:8888/eureka/,http://${eureka.instance.hostname}:8889/eureka/
- application.properties 文件,分别启动三个工程
spring.profiles.active=server2
- 客户端修改 application.properties
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/,http://localhost:8889/eureka/,http://localhost:8890/eureka/
10、搭建消息总线
- 配置依赖
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 控制器修改
package com.vim.modules.web.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class TestController {
@Value("${param}")
private String param;
@RequestMapping(value = "/test")
public String test(){
return param;
}
}
- 刷新配置,发送POST 请求
http://localhost:9000/actuator/bus-refresh