13. 微服务综合案例1 eureka和config server (2刷)

技术一览

  1. 注册中心
  2. 配置中心
  3. 路由网关
  4. 授权中心 Uaa服务
    1. User Account and Authentication 用户帐号及认证
    2. Spring OAuth2 和 Spring boot security
  5. Turbine 聚合监控服务
  6. 链路追踪服务 Zikin
  7. 聚合监控服务 admin-service
  8. 日志服务
eureka
spirng cloud config
Spring cloud OAuth2
Feign
Ribbon
Hystrix
Hystrix Dashboard
Turbine
Spring Cloud Sleuth 集成Zipkin
Spring boot Admin 聚合监控微服务的状况。上面的 turbine 也可以在这里显示。
	继承turbine,turbine集合 其他服务的 Hystrix Dashboard
zuul
Spring Data Jpa
Swagger
Restful api
RabbitMq 发送日志消息

主 Maven 工程

Jolokia是一个利用JSON通过Http实现JMX远程管理的开源项目。具有快速、简单等特点。

  • 运行在服务器上的后端程序,可不可以视化的监控?
  • 对外暴露MBeans的HTTP访问URL。

JMX(Java Management Extensions,即Java管理扩展)

OAuth 2.0 是一种授权机制,主要用来颁发令牌(token)。

  • 首先声明oauth2是一种协议规范,spring-security-oauth2是对他的一种实现。

oAuth 协议为用户资源的授权提供了一个安全的、开放而又简易的标准。

pom

<?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>

    <groupId>com.forezp</groupId>
    <artifactId>SpringCloudInAction</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>blog-service</module> 日志
        <module>config-server</module> 配置中心
        <module>eureka-server</module> 注册中心
        <module>gateway-service</module> 网关
        <module>user-service</module> 用户服务
        <module>monitor-service</module> 监控
        <module>uaa-service</module> 认证鉴权
        <module>log-service</module> 日志
        <module>common</module> 通用
        <module>admin-service</module> 管理
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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>Greenwich.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.jolokia</groupId>
            <artifactId>jolokia-core</artifactId> //监控的核心。spring boot admin用。但是新版没有用
        </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>
            <dependency> //jwt
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-jwt</artifactId>
                <version>1.0.9.RELEASE</version>
            </dependency>


            <!-- https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2 -->
            <dependency> //oauth2
                <groupId>org.springframework.security.oauth</groupId>
                <artifactId>spring-security-oauth2</artifactId>
                <version>2.3.4.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

eureka-server

pom 开启server 和 yaml

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
@SpringBootApplication
@EnableEurekaServer
server:
  port: 8761
  
eureka:
  client:
    register-with-eureka: false #不注册到其他eureka
    fetch-registry: false #不拉取,其他的注册信息
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/
      
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

config-server

pom

	<dependencies>
		<dependency> config-server
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<!-- actuator-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<dependency> bus-amqp
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>



		<dependency> retry
			<groupId>org.springframework.retry</groupId>
			<artifactId>spring-retry</artifactId>
		</dependency>

		<dependency> aop
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
	</dependencies>

enable 和 yaml

@EnableConfigServer

application

# remote git
#spring:
#  cloud:
#    config:
#      server:
#        git:
#          uri: https://git.coding.net/xiaoantimes/xiaoantimes-taichi
#          searchPaths: backend/repo
#          username: 124746406@qq.com
#          password:
#      label: master

# ---native
spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
  profiles:
     active: native
  application:
    name: config-server

# port
server:
  port: 8769

management:
  security:
    enabled: false
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

下面的配置,都是 shared 目录下的文件

admin-service-pro.yml

spring:
  application:
    name: admin-server
  security:
    user:
      name: "admin"
      password: "admin"
server:
  port: 9998
eureka:
  client:
    registryFetchIntervalSeconds: 5 #表示eureka client间隔多久去拉取 服务注册信息,默认为30秒
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10 #表示eureka client发送心跳给server端的频率
    health-check-url-path: /actuator/health #健康检查的路径
    metadata-map:
      user.name: ${spring.security.user.name} #节点的用户名,就是 security的用户名
      user.password: ${spring.security.user.password}

