最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

簡述springboot及springboot cloud環(huán)境搭建

 更新時(shí)間:2017年07月13日 10:49:03   作者:8blues  
這篇文章主要介紹了簡述springboot及springboot cloud環(huán)境搭建的方法,包括spring boot 基礎(chǔ)應(yīng)用環(huán)境搭建,需要的朋友可以參考下

springboot使用特定的方式,簡化了spring的各種xml配置文件,并通過maven或者gradle,完成所需依賴,使用springboot maven插件,可直接輸出可運(yùn)行的jar包,省去了tomcat等容器的部署,使得基于http的網(wǎng)絡(luò)應(yīng)用開發(fā)更加方便快捷。

spring中配置文件官方文檔http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/

springboot基礎(chǔ)應(yīng)用搭建

首先建立maven工程。

pom.xml文件配置如下(每一個(gè)maven工程中的,除了自身GAV外,都使用此配置)

<?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.mahuan</groupId>
 <artifactId>producer</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>producer</name>
 <description>Demo project for Spring Boot</description>
 <!-- lookup parent from repository -->
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
  <relativePath />
 </parent>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>
 <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>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
  </dependency>
    <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
     <fork>true</fork>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.SR6</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
</project>  

建立一個(gè)啟動類,即可運(yùn)行。默認(rèn)端口為8080。

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}

springboot啟動時(shí),會自動掃描所有class文件,發(fā)現(xiàn)@Service、@RestController等注解的class文件,加載到IOC容器中。

springboot cloud注冊中心

為了對多個(gè)springboot應(yīng)用進(jìn)行發(fā)現(xiàn)以及管理,可使用eureka服務(wù)。在啟動類中增加@EnableEurekaServer即可。同時(shí)添加配置文件。

eureka注冊中心,會等待應(yīng)用主動向其進(jìn)行注冊,而eureka注冊中心在發(fā)現(xiàn)了新的應(yīng)用后,會持續(xù)向應(yīng)用發(fā)送心跳,判斷其是否存活,并定時(shí)向注冊中心發(fā)送心跳包,告知其存活情況。

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class App {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}
application.properties
server.port=1111
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

eureka.client.registerWithEureka表示eureka中心不會自己注冊自己。

springboot cloud生產(chǎn)者

如果springboot應(yīng)用配置了eureka注冊中心,并在啟動類中增加了@EnableDiscoveryClient注解,應(yīng)用啟動后會注冊到指定的注冊中心中。

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class App {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}
application.properties配置
server.port=1112
spring.application.name=compute-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

其中spring.application.name是必須要有的配置,是此springboot應(yīng)用的標(biāo)識。實(shí)現(xiàn)不同功能的springboot應(yīng)用,應(yīng)有不同的name。

eureka.client.serverUrl.defaultZone是注冊中心的地址信息,同注冊中心配置的地址相同。

此外由于注冊中心的存在,我們不必再固定生產(chǎn)者的啟動端口,可通過啟動程序控制springboot啟動時(shí),使用的端口。

當(dāng)然固定的端口號,會更加方便運(yùn)維。

注意,此時(shí)程序代碼對于啟動的配置操作,是優(yōu)先于配置文件配置的。

@SpringBootApplication 
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{ 
 public static void main(String[] args) { 
  SpringApplication.run(Application.class, args); 
 } 
 @Override 
 public void customize(ConfigurableEmbeddedServletContainer container) { 
  ///TODO 獲取未被占用的端口
  int port=8080
  container.setPort(port); 
 } 
}

springboot cloud消費(fèi)者

首先application.properties中要有eureka的配置信息,同上述的配置信息相同。

springboot的消費(fèi)者有兩種形式實(shí)現(xiàn)。

RestTemplate

在啟動類中增加@Bean

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class App {
 @Bean
 @LoadBalanced
 RestTemplate restTemplate() {
  return new RestTemplate();
 }
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}

建立一個(gè)Controller類

package com.mahuan.producer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class FirstContrller2 {
 @Autowired
 RestTemplate restTemplate;
 @RequestMapping(value = "/first")
 @ResponseBody
 public String first() {
  return restTemplate.getForEntity("http://compute-service/first", String.class).getBody();
 }
}

其中標(biāo)紅部分,為需要調(diào)用的application的name,后面為調(diào)用的path。如果在注冊中心中有多個(gè)擁有相同application.name的應(yīng)用,會自動進(jìn)行負(fù)載均衡。

Feign

建立一個(gè)interface

package com.mahuan.producer.controller;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "compute-service")
public interface ComputeService {
 @RequestMapping(method = RequestMethod.GET, value = "/first")
 String first();
}

其中@FeignClient說明要調(diào)用的application.name,@RequestMapping中說明調(diào)用的應(yīng)用path。

在Controller類中,直接@Autowired此接口即可。

