Eureka注册中心(注册中心服务端)
<properties>
<!-- 项目设置
:编码格式UTF
-8 -->
<project
.build
.sourceEncoding
>UTF
-8</project
.build
.sourceEncoding
>
<project
.reporting
.outputEncoding
>UTF
-8</project
.reporting
.outputEncoding
>
<java.version>1.8</java
.version
>
<mybatis
-spring
-boot
>1.2.0</mybatis
-spring
-boot
>
<mysql
-connector
>5.1.39</mysql
-connector
>
<druid>1.0.18</druid
>
</properties
>
<!-- springboot父依赖
-->
<parent>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-parent
</artifactId
>
<version>2.1.14.RELEASE
</version
>
<relativePath
/>
<!-- lookup parent from repository
-->
</parent
>
<dependencies>
<!-- 启动依赖
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-web
</artifactId
>
</dependency
>
<!-- 热部署
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-devtools
</artifactId
>
<scope>runtime
</scope
>
<optional>true</optional
>
</dependency
>
<!-- 测试依赖
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-test
</artifactId
>
<scope>test
</scope
>
</dependency
>
<!-- Eureka 注册中心
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-netflix
-eureka
-server
</artifactId
>
</dependency
>
</dependencies
>
<!-- SpringCloud版本依赖管理
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-dependencies
</artifactId
>
<version>Greenwich
.SR5
</version
>
<type>pom
</type
>
<scope>import</scope
>
</dependency
>
</dependencies
>
</dependencyManagement
>
注册中心配置文件 application.yml
#注册中心配置
#
server
:
port
: 8000 #项目端口号
eureka
:
instance
:
hostname
: localhost #eureka服务端的实例名称
client
:
register
-with
-eureka
: false #
false表示不向注册中心注册自己。
fetch
-registry
: false #
false表示自己端就是注册中心,我的职责就是维护服务实
启动类
@SpringBootApplication
@EnableEurekaServer
public class Config_App {
public static void main(String
[] args
) {
SpringApplication
.run(Config_App
.class, args
);
}
}
服务提供者(消费者) 两者一致
依赖
<properties>
<!-- 项目设置
:编码格式UTF
-8 -->
<project
.build
.sourceEncoding
>UTF
-8</project
.build
.sourceEncoding
>
<project
.reporting
.outputEncoding
>UTF
-8</project
.reporting
.outputEncoding
>
<java.version>1.8</java
.version
>
<mybatis
-spring
-boot
>1.2.0</mybatis
-spring
-boot
>
<mysql
-connector
>5.1.39</mysql
-connector
>
<druid>1.0.18</druid
>
</properties
>
<!-- springboot父依赖
-->
<parent>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-parent
</artifactId
>
<version>2.1.14.RELEASE
</version
>
<relativePath
/>
<!-- lookup parent from repository
-->
</parent
>
<dependencies>
<!-- 启动依赖
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-web
</artifactId
>
</dependency
>
<!-- 热部署
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-devtools
</artifactId
>
<scope>runtime
</scope
>
<optional>true</optional
>
</dependency
>
<!-- 测试依赖
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-test
</artifactId
>
<scope>test
</scope
>
</dependency
>
<!-- Eureka 注册中心
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-netflix
-eureka
-server
</artifactId
>
</dependency
>
</dependencies
>
<!-- SpringCloud版本依赖管理
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-dependencies
</artifactId
>
<version>Greenwich
.SR5
</version
>
<type>pom
</type
>
<scope>import</scope
>
</dependency
>
</dependencies
>
</dependencyManagement
>
配置文件
##服务端
eureka
:
client
:
serviceUrl
:
#eureka 注册中心地址
defaultZone
: http
://localhost
:8000/eureka
instance
:
instance
-id
: spring
-consumer8002 #ip名称
prefer
-ip
-address
: true
server
:
#项目端口号
port
: 8001
spring
:
application
:
#服务名称
,随便写
name
: spring
-consumer
启动类
@SpringBootApplication
@EnableEurekaClient
public class Client_App {
public static void main(String
[] args
) {
SpringApplication
.run(Client_App
.class, args
);
}
}
服务端配置文件
##服务端
eureka
:
client
:
serviceUrl
:
#eureka 注册中心地址
defaultZone
: http
://localhost
:8000/eureka
instance
:
instance
-id
: spring
-consumer8002 #ip名称
prefer
-ip
-address
: true
server
:
#项目端口号
port
: 8001
spring
:
application
:
#服务名称
,随便写
name
: spring
-consumer
启动类
@SpringBootApplication
@EnableEurekaClient
public class Client_App {
public static void main(String
[] args
) {
SpringApplication
.run(Client_App
.class, args
);
}
}
restTemplate调用
再消费者中添加config类 也表示使用负载均衡ribbon
@Configuration
public class SpringConfig {
@Bean
@LoadBalanced
public RestTemplate
restTemplate() {
return new RestTemplate();
}
}
方法调用
@RestController
public class ConsumerController {
@Autowired
private RestTemplate rest
;
@RequestMapping("/findAll")
public List
<String> findAll(){
return rest
.getForObject("http://spring-provider-restTem/findAll",List
.class);
}
}
启动类不变
开启Hystrix容错保护 加入依赖
<!-- hystrix断路器
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-hystrix
</artifactId
>
<version>1.3.1.RELEASE
</version
>
</dependency
>
启动类加入
@EnableHystrix
Feign调用 (包含ribbon跟Hystrix) Feign默认的就有ribbon负载均衡 依赖
<!-- feign远程调用
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-openfeign
</artifactId
>
</dependency
>
配置
#开启feign调用 跟断路器
feign
:
hystrix
:
enabled
: true
接口调用 Value表示你要调用项目的application.name名称 fallbackFactory表示断路器访问的类 ,@requestMapping()表示你要调用的方法赋值给findAll 也表示Hystrix容错保护
@FeignClient(value
="spring-feign-provider",fallbackFactory
= ConsumerFallBack
.class)
public interface ConsumerService {
@RequestMapping("/getAll")
public String
findAll();
}
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
分布式配置中心 server 添加依赖
<!-- SpringCloud config
-server 分布式配置中心 服务端 依赖
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-config
-server
</artifactId
>
</dependency
>
配置文件application.properties
#项目注册名称
spring
.application
.name
=config
-server
#项目发布版本号
server
.port
=9999
#远程 Git 项目仓库地址
spring
.cloud
.config
.server
.git
.uri
=https
://gitee
.com
/li_quan_you
/springcloudconfigce
.git
#Gitee
: https
://gitee
.com
/longtanmashuai
/springcloud
-config
-server
.git
#Github
: https
://github
.com
/CoddingMa
/-springcloud
-config
-server
.git
#远程 Git 私有项目的账号密码
spring
.cloud
.config
.server
.git
.username
=
#远程 Git 私有项目的密码
spring
.cloud
.config
.server
.git
.password
=
eureka
.client
.serviceUrl
.defaultZone
=http
://localhost
:8000/eureka
/
启动类
@EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
public class ConfigServer_App {
public static void main(String
[] args
) {
SpringApplication
.run(ConfigServer_App
.class, args
);
}
}
Client 添加依赖
<!-- SpringCloud config
-server 分布式配置中心 消费端 依赖
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-config
</artifactId
>
</dependency
>
配置文件
#项目名称
spring
.application
.name
=config
-client
#端口号
server
.port
=9001
#分支
spring
.cloud
.config
.label
=master
#多环境配置
,默认
default(MS
:Git 仓库配置文件中 profile 与此处对应
)
spring
.cloud
.config
.profile
=dev
# 注册中心配置路径
eureka
.client
.serviceUrl
.defaultZone
=http
://localhost
:8000/eureka
/
# 开启分布式配置
spring
.cloud
.config
.discovery
.enabled
=true
# 要获取的分布式配置名称
spring
.cloud
.config
.discovery
.serviceId
=config
-server
启动类
@SpringBootApplication
@EnableEurekaClient
public class Client_App {
public static void main(String
[] args
) {
SpringApplication
.run(Client_App
.class, args
);
}
}
Zuul路由网关 依赖
<!-- SpringCloud Zuul 网关路由 依赖
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-netflix
-zuul
</artifactId
>
</dependency
>
配置文件
eureka
:
client
:
serviceUrl
:
#eureka 注册中心地址
defaultZone
: http
://localhost
:8000/eureka
/
instance
:
instance
-id
: spring
-zuul5555 #ip名称
prefer
-ip
-address
: true
server
:
#项目端口号
port
: 5555
spring
:
application
:
#服务名称
,随便写
name
: spring
-zuul
zuul
:
ignored
-services
: "*" #拦截请求
*包含所有
routes
: #白名单
student
.serviceId
: crm
-consumer #服务名称
student
.path
: /student
启动类
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class Zuul_App {
public static void main(String
[] args
) {
SpringApplication
.run(Zuul_App
.class, args
);
}
}
继承zuulfilter
@Component
public class ZuulDemoFilter extends ZuulFilter{
private static Logger log
= LoggerFactory
.getLogger(ZuulDemoFilter
.class);
public boolean shouldFilter() {
return true;
}
public Object
run() throws ZuulException
{
RequestContext ctx
= RequestContext
.getCurrentContext();
HttpServletRequest request
= ctx
.getRequest();
log
.info(String
.format("%s >>> %s", request
.getMethod(),request
.getRequestURL().toString()));
Object accessToken
= request
.getParameter("token");
String refer
=request
.getHeader("refer");
if (accessToken
!= null
) {
return 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
;
}
@Override
public String
filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
}
全部依赖
<!-- springboot父依赖
-->
<parent>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-parent
</artifactId
>
<version>2.1.14.RELEASE
</version
>
<relativePath
/>
<!-- lookup parent from repository
-->
</parent
>
<!-- SpringCloud版本依赖管理
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-dependencies
</artifactId
>
<version>Greenwich
.SR5
</version
>
<type>pom
</type
>
<scope>import</scope
>
</dependency
>
</dependencies
>
</dependencyManagement
>
<dependencies>
<!-- 启动依赖
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-web
</artifactId
>
</dependency
>
<!-- 测试依赖
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-test
</artifactId
>
<scope>test
</scope
>
</dependency
>
<!-- Eureka 注册中心
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-netflix
-eureka
-server
</artifactId
>
</dependency
>
</dependencies
>
<!-- feign远程调用
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-openfeign
</artifactId
>
</dependency
>
<!-- SpringCloud Zuul 网关路由 依赖
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-netflix
-zuul
</artifactId
>
</dependency
>
<!-- SpringCloud config
-server 分布式配置中心 服务端 依赖
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-config
-server
</artifactId
>
</dependency
>
<!-- SpringCloud config
-server 分布式配置中心 消费端 依赖
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-config
</artifactId
>
</dependency
>
<!-- hystrix断路器
-->
<dependency>
<groupId>org
.springframework
.cloud
</groupId
>
<artifactId>spring
-cloud
-starter
-hystrix
</artifactId
>
<version>1.3.1.RELEASE
</version
>
</dependency
>
全部配置 注册中心配置
#注册中心配置
server
:
port
: 8000 #项目端口号
eureka
:
instance
:
hostname
: localhost #eureka服务端的实例名称
client
:
register
-with
-eureka
: false #
false表示不向注册中心注册自己。
fetch
-registry
: false #
false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service
-url
:
defaultZone
: http
://$
{eureka
.instance
.hostname
}:$
{server
.port
}/eureka
/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
服务端配置
##服务端
eureka
:
client
:
serviceUrl
:
#eureka 注册中心地址
defaultZone
: http
://localhost
:8000/eureka
instance
:
instance
-id
: spring
-feign
-provider8001 #ip名称
prefer
-ip
-address
: true
server
:
#项目端口号
port
: 8001
spring
:
application
:
#服务名称
,随便写
name
: spring
-feign
-provider
#开启feign调用 跟断路器
feign
:
hystrix
:
enabled
: true
路由配置
eureka
:
client
:
serviceUrl
:
#eureka 注册中心地址
defaultZone
: http
://localhost
:8000/eureka
/
instance
:
instance
-id
: spring
-zuul5555 #ip名称
prefer
-ip
-address
: true
server
:
#项目端口号
port
: 5555
spring
:
application
:
#服务名称
,随便写
name
: spring
-zuul
zuul
:
ignored
-services
: "*" #拦截请求
*包含所有
routes
: #白名单
student
.serviceId
: crm
-consumer #服务名称
student
.path
: /student
启动类注解
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableFeignClients
@EnableEurekaServer
@EnableHystrix
整合Swagger 依赖
<!-- swagger api
的依赖
-->
<dependency>
<groupId>io
.springfox
</groupId
>
<artifactId>springfox
-swagger2
</artifactId
>
<version>2.6.1</version
>
</dependency
>
<dependency>
<groupId>io
.springfox
</groupId
>
<artifactId>springfox
-swagger
-ui
</artifactId
>
<version>2.6.1</version
>
</dependency
>
配置
eureka
:
client
:
#eureka 注册中心地址
serviceUrl
:
defaultZone
: http
://localhost
:8000/eureka
/
#eureka 注册中心展示的微服务信息页面
,默认为 http
://localhost
:$
{server
.port
}/info
instance
:
status
-page
-url
: http
://localhost
:$
{server
.port
}/swagger
-ui
.html #swagger启动页面访问路径
server
:
#项目端口号
port
: 8010
spring
:
application
:
#服务名称
,随便写
name
: service
-swagger
注解
@EnableSwagger2
@SpringBootApplication
@EnableEurekaClient
@Api:修饰整个类,描述 Controller 的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP 响应其中
1 个描述
@ApiResponses:HTTP 响应整体描述
@ApiIgnore:使用该注解忽略这个 API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
引入自己定义的通用项目
<dependency><!-- 引入自己定义的api通用包,可以使用clock部门Entity
-->
<groupId>com
.ysd
</groupId
>
<artifactId>crm
-api
-clock
</artifactId
>
<version>$
{project
.version
}</version
>
</dependency
>