乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      關(guān)于springboot,你可能不知道的事兒

       貪挽懶月 2022-06-20 發(fā)布于廣東

      一、actuator監(jiān)控

      springboot自帶actuator監(jiān)控,開啟配置后,訪問相關(guān)鏈接就可以返回服務(wù)運(yùn)行相關(guān)信息,使用方法如下:

      1、pom.xml:

      <dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
       <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
      </dependency>

      2、application.yml:

      spring:
        security:
          user:
            name: admin
            password: admin
      management:
        # 端點(diǎn)信息接口使用的端口,為了和主系統(tǒng)接口使用的端口進(jìn)行分離
        server:
          port: 8090
          servlet:
            context-path: /sys
        # 端點(diǎn)健康情況,默認(rèn)值"never",設(shè)置為"always"可以顯示硬盤使用情況和線程情況
        endpoint:
          health:
            show-details: always
        # 設(shè)置端點(diǎn)暴露的哪些內(nèi)容,默認(rèn)["health","info"],設(shè)置"*"代表暴露所有可訪問的端點(diǎn)
        endpoints:
          web:
            exposure:
              include: '*'

      完成上述兩步,actuator監(jiān)控就配置好了,然后訪問localhost:8090/sys/actuator/beans,登錄的時候用戶名密碼為yml中配置的,然后就可以看到spring中管理的所有bean了。具體有哪些路徑可以訪問,可以參考如下的文檔:

      actuator文檔:https://docs./spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#production-ready

      二、springboot-admin(SBA)

      上面說到了actuator監(jiān)控,雖然方便,但是返回的是json,不太友好。springboot-admin就提供了ui界面展示這些信息。用法也很簡單,我們需要新建一個springboot-admin-server項(xiàng)目,用來做服務(wù)端,其他所有需要被監(jiān)控的項(xiàng)目去連接這個服務(wù)端就可以。

      1、springboot-admin-server:

      • pom.xml:
      <dependency>
           <groupId>de.codecentric</groupId>
           <artifactId>spring-boot-admin-starter-server</artifactId>
      </dependency>
      • application.yml:
      server:
        port: 666
      • 主啟動類:
      // 開啟adminserver
      @EnableAdminServer
      @SpringBootApplication
      public class SpringbootAdminServerApplication {

       public static void main(String[] args) {
        SpringApplication.run(SpringbootAdminServerApplication.class, args);
       }
      }

      服務(wù)端這樣就可以了。

      2、springboot-admin-client:

      admin-client就是需要監(jiān)控的項(xiàng)目,在需要監(jiān)控的項(xiàng)目里做如下的改造:

      • pom.xml:
      <dependency>
       <groupId>de.codecentric</groupId>
       <artifactId>spring-boot-admin-starter-client</artifactId>
      </dependency>
      <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-security</artifactId>
      </dependency>
      • appication.yml:
      server:
        port: 80
      spring:
        application:
          name: util
        boot:
          admin:
            client:
              url:
              - "http://localhost:666/"
              instance:
                metadata:
                  # 客戶端端點(diǎn)信息的用戶名、密碼
                  user.name: ${spring.security.user.name}
                  user.password: ${spring.security.user.password}
        security:
          user:
            name: admin
            password: admin
      management:
        # 端點(diǎn)健康情況,默認(rèn)值"never",設(shè)置為"always"可以顯示硬盤使用情況和線程情況
        endpoint:
          health:
            show-details: always
        # 設(shè)置端點(diǎn)暴露的哪些內(nèi)容,默認(rèn)["health","info"],設(shè)置"*"代表暴露所有可訪問的端點(diǎn)
        endpoints:
          web:
            exposure:
              include: '*'

      這樣就配置好了,啟動上面的springboot-admin-server,再啟動客戶端,然后訪問:http://localhost:666/,就可以看到被admin-server監(jiān)控的應(yīng)用了。

      三、springboot-https

      我們自己擼的代碼發(fā)布到tomcat后,都是用http訪問的,如果想用https訪問怎么搞?兩種辦法,一種是在nginx配置證書,然后反向代理我們的項(xiàng)目,這種方式只需申請證書,在nginx中配置,項(xiàng)目不需要做任何修改;另一種是不需要用nginx,在項(xiàng)目中配置證書。下面將講的是第二種方式。

      1、生成證書:

      生產(chǎn)環(huán)境,這個證書是在那些認(rèn)證機(jī)構(gòu)買來的,這里為了節(jié)省money,利用jdk的keytools來生成證書(瀏覽器會提示不安全)。

      • cmd進(jìn)入jdk的bin目錄,執(zhí)行如下命令:
      keytool -genkey -alias test -keyalg RSA -keystore ./server.keystore

      -alias是別名,要記住,等下項(xiàng)目的配置文件中要配置;server.keystore是證書的文件名。

      回車后會要求你輸入一些信息,如下:

      輸入密鑰庫口令:
      再次輸入新口令:
      您的名字與姓氏是什么?
        [Unknown]:  zhusl
      您的組織單位名稱是什么?
        [Unknown]:  company
      您的組織名稱是什么?
        [Unknown]:  company
      您所在的城市或區(qū)域名稱是什么?
        [Unknown]:  sz
      您所在的省/市/自治區(qū)名稱是什么?
        [Unknown]:  sz
      該單位的雙字母國家/地區(qū)代碼是什么?
        [Unknown]:  CN
      CN=zhusl, OU=company, O=company, L=sz, ST=sz, C=CN是否正確?
        [否]:  y

      輸入 <test> 的密鑰口令
              (如果和密鑰庫口令相同, 按回車):

      Warning:
      JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore ./server.keystore -destkeystore ./server.keystore -deststoretype pkcs12" 遷移到行業(yè)標(biāo)準(zhǔn)格式 PKCS12。

      這里要記住輸入的<test>密鑰口令,等下項(xiàng)目配置文件中要配置的。執(zhí)行完后,會有個warning,直接執(zhí)行它建議使用的那段命令就好了,然后會重新生成一個server.keystore證書。執(zhí)行了那段命令后,會返回如下提示信息:

      輸入源密鑰庫口令:
      已成功導(dǎo)入別名 test 的條目。
      已完成導(dǎo)入命令: 1 個條目成功導(dǎo)入, 0 個條目失敗或取消

      Warning:
      已將 "./server.keystore" 遷移到 Non JKS/JCEKS。將 JKS 密鑰庫作為 "./server.keystore.old" 進(jìn)行了備份。
      • 這就表示生成證書成功了,就在jdk的bin目錄下。找到該證書,復(fù)制到項(xiàng)目的resources目錄下。

      2、配置證書:

      • application.yml:
      server:
        port: 443
        ssl:
          key-alias: test # 剛才設(shè)置的別名
          key-store: classpath:server.keystore
          enabled: true
          key-store-type: PKCS12
          key-store-password: 123456 # 剛才設(shè)置的密碼
      • 配置tomcat:
      @Configuration
      public class TomcatConfig {

       /**
        * 配置將 80 轉(zhuǎn)到 443 端口
        * @return
        */
       @Bean
       public Connector connector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(80);
        connector.setSecure(false);
        connector.setRedirectPort(443);
        return connector;
       }
       
       /**
        * 將connector設(shè)置到tomcat中
        * @param connector
        * @return
        */
       @Bean
       public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
         @Override
                  protected void postProcessContext(Context context) {
                      SecurityConstraint securityConstraint = new SecurityConstraint();
                      securityConstraint.setUserConstraint("CONFIDENTIAL");
                      SecurityCollection collection = new SecurityCollection();
                      collection.addPattern("/*");
                      securityConstraint.addCollection(collection);
                      context.addConstraint(securityConstraint);
                  }
        };
        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
       }
      }

      接下來啟動項(xiàng)目,訪問項(xiàng)目的controller,你就會發(fā)現(xiàn),即使用http訪問,也會自動轉(zhuǎn)到https。

      https訪問

      關(guān)注我

      獲取更多內(nèi)容


        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多