同時(shí)啟動類中,需要增加@EnableFeignClients注解。

package com.mahuan.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class App {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
}

以上所述是小編給大家介紹的springboot及springboot cloud環(huán)境搭建,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • SpringBoot多配置切換的配置方法

    SpringBoot多配置切換的配置方法

    這篇文章主要介紹了SpringBoot多配置切換的配置方法及spring boot設(shè)置端口和上下文路徑的方法,需要的朋友可以參考下
    2018-04-04
  • idea如何快速查找一個(gè)類或類中方法名和變量

    idea如何快速查找一個(gè)類或類中方法名和變量

    這篇文章主要介紹了idea如何快速查找一個(gè)類或類中方法名和變量問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • java之a(chǎn)ssert關(guān)鍵字用法案例詳解

    java之a(chǎn)ssert關(guān)鍵字用法案例詳解

    這篇文章主要介紹了java之a(chǎn)ssert關(guān)鍵字用法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 關(guān)于Java應(yīng)用日志與Jaeger的trace關(guān)聯(lián)的問題

    關(guān)于Java應(yīng)用日志與Jaeger的trace關(guān)聯(lián)的問題

    這篇文章主要介紹了Java應(yīng)用日志如何與Jaeger的trace關(guān)聯(lián),通過jaeger發(fā)現(xiàn)這十次請求中有一次耗時(shí)特別長,想定位一下具體原因,感興趣的朋友跟隨小編一起看看吧
    2022-01-01
  • SpringBoot集成AOP的代碼示例

    SpringBoot集成AOP的代碼示例

    AOP是一種編程范式,它旨在將橫切關(guān)注點(diǎn)(cross-cutting concerns)從應(yīng)用程序的業(yè)務(wù)邏輯中分離出來,橫切關(guān)注點(diǎn)是那些在多個(gè)模塊中重復(fù)出現(xiàn)的功能,如日志記錄、性能監(jiān)控、事務(wù)管理、安全控制等,本文介紹了Spring Boot如何集成AOP,需要的朋友可以參考下
    2024-09-09
  • java實(shí)現(xiàn)圖的鄰接表存儲結(jié)構(gòu)的兩種方式及實(shí)例應(yīng)用詳解

    java實(shí)現(xiàn)圖的鄰接表存儲結(jié)構(gòu)的兩種方式及實(shí)例應(yīng)用詳解

    這篇文章主要介紹了java實(shí)現(xiàn)圖的鄰接表存儲結(jié)構(gòu)的兩種方式及實(shí)例應(yīng)用詳解,鄰接表構(gòu)建圖是必須需要一個(gè)Graph對象,也就是圖對象!該對象包含屬性有:頂點(diǎn)數(shù)、邊數(shù)以及圖的頂點(diǎn)集合,需要的朋友可以參考下
    2019-06-06
  • Java使用String.format方法格式化字符串的示例詳解

    Java使用String.format方法格式化字符串的示例詳解

    在編程過程中,我們經(jīng)常需要創(chuàng)建格式化的字符串來滿足特定的需求,比如生成用戶友好的消息、構(gòu)建報(bào)告或是輸出調(diào)試信息,Java 提供了一個(gè)強(qiáng)大的工具——String.format 方法,本文給大家介紹了Java使用String.format方法格式化字符串的示例,需要的朋友可以參考下
    2024-11-11
  • Spring使用注解存儲和讀取對象詳解

    Spring使用注解存儲和讀取對象詳解

    這篇文章主要給大家介紹了關(guān)于Spring如何通過注解存儲和讀取對象的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),有一定的參考價(jià)值,需要的朋友可以參考下
    2023-04-04
  • Spring Cloud中Eureka開啟密碼認(rèn)證的實(shí)例

    Spring Cloud中Eureka開啟密碼認(rèn)證的實(shí)例

    這篇文章主要介紹了Spring Cloud中Eureka開啟密碼認(rèn)證的實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • ScheduledThreadPoolExecutor巨坑解決

    ScheduledThreadPoolExecutor巨坑解決

    這篇文章主要為大家介紹了使用ScheduledThreadPoolExecutor遇到的巨坑解決示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評論

灌云县| 太和县| 宁乡县| 福泉市| 莒南县| 长葛市| 松溪县| 历史| 广西| 田林县| 谷城县| 天长市| 五指山市| 炉霍县| 中超| 淮滨县| 漠河县| 廊坊市| 堆龙德庆县| 东乌| 泰来县| 嘉义市| 民乐县| 泸溪县| 富宁县| 闽清县| 涪陵区| 苗栗市| 溧水县| 广灵县| 扬州市| 双鸭山市| 丰城市| 贞丰县| 若尔盖县| 江北区| 北流市| 宁化县| 禹州市| 新兴县| 平和县|