management:
  endpoints:
    web:
      exposure:
        include: "*" #管理 断点 web 暴露 包含
  endpoint:
    health:
      show-details: ALWAYS # 端点 健康 展示细节 总是


#server:
#  port: 9998
#security:
#  user:
#    name: admin
#    password: 123456
#management:
#  security:
#    enabled: false
#
#spring:
#  boot:
#    admin:
#      routes:
#        endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream,activiti
#      turbine:
#        clusters: default
#        location: monitor-service
#
#eureka:
#  instance:
#    metadata-map:
#      user.name: admin
#      user.password: 123456

logging:
  file: "logs/admin-service-pro.log" #日志目录
exposure 
英 /ɪkˈspəʊʒə(r)/  美 /ɪkˈspoʊʒər/  全球(英国)  
简明 牛津 新牛津  韦氏  柯林斯 例句  百科
n. 暴露;曝光;揭露;陈列
复数 exposures

shared下的application.yml


#logging:
#  level:
#    org.springframework.security: INFO

#hystrix:
#  command:
#    default:
#      execution:
#        isolation:
#          thread:
#            timeoutInMilliseconds: 10000

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/


#endpoints:
#  health:
#    sensitive: false
#    enabled: true
#  actuator:
#    enabled: true
#    sensitive: false
#  beans:
#    sensitive: false
#    enabled: true


# 端点配置,*
# 端点健康,展示细节 总是


feign:
  hystrix:
    enabled: true

blog-service-pro.yml

server:
  port: 8763


spring:
  zipkin:
    base-url: http://localhost:9411/ #zipkin地址
  sleuth:
    web:
      client:
        enabled: true #开启 sleuth
    sampler:
      probability: 1.0 # 将采样比例设置为 1.0,也就是全部都需要。默认是 0.1
  datasource: #mysql配置
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/sys-blog?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8
    username: root
    password: 123456
  jpa: #jpa配置
    hibernate:
      ddl-auto: update
    show-sql: true

  rabbitmq:
    host: localhost #mq配置
    port: 5672
    username: guest
    password: guest
    publisher-confirms: true
    virtual-host: /

gateway-service-pro.yml

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 20000 #hystrix 超时时间,很重要

ribbon:
  ReadTimeout: 20000 #ribbon超时时间
  ConnectTimeout: 20000 #ribbon连接超时时间

zuul:
  host:
    connect-timeout-millis: 20000 #zuul 连接
    socket-timeout-millis: 20000 #zuul socket超时时间

  routes:
    user-service:
      path: /userapi/** #路由配置,访问userapi访问 user-service服务
      serviceId: user-service
      sensitiveHeaders:

    blog-service:
      path: /blogapi/**
      serviceId: blog-service
      sensitiveHeaders: #默认敏感头部不通过,会去掉,设置为空。案例用 Bearer {token}

server:
  port: 5000 #网关的端口


logger-service-pro.yml

spring:
  datasource mysql配置
  jpa配置

  rabbitmq配置

server:
  port: 9997


monitor-service-pro.yml


server:
  port: 8766

management:
  endpoints:
    web:
      exposure:
        include: "*" #断点暴露 *
      cors:
        allowed-origins: "*" #允许源 和 方法
        allowed-methods: "*"

turbine:
  app-config: blog-service,user-service #turbine聚合的
  aggregator:
    clusterConfig: default #默认参数
  clusterNameExpression: new String("default") #默认
  combine-host: true
  instanceUrlSuffix:
    default: actuator/hystrix.stream #默认的监控是 最新的 actuator的hystrix的位置

uaa-service-pro.yml

spring:
  datasource mysql
  jpa
server:
  port: 9999

user-service-pro.yml

server:
  port: 8762


spring:
  zipkin:
    base-url: http://localhost:9411/ #zipkin地址
  sleuth:
    web:
      client:
        enabled: true # sleuth打开
    sampler:
      probability: 1.0 # 将采样比例设置为 1.0,也就是全部都需要。默认是 0.1
#  rabbitmq:
#    host: localhost
#    port: 5672
  datasource mysql
  jpa

  rabbitmq

foo: foo version 1

zipkin-service-pro.yml

server:
  port: 9411
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值