微服務架構java
一、分佈式服務組成的系統web
二、按照業務,而不是技術來劃分組織spring
三、作有生命的產品而不是項目tomcat
五、自動化運維( DevOps )架構
六、高度容錯性app
......負載均衡
以上是項目搭建目錄,分別是eureka-server、spring-servic-9090、spring-servic-909一、spring-servic-9092。運維
一、新建EurekaApplication做爲啓動Eureka服務。maven
package appeurkaservic; import org.spring
framework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */ @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } }
二、配置application.yml分佈式
registerWithEureka:因爲該應用爲註冊中心,全部設置爲false,表明不向註冊中心註冊本身。
fetchRegistry:因爲註冊中心的職責就是維護服務實例,它並不須要去檢索服務,全部也設置爲false。
server: port: 9999 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
三、添加依賴pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring-cloud-eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.4.RELEASE</version> </dependency> </dependencies> <!--添加springcloud--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
四、啓動 EurekaApplication,以後訪問http://localhost:9999/ ,端口在application.yml已經配置。
以後能夠看到eureka信息面板。
一、目錄
二、添加pom.xml依賴。
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.4.RELEASE</version> </dependency> </dependencies> <!--添加springcloud--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
三、新建application9090
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * 端口號9090 * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/20 */ @SpringBootApplication @EnableEurekaClient public class Application9090 { public static void main(String[] args) { SpringApplication.run(Application9090.class,args); } }
四、新建配置,配置服務Tomcat的端口號爲9090
package com.config; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/20 */ @Configuration public class Appconfig { @Bean public ConfigurableServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.setPort(9090); return factory; } }
五、新建測試IndexController
package com.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 微服務端口9090 * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/20 */ @Controller public class IndexController { @RequestMapping("index.do") @ResponseBody public String index(){ return "服務端口號:9090"; } }
六、以後啓動Application9090 訪問 http://localhost:9090/index.do
七、 查看eureka 出現9090服務表明已經註冊進來,其餘兩個是以前已經運行項目。
八、一樣新建spring-srvice-9091項目,按spring-srvice-9090同樣新建,修改相應的端口號便可。
一、pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.4.RELEASE</version> </dependency> </dependencies> <!--添加springcloud--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
二、Application9092
經過@EnableEurekaClient讓該應用成爲Eureka客戶端應用。
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */ @SpringBootApplication @EnableEurekaClient public class Application9092 { public static void main(String[] args) { SpringApplication.run(Application9092.class,args); } }
三、Appconfig 在裏面添加端口。
建立RestTemplate的spring Bean實例,並經過 @LoadBalanced 註解開啓客戶端負載均衡。
package com.config; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */ @Configuration public class Appconfig { @Bean public ConfigurableWebServerFactory webServerFactory(){ TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.setPort(9092); return factory; } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
四、測試IndexController
package com.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; /** * @Description * @Author ybb 34953126@qq.com * @Version V1.0.0 * @Since 1.0 * @Date 2019/10/21 */ @Controller public class IndexController { @Autowired RestTemplate restTemplate; @RequestMapping("client.do") @ResponseBody public String index(){ ResponseEntity<String> result = restTemplate.getForEntity("http://microservice-srvic1/index.do",String.class); return result.getBody(); } }
application.yml
server: port: 9092 spring: application: name: microservice-9092-srvic3 eureka: client: serviceUrl: defaultZone: http://localhost:9999/eureka/
五、啓動,spring-service-909一、spring-service-9092,查看spring eureka主頁。顯示以下表明三個服務註冊成功。
六、訪問spring-service-9092方法(http://localhost:9092/client.do)
便可隨機訪問9090和9091項目